Error building native extensions prevents non-sudo command working - ruby-on-rails

I'm upgrading an existing setup to use Rails 5 beta 3. As part of that, I've upgraded Ruby to version 2.2.4 (using RVM), i.e.
rvm install 2.2.4
rvm use 2.2.4
With that done, I'm trying to install the Rails gem. However, running
sudo gem install rails
gives me the error
ERROR: Error installing mime-types-data: mime-types-data requires Ruby version >= 2.0.
Having read this question, I've looked at my Ruby versions:
ruby --version => ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-linux]
sudo ruby --version => ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]
I don't seem to be able to access RVM from sudo, so I tried using gem install rails instead, which also fails while building native extensions:
Building native extensions. This could take a while...
ERROR: Error installing rails:
ERROR: Failed to build gem native extension.
/home/ubuntu/.rvm/rubies/ruby-2.2.4/bin/ruby -r ./siteconf20160405-31973-l7bzq7.rb extconf.rb
checking if the C compiler accepts ... *** 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.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/home/ubuntu/.rvm/rubies/ruby-2.2.4/bin/$(RUBY_BASE_NAME)
--help
--clean
/home/ubuntu/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/mkmf.rb:456:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /home/ubuntu/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/mkmf.rb:571:in `block in try_compile'
from /home/ubuntu/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/mkmf.rb:522:in `with_werror'
from /home/ubuntu/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/mkmf.rb:571:in `try_compile'
from extconf.rb:80:in `nokogiri_try_compile'
from extconf.rb:87:in `block in add_cflags'
from /home/ubuntu/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/mkmf.rb:619:in `with_cflags'
from extconf.rb:86:in `add_cflags'
from extconf.rb:336:in `<main>'
extconf failed, exit code 1
Gem files will remain installed in /home/ubuntu/.rvm/gems/ruby-2.2.4/gems/nokogiri-1.6.7.2 for inspection.
Results logged to /home/ubuntu/.rvm/gems/ruby-2.2.4/extensions/x86_64-linux/2.2.0/nokogiri-1.6.7.2/gem_make.out
I see two solutions to this:
Upgrade the Ruby that's used in sudo commands to 2.0 or above (preferably 2.2.4). I can't figure out how to do this, because sudo rvm results in command not found for rvm.
Fix the error that comes up when using gem install rails. Again, I don't know how to do this.
Which of these is the easiest (or is there an easier fix), and how do I do it?

It looks like you have installed RVM in single user mode. This is the reason why sudo rvm returns command not found error. You can find more about RVM modes here: https://rvm.io/rvm/install#2-load-rvm-into-your-shell-sessions-as-a-function.
But, based on log your provided, the error is in installing Nokogiri. Please install development header files first with command sudo apt-get install ruby-dev zlib1g-dev liblzma-dev. For details follow: http://www.nokogiri.org/tutorials/installing_nokogiri.html

The sudo command that you invoke will be executed as the root user, hence it won't load your profile settings (which is where the rvm ruby version is loaded, if it works like rbenv which is what I use, never used rvm)
On the other hand, whenever a Building native extensions fail when installing a gem it means that you need the development packages (C/C++ header files) for a library that is the one that actually does the whole thing (ruby gems are usually wrappers around it), so for instance, if you want to build ruby you will need the ruby development package from your linux distribution (or from brew if you use OSX), the development package from postgres to build pg gem, etc.

Related

Error Installing Rails 5.2.3 on Ubuntu 18.04

