Home > Software design >  Can't install Rails on MacOs. Error messages: Failed to build gem native extensions
Can't install Rails on MacOs. Error messages: Failed to build gem native extensions

Time:11-22

When trying to install Rails from my terminal, on my Mac OS Big Sur, by typing "sudo gem install rails" I get the error message: "Failed to build native extension". I have ruby 2.6.3 installed. I tried installing Xcode as I saw that being suggested in forums, but it still doesn't work. I don't know what to do:

Here, the error in the terminal:

% sudo gem install rails Building native extensions. This could take a while... ERROR: Error installing rails: ERROR: Failed to build gem native extension.

current directory: /Library/Ruby/Gems/2.6.0/gems/nio4r-2.5.8/ext/nio4r

/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby -I /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0 -r ./siteconf20211119-69718-1dr6qis.rb extconf.rb checking for unistd.h... *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options.

CodePudding user response:

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.

  • Related