Error installing gem with native extension (collect2: error: ld returned 1 exit status)

Posted . Visible to the public.

If you have problems installing a gem and get a error collect2: error: ld returned 1 exit status it's due to missing development headers of a library (ld is the linker).

For example, with this output:

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

   /home/foobar/.rvm/rubies/ruby-2.2.3/bin/ruby -r ./siteconf20150915-3539-1i9layj.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 /home/foobar/.rvm/gems/ruby-2.2.3/gems/json-1.8.3 for inspection.
Results logged to /home/foobar/.rvm/gems/ruby-2.2.3/extensions/x86_64-linux/2.2.0/json-1.8.3/gem_make.out

You can see the linker can't find gmp (/usr/bin/ld: cannot find -lgmp).

Solution:

search for library with gmp in it:

$ apt-cache search gmp       
libgmp-dev - Multiprecision arithmetic library developers tools
libgmp10 - Multiprecision arithmetic library
libgmp10-doc - Multiprecision arithmetic library example code
libgmp3-dev - Multiprecision arithmetic library developers tools
libgmpxx4ldbl - Multiprecision arithmetic library (C++ bindings)
[...]

check if you have installed the development package (packages with -dev in the name):

$ dpkg -l | grep gmp`
ii  libgmp10:amd64                              2:5.1.3+dfsg-1ubuntu1                               amd64        Multiprecision arithmetic library

If the ...-dev packages are not in the list, install them with sudo apt-get install libgmp-dev libgmp3-dev and after that the gem install json should work.

Last edit
Kim Klotz
License
Source code in this card is licensed under the MIT License.
Posted by Kim Klotz to makandra dev (2015-09-15 09:17)