I'm trying to install Rails 5.2.3 on Ubuntu 18.04 PC using Ruby version -2.5.5 and during the installation I'm facing this error.
$ gem install rails -v 5.2.3
Building native extensions. This could take a while...
ERROR: Error installing rails:
ERROR: Failed to build gem native extension.
current directory: /home/amani/.rvm/gems/ruby-2.5.5/gems/nokogiri-1.10.4/ext/nokogiri
/home/amani/.rvm/rubies/ruby-2.5.5/bin/ruby -I /home/amani/.rvm/rubies/ruby-2.5.5/lib/ruby/site_ruby/2.5.0 -r ./siteconf20190831-23754-eu6kq8.rb extconf.rb
checking if the C compiler accepts ... *** 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.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/home/amani/.rvm/rubies/ruby-2.5.5/bin/$(RUBY_BASE_NAME)
--help
--clean
/home/amani/.rvm/rubies/ruby-2.5.5/lib/ruby/2.5.0/mkmf.rb:456:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /home/amani/.rvm/rubies/ruby-2.5.5/lib/ruby/2.5.0/mkmf.rb:574:in `block in try_compile'
from /home/amani/.rvm/rubies/ruby-2.5.5/lib/ruby/2.5.0/mkmf.rb:521:in `with_werror'
from /home/amani/.rvm/rubies/ruby-2.5.5/lib/ruby/2.5.0/mkmf.rb:574:in `try_compile'
from extconf.rb:138:in `nokogiri_try_compile'
from extconf.rb:162:in `block in add_cflags'
from /home/amani/.rvm/rubies/ruby-2.5.5/lib/ruby/2.5.0/mkmf.rb:632:in `with_cflags'
from extconf.rb:161:in `add_cflags'
from extconf.rb:416:in `<main>'
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/home/amani/.rvm/gems/ruby-2.5.5/extensions/x86_64-linux/2.5.0/nokogiri-1.10.4/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /home/amani/.rvm/gems/ruby-2.5.5/gems/nokogiri-1.10.4 for inspection.
Results logged to /home/amani/.rvm/gems/ruby-2.5.5/extensions/x86_64-linux/2.5.0/nokogiri-1.10.4/gem_make.out
These are the error messages on terminal.
I just looked at your terminal log and realized that you are using RVM (Ruby version manager) as your ruby manager. The catch here is that RVM seems to be heavyweight as it manages not just your ruby version but also your ruby gems. A better option for you should be to use rbenv (Ruby environment), which manages just your ruby version and Bundler which manages ruby gems.
This way you will rarely run into conflicts where RVM and Bundler will be doing the same thing (managing your ruby gems), since rails installation depends on Bundler.
Here's a simple command built-in that will remove RVM and its related directories from your system:
rvm implode
In order to remove the final trace of rvm, you need to remove the rvm gem, too:
gem uninstall rvm
If you've made modifications to your PATH you might want to pull those, too. Check your .bashrc, .profile and .bash_profile files, among other things.
You may also have an /etc/rvmrc file, or one in your home directory ~/.rvmrc that may need to be removed as well.
When you are done with the removal of RVM and all of its files and directories, follow this guide How To Install Ruby on Rails with rbenv on Ubuntu 18.04 to install a fresh copy of Ruby on Rails with rbenv on your Ubuntu machine.
If you still run into any issue relating to bundler while using rbenv, simply follow the instruction below:
You need to simply install an update for RubyGems Package Manager locally. To do this, run
gem update --system
If you don't have bundler version 2 installed locally, then run
gem install bundler
And then finally run
bundler update --bundler
That's all.
I hope this helps.

rails 5.0.0 when installing "nio4r" : Failed to build gem native extension

Here is the logs: http://pastebin.com/CAgur9xd
Installing nio4r 1.2.1 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
C:/RailsInstaller/Ruby2.2.0/bin/ruby.exe -r ./siteconf20160720-8272-c88sgk.rb extconf.rb --with-cflags=-std=c99
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.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=C:/RailsInstaller/Ruby2.2.0/bin/$(RUBY_BASE_NAME)
C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/mkmf.rb:456:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/mkmf.rb:587:in `try_cpp'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/mkmf.rb:1060:in `block in have_header'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/mkmf.rb:911:in `block in checking_for'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/mkmf.rb:351:in `block (2 levels) in postpone'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/mkmf.rb:321:in `open'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/mkmf.rb:351:in `block in postpone'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/mkmf.rb:321:in `open'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/mkmf.rb:347:in `postpone'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/mkmf.rb:910:in `checking_for'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/mkmf.rb:1059:in `have_header'
from extconf.rb:3:in `<main>'
extconf failed, exit code 1
when installing bundle it returns(starts at line 117 in the logs ):
Installing nio4r 1.2.1 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
So the bundle can't be installed. It returns at the end :
An error occurred while installing nio4r (1.2.1), and Bundler cannot continue.
Make sure that `gem install nio4r -v '1.2.1'` succeeds before bundling.
Note :I have tried the solutions in other questions, but it is still the same.
If it is possible to install "nio4r" manually please tell me how .
I am facing the same issue while I was trying to upgrade my Rails to V5.0.0, I follow this instruction and it works perfectly. Read the section "Install the Ruby DevKit" here >> http://jekyll-windows.juthilo.com/1-ruby-and-devkit/
Solution for Fedora 64bit
sudo dnf install redhat-rpm-config
I had a similar problem and running this command solved my issue.
bundle config build.nio4r --with-cflags="-std=c99"
Now you can successfully run bundle install afterwards.
I came across this problem as i was setting up rails too. (looking for an alternative to php).
I realized that i had installed x86 ruby with x64 Devkit, so i first downloaded x64 ruby which i installed and uninstalled the x86 version. I made sure my path variables were well set.
Then i installed devkit using the instructions on this page http://rubyonwindowsguides.github.io/book/ch02-04.html
Finally i installed rails and it worked fine without the errors this time
Hope this helps
My son fixed this problem for me. The problem was that I installed first, Ruby and then the Rails installer. So I had to uninstall Ruby and reinstall with the Rails installer only.
Had this same issue on Ubuntu 16.04.
What I did to fix this :
Remove Gemfile.lock : sudo rm Gemfile.lock
Installed Ruby-Dev:
sudo apt-add-repository ppa:brightbox/ruby-ng
sudo apt-get update
sudo apt-get install ruby2.4-dev
and the important step : REBOOT!
this may be related: GCC compilation error when installing Nokogiri
I found a error. i had a 2 gcc versions installed on my system and I just run brew unlink gcc and that solved the problem.
this was my case too. benw unlink gcc worked
Remove the Gemfile.lock and run bundle install command
To quote from your log output:
The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
On windows, you need to install the DevKit in order to be able to compile gems.
If you use Windows and installed ruby with RubyInstaller with version more or equal to 2.4, than you should not install DevKit from RubyInstaller website and only use 'msys64' which is installed after Ruby with RubyInstaller (make sure you type number 3 to install both msys and mingw). Delete devkit directory and its path from PATH environment variable, so when rubygems will try to install nio4r and call C compilers, it will call msys ones.
Read here https://rubyinstaller.org/2017/05/25/rubyinstaller-2.4.1-1-released.html for more info about MSYS2 and DevKit in RubyInstaller

