Read more

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

Kim Klotz
September 15, 2015Software engineer at makandra GmbH

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).

Illustration money motivation

Opscomplete powered by makandra brand

Save money by migrating from AWS to our fully managed hosting in Germany.

  • Trusted by over 100 customers
  • Ready to use with Ruby, Node.js, PHP
  • Proactive management by operations experts
Read more Show archive.org snapshot

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.

Posted by Kim Klotz to makandra dev (2015-09-15 11:17)