67

The error message:

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

    /usr/bin/ruby2.1 -r ./siteconf20150328-1540-hff2f0.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=/usr/bin/ruby2.1
  --help
  --clean
/usr/lib/ruby/2.1.0/mkmf.rb:456:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
  from /usr/lib/ruby/2.1.0/mkmf.rb:571:in `block in try_compile'
  from /usr/lib/ruby/2.1.0/mkmf.rb:522:in `with_werror'
  from /usr/lib/ruby/2.1.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 /usr/lib/ruby/2.1.0/mkmf.rb:621:in `with_cflags'
  from extconf.rb:86:in `add_cflags'
  from extconf.rb:337:in `<main>'

extconf failed, exit code 1

Gem files will remain installed in /var/lib/gems/2.1.0/gems/nokogiri-1.6.6.2 for inspection.
Results logged to /var/lib/gems/2.1.0/extensions/x86_64-linux/2.1.0/nokogiri-1.6.6.2/gem_make.out

Configurations:

  1. Ubuntu 14.10 (GNU/Linux 3.16.0-23-generic x86_64)
  2. gcc (Ubuntu 4.9.1-16ubuntu6) 4.9.1
  3. ruby 2.1.2p95 (2014-05-08) [x86_64-linux-gnu]

5 Answers 5

230

After upgrading to Rails 4.2.4 on Ubuntu 14.04 using RVM I needed to do this:

sudo apt-get install libgmp-dev

or

sudo apt-get install libgmp3-dev

Full stacktrace: http://ruby-on-rails-eq8.blogspot.co.uk/2015/10/solving-ubuntu-1404-ruby-c-dependancy.html

1
  • 4
    MacKinley Smith, than you for the thought, but sorry, I guess "no". The original answer by shivam was for a problem years ago and was a valid answer. By pure coincidence the stack trace of the original question was similar when I was googling for a solution of my problem/answer and this was the top hit in Google. That's why I stick the answer here. So I'm glad my answer helps, but I don't want anyone loose their points for sake of it. Oct 30, 2015 at 8:50
57

You seem to be missing some Ruby requirement. If you are using RVM, try:

rvm requirements

to get the list of dependencies and install them manually.

Otherwise, install the known basic requirements directly using:

sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libxml2-dev autoconf libc6-dev ncurses-dev automake libtool
6
  • 7
    I don't use rvm, but the second solution worked for me. Thank you very much!
    – Allen
    Mar 28, 2015 at 13:40
  • the apt-get command does not work on Fedora take a look at my answer below Aug 4, 2015 at 2:44
  • 1
    Doesn't work, still fail with /home/peng/.rvm/rubies/ruby-2.2.3/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.
    – tribbloid
    Sep 20, 2015 at 21:01
  • @tribbloid Same here. Could you resolve the issue meanwhile?
    – JJD
    Oct 13, 2015 at 8:15
  • same with rbenv.. needed build essentials
    – don.najd
    May 2, 2016 at 18:45
7

You get this error because the development dependencies for Ruby are missing.

I suggest that you install and use RVM to solve this, check the official and updated instructions here https://rvm.io/rvm/install

At the time when I answer this question I followed these steps:

gpg --keyserver hkp://keys.gnupg.net --recv-keys
409B6B1796C275462A1703113804BB82D39DC0E3

\curl -sSL https://get.rvm.io | bash

If you get this warning

  * WARNING: You have '~/.profile' file, you might want to load it,
    to do that add the following line to '/home/mao/.bash_profile':

  source ~/.profile

execute this command:

echo source ~/.profile >> ~/.bash_profile

Then, reload/apply the bash_profile that you just updated by executing this:

source ~/.bash_profile

Finally run this to install the requirements:

rvm requirements

This should work for any Linux distribution like Ubuntu, Fedora, Debian, Redhat, Slackware.

2
  • 1
    "\curl -sSL https://get.rvm.io | bash' is one of several options for installing RVM. Rather than follow this little excerpt from RVM's documentation here, read and follow the installation instructions on RVM's site. On the surface RVM is easy to install, but we repeatedly get questions from people who didn't follow every debugging step when something goes wrong, and that page covers it all. Jun 8, 2017 at 16:01
  • @theTinMan I have updated my answer to mention the RVm Install page as the updated way to do it. Jun 20, 2017 at 14:55
1

For Centos 7, Redhat 7 :

yum -y install gcc ruby-dev
1
  • This also works on some debian variants. (apt-get install ruby-dev) Worked for me anyway.
    – tekHedd
    Jun 18, 2020 at 22:10
0

You can install build-essential to setup devtools in ubuntu:

apt-get install build-essential 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.