How do I upgrade Rails?

I am new to Ruby and Rails, as well as all this command line melarky. So please bear with me.
I have installed, RubyGems, Homebrew, RVM, Rails etc, by following various tutorials.
I think I have all of this going now, but when I type:
rails --version
I get, 3.2.12. I notice that Rails 4 is out. How do I upgrade to this version?
If I do:
gem install rails
Even doing:
gem update rails
I still get 3.2.12.
Any ideas?
UPDATE
When trying a few options here, I get this message returned...
Michaels-MacBook-Pro:~ ParanoidAndroid$ gem install rails --version=4.0
Building native extensions. This could take a while...
ERROR: Error installing rails:
ERROR: Failed to build gem native extension.
/usr/local/rvm/rubies/ruby-1.9.3-p392/bin/ruby extconf.rb
*** 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.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/usr/local/rvm/rubies/ruby-1.9.3-p392/bin/ruby
--with-atomic_reference-dir
--without-atomic_reference-dir
--with-atomic_reference-include
--without-atomic_reference-include=${atomic_reference-dir}/include
--with-atomic_reference-lib
--without-atomic_reference-lib=${atomic_reference-dir}/lib
/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:381:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:461:in `try_link0'
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/mkmf.rb:712:in `try_run'
from extconf.rb:26:in `<main>'
Gem files will remain installed in /usr/local/rvm/gems/ruby-1.9.3-p392/gems/atomic-1.1.14 for inspection.
Results logged to /usr/local/rvm/gems/ruby-1.9.3-p392/gems/atomic-1.1.14/ext/gem_make.out
Seems there's an error happening. Any further ideas?
Thanks again everyone!
If you already have a Rails project you should:
Specify the desired version in your Gemfile (e.g. gem 'rails', '~> 5.2.0.rc1')
Run bundle install (bundle update may be needed)
Run rails app:update (rake rails:update for 4.2 and earlier)
Follow instructions on screen
Follow the official guide for additional steps depending on your version
If you are using RVM then you should create a seperate gemset first like:
rvm gemset create whateverName
in this example I will install rails 4
rvm install 2.0.0
rvm list
rvm 2.0.0
^^^^(you may need to copy and paste the version number exactly here as it was displayed in rvm list)
rvm gemset create rails4
rvm gemset use rails4
add the following to your gemfile for existing apps
gem 'rails', '4.0.0'
then run
bundle update rails
or do following to just install the gem
gem install rails --version=4.0
UPDATE
If you havne't installed Xcode 4.5 CLI tools then you will need to
Preferences > Downloads > Components
DMG
https://developer.apple.com/downloads
you may then also need to use homebrew to update gcc
brew install apple-gcc42
the following link has all the information you will need:
https://thoughtbot.com/blog/the-hitchhikers-guide-to-riding-a-mountain-lion
gem update rails does nothing for you, because bundler makes sure that only the gems specified in your Gemfile are loaded and used.
So in order to upgrade to Rails 4, you need to change the Rails version number in your Gemfile and run bundle update rails.
This Railscast guides you through all the steps of manually updating an existing application from Rails 3.2 to Rails 4:
http://railscasts.com/episodes/415-upgrading-to-rails-4
You can install either with specific version OR putting gem version in itself Gemfile.
gem install rails -v 4.0.0
You can get a list of all gem versions with
gem list rails --remote --all
To install specify version 4.0.1 for example you can use
gem install rails -v 4.0.1
Or you could just specify the version in your Gemfile
Best to use gem list --remote --all. Unless your looking for that particular version as long as the gemset for that project has been established

Install Rails Failed on OSX 10.8.4

I always failed to install rails on my new Air with OSX 10.8.4, even I succeed installed ruby2, Command Line Tool of XCode, it still prompts some error when I try to install rails.
I found lots solutions from the Internet, but they all don't work on my issue, I'm completely confused, doesn't any can help me? I'll really appreciate!!!
localhost:myapp zerocool$ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.3.0]
localhost:myapp zerocool$ sudo gem install rails
Password:
Building native extensions. This could take a while...
ERROR: Error installing rails:
ERROR: Failed to build gem native extension.
/Users/zerocool/.rvm/rubies/ruby-2.0.0-p247/bin/ruby extconf.rb
*** 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.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/zerocool/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
--with-atomic_reference-dir
--without-atomic_reference-dir
--with-atomic_reference-include
--without-atomic_reference-include=${atomic_reference-dir}/include
--with-atomic_reference-lib
--without-atomic_reference-lib=${atomic_reference-dir}/
/Users/zerocool/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:434:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /Users/zerocool/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:519:in `try_link0'
from /Users/zerocool/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:817:in `try_run'
from extconf.rb:24:in `<main>'
Gem files will remain installed in /Users/zerocool/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/atomic-1.1.12 for inspection.
Results logged to /Users/zerocool/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/atomic-1.1.12/ext/gem_make.out
I ran into this issue today and it seems that my download of ruby-2.0.0 did not work properly. I would recommend trying rvm reinstall 2.0.0 followed by gem install rails.
Finally, I got the solution! Run rvm implodeļ¼Œand use rm to delete /etc/rvmrc and ~/.rmvrc, restart your mac, and you can use gem install rails to install rails successfully.
First I installed gcc via homebrew:
installing gcc on mavericks using brew
brew tap homebrew/dupes
brew install apple-gcc42
Then I symlinked the newly installed apple-gcc42
into my /usr/bin folder
cd /usr/bin
sudo mv gcc gcc_mavs sudo ln -s /usr/local/Cellar/apple-gcc42/4.2.1-5666.3/bin/gcc-4.2 gcc
Credits: https://coderwall.com/p/lqpp8w
I had this same error when running against ruby-2.0.0-p195, but updating to ruby-2.0.0-p247 (the current RVM default for 2.0.0) fixed it.

