Mac: Failed to build gem native extension

When installing ruby on rails, vagrant, rmagick, sqlite3-ruby, mysql or any other software using ruby native extensions, you might get the following error message:

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

I got this error because I was on a new Mac and didn’t have XCode installed on this machine (using another one for Mac development). And since it’s over 1 GB of disk space I’d need for Xcode, I wanted to avoid having to install it just to get this other stuff installed. The alternative are the “command line tools”. So I went to, logged in with my Apple ID, downloaded the latest version of the “Command Line Tools (OS X Mountain Lion) for Xcode” and installed them.

But even with the command line tools install, I was still getting the following error message (which was also there before installing the tools):

Configuring libffi for i386
configure: WARNING: if you wanted to set the --build type, don't use --host.
    If a cross compiler is detected then cross compile mode will be used
configure: error: in `/Library/Ruby/Gems/1.8/gems/ffi-1.8.1/ext/ffi_c/libffi-i386':
configure: error: C compiler cannot create executables
See `config.log' for more details
make[1]: *** No targets specified and no makefile found.  Stop.
make: *** ["/Library/Ruby/Gems/1.8/gems/ffi-1.8.1/ext/ffi_c"/libffi-i386/.libs/libffi_convenience.a] Error 2

Then I had a look at config.log, located there (you might have a different path, when using a newer version):


And foung the following error:

xcode-select: Error: No Xcode is selected. Use xcode-select -switch , or see the xcode-select manpage (man xcode-select) for further information.

The solution is to use xcode-select but it took me a few tries to find out what I was supposed to use as path without Xcode installed:

sudo xcode-select -switch /usr/lib/

This error was gone but I got a few error messages like this:

xcrun: Error: failed to exec real xcrun. (No such file or directory)

Looking at the man pages:

xcrun provides a means to locate or invoke coexistence- and platform-aware developer tools from the command-line, without requiring users to modify makefiles or otherwise take inconvenient measures to support multiple Xcode tool chains.

Well, it didn’t really help me much…

The command causing looked like this:

xcrun cc -I. -I. -I/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/universal-darwin12.0 -I. -DRUBY_EXTCONF_H=\"extconf.h\"  -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE   -fno-common -arch i386 -arch x86_64 -g -Os -pipe -fno-common -DENABLE_DTRACE  -fno-common  -pipe -fno-common  -I/Library/Frameworks/Mono.framework/Versions/2.10.11/lib/libffi-3.0.11/include    -c MappedType.c

So xcrun just gets a command line as parameters. So first I checked that cc was working:

$ cc --version
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.4.0
Thread model: posix

Then since just calling cc was working, I was wondering why I’d need xcrun… Maybe the solution is just to create my own xcrun just executing the command line provided as parameters.

First I made a back up of xcrun, then created a shell script there:

sudo mv /usr/bin/xcrun /usr/bin/xcrun.sav
sudo vi /usr/bin/xcrun

Now in vi, I’ve put the following contents in xcrun:


It just execute the same command line but without the first xcrun. Of course, you need to give it execute rights:

sudo chmod +x /usr/bin/xcrun

And then executing gem install finally worked ! I guess there is probably a cleaner way to do it but at least I got it working even though it was late at night and I was almost sleeping on the keyboard !

Update: The trick with xcrun might cause problems installing some other software e.g. osxfuse. So you might have to remove it afterwards:

sudo mv /usr/bin/xcrun /usr/bin/xcrun.sav2
sudo mv /usr/bin/xcrun.sav /usr/bin/xcrun

If you need it again, you can restore it like this:

sudo mv /usr/bin/xcrun /usr/bin/xcrun.sav
sudo mv /usr/bin/xcrun.sav2 /usr/bin/xcrun

2 thoughts on “Mac: Failed to build gem native extension

Leave a Reply

Your email address will not be published.