ERROR: Failed to build gem native extension (ruby extconf.rb): Mac OSX
Try this:
ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future gem install <gemName>
For an explanation see this answer
Can't install Rails on MacOs. Error messages: Failed to build gem native extensions
I have encountered a similar problem more than once.
I can think of three potential causes and solutions, though your problem may be rooted in something different.
1. Ruby version mismatch
You thought you were using Ruby 2.6.3. But actually the ruby your gem command recognises is a Ruby in a different version.
Check it out with ruby --version
and make sure it is the right one.
If you have Rails in Ruby in a previous version, and if you have upgraded your Ruby since, then this type of error might be raised, depending on how your package management system handles the case.
If so, try uninstalling Rails first and reinstalling it.
2. Problem of installing Ruby
For some reason, the Ruby you are trying to use (2.6.3) has not been correctly installed. Then, a simple solution is to reinstall the Ruby, making sure it is correctly installed.
It highly depends which system you are using, but if you are using rbenv
,
rbenv uninstall 2.6.3
rbenv install 2.6.3
rbenv rehash
should do the job. You can then reinstall your Rails.
3. Problem of permission (directory writing etc)
In your case, you run sudo gem install rails
, and it probably means you have installed Ruby and Gems, including Rails, as the superuser while your Rails app directory is in a normal user.
I understand it is generally safer to install Ruby and Gems as the same user that runs Rails (mind you, though, it may not be impossible in practice in some specific environments). When the users are different, all sorts of permission problems can be encountered, and to install Rails apps are also problematic – it's possible but not straightforward in my experience.
So, my recommendation is, if ever possible and practical for you, to delete all the installed Ruby and reinstall it (=Ruby) as a normal user, who owns a Rails app directory, without using sudo
. For example, if you are using rbenv
, uninstall rbenv
, delete all the directory related to rbenv
, and reinstall rbenv
from scratch as the normal user. Or, if you are using the system default Ruby, I'd recommend to install the Ruby-version management system, that is either rbenv
or rvm
, as the normal user, install Ruby(s) with it, and manage the installed Ruby versions with it.
If neither is possible or practical, check out the directory permission where Gems are installed and make sure they are consistent with what you would expect. Personally, I would make all the efforts to use a Ruby-version management system whenever I use Rails.
ERROR: Error installing cocoapods: ERROR: Failed to build gem native extension
Run following command
brew cleanup -d -v
Then run below command
brew install cocoapods
Note: If you see failed to link then run brew link cocoapods
If linking is getting failed then run
brew link --overwrite cocoapods
This is how it get solved for me
For someone who is having m1 chip system they need to run this via Rosetta
select Terminal and press cmd(⌘)+I and check the "Open using Rosetta" option. ( For m1 chip based mac )
CocoaPods installation failed Failed to build gem native extension
Checkout this post stackoverflow. If it doesn't work, try sudo less installation, here's the link for that cocoapods sudo less installation
Ruby: Compilation error during installation of ‘rails’ gem on macOS
Building gems with native extensions is always a pain. You need to have several libs or compile tools installed. In case of nokogiri it's libxml. For this reason i would recommend to use rvm for getting started. RVM is not the cleanest tool, when it comes to unix philosophy, but it's a full-stack solution, meaning that it just works out of the box. It will install all the dependencies for you, using the default osx package manager. As a package manager i recommend homebrew.
#install homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
#install rvm into $HOME
brew install gpg
gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
\curl -sSL https://get.rvm.io | bash -s stable
rvm install ruby --latest
gem install rails #no sudo, as ruby is in $HOME
Failed to build gem native extension
The problem is that in Lion Xcode 4.3 doesn't by default install (or even include) the command-line tools such as gcc - and those are what are needed here. In the Downloads pane of Xcode's prefs, under Components, ask for the command-line tools. (See the readme: https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/WhatsNewXcode/Chapters/xcode_4_0.html#//apple_ref/doc/uid/TP40016147-SW35)
Actually I believe you didn't need to install Xcode at all, since in fact the command-line tool can now be downloaded and installed separately.
How to install json gem - Failed to build gem native extension(mac 10.10)
Sad, however, JSON-1.7.7 (and upto 1.8.1) is incompatible with Ruby-2.2.x. As you are using Ruby-2.2.1, it will not work for you.
There are 2 options for you:
- Update your
json
gem to1.8.2
version. -- Preferable - Edit
ruby-2.2.1/ext/json/fbuffer/fbuffer.h
file and replace problematic code withVALUE result = rb_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
. Look here for details
Related Topics
Selenium Scroll Element into (Center Of) View
How to Change Column Type in Heroku
Putting the Results of Pp (Or Anything Outputted to Console) into a String
Activerecord::Connectiontimeouterror
Ruby Gem Dependencies on Offline Server
How to Dump Strings in Yaml Using Literal Scalar Style
Rails - Redirecting Console Output to a File
Determining If a Variable Is Within Range
Ruby 1.9 - Invalid Multibyte Char (Us-Ascii)