Unable to install Rails extconf.rb failed

I'm new to Ruby on Rails, and I'm getting the below error when I'm installing it.
Please note, I have installed the latest Xcode and installed command line tools, and I'm using OSX 10.8.4. I've installed Ruby 2.0.0 as well.
-bash-3.2$ sudo gem install rails
Password:
Building native extensions. This could take a while...
ERROR: Error installing rails:
ERROR: Failed to build gem native extension.
/Users/WillieTran/.rvm/rubies/ruby-2.0.0-p247/bin/ruby extconf.rb
*** 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.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/Users/WillieTran/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
--with-atomic_reference-dir
--without-atomic_reference-dir
--with-atomic_reference-include
--without-atomic_reference-include=${atomic_reference-dir}/include
--with-atomic_reference-lib
--without-atomic_reference-lib=${atomic_reference-dir}/
/Users/WillieTran/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:434:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /Users/WillieTran/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:519:in `try_link0'
from /Users/WillieTran/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/mkmf.rb:817:in `try_run'
from extconf.rb:24:in `<main>'
Gem files will remain installed in /Users/WillieTran/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/atomic-1.1.12 for inspection.
Results logged to /Users/WillieTran/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/atomic-1.1.12/ext/gem_make.out
I had similar problem and it gets solved by installing command line tools:
xcode-select --install
The symlinking did not solve the problem for me. I uninstalled my ruby that I previously had installed using rvm by typing rvm uninstall ruby-2.0.0 and then I typed rvm requirements. This command found that I needed a few other things, such as gcc46. It's strange that the gcc compiler was needed, even though I had installed the xcode command line tools, and gcc was most definitely installed on my system.
So, if none of the usual methods are working, give rvm requirements a shot!
I guess it's a same problem with this question.
Try sudo ln -s /usr/bin/{llvm-,}gcc-4.2 and gem install rails again.
By the way, you're in your own home directory, you don't have to use sudo.
I just ran into this with ruby 2.5.3 and was able to install cairo by calling:
PKG_CONFIG_PATH=/usr/local/opt/libffi/lib/pkgconfig gem install cairo -v '1.15.2'
Took a while to find something that worked, as I already had the command line tools installed, and am using asdf not rvm.
Thanks to this thread for helping with the fix: https://github.com/ruby-gnome2/ruby-gnome2/issues/259#issuecomment-59751807

Resources