2

When I install gem <insert gem name> it fails with error ERROR: Failed to build gem native extension.

For example, when I try to install gems json, eventmachine, mysql2 on a new server it almost always fails.

NOTE: This is a QA-type question, i.e. please see below for the solution proposed by me or join the discussion.

Yaro Holodiuk
  • 568
  • 6
  • 15

2 Answers2

6

You can go to terminal and write it down sudo apt-get install libmysqlclient-dev.

I hope this help you.

akbarbin
  • 4,985
  • 1
  • 28
  • 31
5

This error often occurs on newly created servers, so, naturally, the error means that some dependencies are missing.

The "twist" is, the error itself usually tells us which dependencies are missing, but we skim over the description and try to google 'failed to build gem ...'. It is better to inspect the error output first.

For example, let us take a look at output while trying to install gem 'json' on a new machine:

user@server:~$ gem install json -v '1.8.3'
Fetching: json-1.8.3.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing json:
    ERROR: Failed to build gem native extension.

    .../.rvm/rubies/ruby-2.2.2/bin/ruby -r ./siteconf20151204-22068-1ek4f2f.rb extconf.rb
creating Makefile

make "DESTDIR=" clean

make "DESTDIR="
compiling generator.c
linking shared-object json/ext/generator.so
/usr/bin/ld: cannot find -lgmp
collect2: error: ld returned 1 exit status
make: *** [generator.so] Error 1

make failed, exit code 2

Gem files will remain installed in .../.rvm/gems/ruby-2.2.2/gems/json-1.8.3 for inspection.
Results logged to .../.rvm/gems/ruby-2.2.2/extensions/x86_64-linux/2.2.0/json-1.8.3/gem_make.out

After inspecting the error you can see what is missing:

/usr/bin/ld: cannot find -lgmp

So what is -lgmp? Let me describe my path of trial and error while finding it out. After googling a little I have come to discover that l in lgmp stands for library and that GMP is a C library that was not yet installed on my machine. My next google query was 'install gmp ubuntu', which led me to install libgmp3-dev and the problem was solved.

Now, let us take a look at the output while installing mysql2 on a new server:

user@server:~$ gem install mysql2 -v '0.3.20'
Fetching: mysql2-0.3.20.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    .../.rvm/rubies/ruby-2.2.2/bin/ruby -r ./siteconf20151204-9782-1eobqf2.rb extconf.rb
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for mysql_query() in -lmysqlclient... no
-----
libmysqlclient is missing. Trying again with extra runtime libraries...
-----
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for main() in -lnsl... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
-----
libmysqlclient is missing. You may need to 'apt-get install libmysqlclient-dev' or 'yum install mysql-devel', and try again.
-----
*** 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.
# SNIP

Again, while looking closely you can see that libmysqlclient is missing, and the error even provides the most obvious solution: You may need to 'apt-get install libmysqlclient-dev' or 'yum install mysql-devel', and try again.

I hope this will help developers solve such kind of errors faster without having to google for every specific gem failure, which often brings no results at all.

Yaro Holodiuk
  • 568
  • 6
  • 15