Wednesday, December 30, 2009

Resolved: "failed to build gem native extension" on Windows

- Windows XP SP3
- Ruby 1.8.6 (patchlevel 383, i386-minw32)

When trying to do "gem install" on certain gems, I got a message "failed to build gem native extension" and the installation would fail. Some common gems that this happens to include ruby-debug, ruby-debug-ide, hpricot, sqlite3-ruby, and ruby-oci8.

Install the Ruby Development Kit from RubyForge. It's currently only available as a .7z file, so you'll need 7Zip to open it. Follow the instructions in the included INSTALL.txt file, but make sure when you copy the bin folder to your Ruby installation that you copy just the files inside it. Don't overwrite your Ruby installation's bin folder. That would be very bad.

So what is a native extension and why is trying to install this?
Your Ruby interpreter (the ruby.exe program) is written C (or Java if it's jruby). Certain gems need to actually add C libraries to your Ruby installation in order to function properly, not just ruby-language files. They're usually downloaded as source (not binaries) and compiled on your machine to match your system's architecture. The problem is when, for instance, your machine doesn't have a C compiler available or not the same one as the gem is expecting (you may see something about "MSC version unmatch" if the version is different). The Ruby Dev Kit comes with a C compiler (gcc) that runs on Windows and is (probably) compatible with the gems. This resolved my issue.

The fact that you have to download the Dev Kit for building native extensions is apparently a recent change (Nov. '09), and it probably only applies to Windows Ruby installations. This change is currently not well documented, which is why I'm posting my findings.

This can also manifest itself in NetBeans IDE if you opt to install the FastDebugger and get similar error messages about native extensions failing to build.

Related Links:

1 comment:

Miklós said...

Thanx! Awesome it works