Problems installing Ruby Gems that require native extensions on the Mac

created at 28/12/2011

Problems installing Ruby Gems that require native extensions on the Mac

If you're using a Mac for your development work, you may have recently experienced problems installing Ruby GEMs that require native extensions to be built. Message such as the following have been driving me nuts here for a while:
gem install nokogiri

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

       /Users/skex/.rvm/rubies/ruby-1.9.2-p290/bin/ruby extconf.rb
checking for libxml/parser.h... *** 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.
While there can be many different reasons for this error to occur, if you're on a Mac there's a new one to look out for, in particular if this problem happens for every Ruby Gem you are trying to install. You get your first hint when looking - as suggested - at the mkmf.log file, which for us looked like this:
$ more mkmf.log

"/usr/bin/gcc-4.2 -o conftest -I/Users/skex/.rvm/rubies/ruby-1.9.2-p290/include/ruby-1.9.1/x86_64-darwin10.7.0 -I......."
checked program was:
/* begin */
1: #include "ruby.h"
3: int main() {return 0;}
/* end */
You will find the mkmf.log file in the installation directory of the Gem you are trying to install - in our case it was in
The interesting thing about the mkmf.log file is that it doesn't contain any error message at all. I noticed that the compiler was referenced as
so I had a look....and sure enough, there was no file named gcc-4.2 in the /usr/bin directory - however there was a file called
so I tried to help the compiler along by creating a softlink:
sudo ln -s llvm-gcc-4.2 gcc-4.2
I tried the gem installation again - and it went through without any problem. The issue seems to have been one of timing - when we installed ruby1.9.2-p290 using rvm, there had been a compiler named gcc-4.2 in that directory, however sometime after ruby was installed, we upgraded XCode on the Mac, and with the latest XCode came a new default compiler (llvm), and apparently also a new compiler naming convention: from gcc-4.2 to llvm-gcc-4.2. You can check if you are affected by this problem by either performing the above fix (creating the softlink) and seeing if your problems go away then, or by checking which compiler has been used to build the version of ruby you are having problems with:
ruby -rrbconfig -e 'puts Config::CONFIG["CC"]'
If you are getting
back, then your ruby was built using this compiler - which now no longer exists and explains why you're having these probhlems, as - unless you specify something else - the same compiler command line is used to build your native extensions.