How to fix gem install error


You are trying to install a Ruby gem but installation is failing with an error something like this,

Failed to build gem native extension

Most likely cause of these errors is missing build dependencies. Let’s dig into a couple of solutions.


First make sure you have these minimum prerequisites installed on your system,

sudo apt-get install ruby-full build-essential zlib1g-dev

Next I’ll give you an example on how you can search and install missing build dependencies for a particular case.
Let’s say I’m’ trying to install the gem sass on my system and installation is failing with errors,

gem install sass
Building native extensions.  This could take a while...
ERROR:  Error installing sass:
    ERROR: Failed to build gem native extension.
/usr/bin/ruby2.2 -r ./siteconf20180217-38265-1l6kefu.rb extconf.rb
checking for ffi.h... no
checking for ffi.h in /usr/local/include,/usr/include/ffi... no
checking for shlwapi.h... no
checking for rb_thread_blocking_region()... no
checking for rb_thread_call_with_gvl()... yes
checking for rb_thread_call_without_gvl()... yes
creating extconf.h
creating Makefile

make "DESTDIR=" clean

make "DESTDIR="
Running autoreconf for libffi
/var/lib/gems/2.2.0/gems/ffi-1.9.21/ext/ffi_c/libffi/ 2: exec: autoreconf: not found
make: *** ["/var/lib/gems/2.2.0/gems/ffi-1.9.21/ext/ffi_c/libffi-x86_64-linux-gnu"/.libs/libffi_convenience.a] Error 127

make failed, exit code 2

In the above output what you need to look for is the lines ending with a no and containing file names that end with a .h. We can extract three lines that meet our criteria,

checking for ffi.h... no
checking for ffi.h in /usr/local/include,/usr/include/ffi... no
checking for shlwapi.h... no

First two lines have the same header file ffi.h while the third line mention the header file shlwapi.h. So the build is failing because of these two missing header files. We need to install packages that provide these header files.
How do we find these packages? Well, the solution mentioned here is for Debian but can easily be adopted for other distributions.
Open the Debian package content search page,

Debian package content search

In the Keyword field enter ffi.h. Under the Display section select packages that contain files named like this. Finally select Debian Distribution, Architecture and hit the Search button.
From the search result page we can see that libffi-dev is the package that provides ffi.h header file. We repeat the above search procedure for shlwapi.h and find that mingw-w64-i686-dev provides that particular header file. Now all we need to do is install these packages,

sudo apt-get install libffi-dev mingw-w64-i686-dev

Now sass gem can be installed without any build errors.
You can adopt this approach to identify missing dependencies and find the relevant packages providing those dependencies.


 Date: March 26, 2021
 Tags:  how-to gem

I don't have comments on this blog as they not only are difficult to manage, but are also prone to privacy and security risks.
Instead of leaving a comment, you could email me directly.


Liked what you just read? Please consider buying me a cup of coffee!

⏪ How to fix Jekyll build warnings

A better term than "Google it" for web search ⏩