How Do I Install A Ruby Gem With Native Extensions On Windows?

How Do I Install A Ruby Gem With Native Extensions On Windows?

One of the challenges many Ruby on Windows users have is how to easily use native RubyGems from the community such as the rdiscount or redcloth gems.

Many Window users have seen this error:

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

For most gems this isn’t a problem because the extension author has provided a binary gem compatible with the users Ruby environment. In other cases, only the extension source code is available in the gem and the user is expected to have a “sane” environment installed and configured to build and use the native gem.

A “sane” environment typically means a system with make, gcc, sh and similar *nix build tools installed. This expectation of a “sane” build environment is not always true for Windows users and is the primary reason why the DevKit was created. The DevKit, based upon MSYS and MinGW components, is an easy-to-install and easy-to-use solution for quickly setting up a “sane” build environment, enabling you to use most native RubyGems developed by the Ruby community.

Requirements

  • Window XP or greater (32 or 64-bit).
  • A Ruby installation from RubyInstaller.

Steps involved

  1. Download and install 7-zip.
  2. Download the Development Kit and install it in say c:\devkit.
  3. Open a command window and type c:.
  4. Change folders by typing cd devkit.
  5. Type ruby dk.rb init to generate the config.yml file to be used later. Your installed Rubies will be listed there (only those installed by a RubyInstaller package are detected at present). This needs to be done only once.
  6. Finally type, ruby dk.rb install for DevKit enhance your installed Rubies. This needs to be done only once.
  7. Confirm your Ruby environment is correctly using the DevKit by running: gem install bson_ext --platform=ruby. It’s crucial that you include the --platform=ruby option to force RubyGems to build the native gem rather than potentially installing an incorrect binary gem.

That’s it.

is an author and founder of RubyLearning.com and RubyLearning.org where over 35000 participants have learnt Ruby programming from across the globe.

Posted by Satish Talim

{ 6 comments… read them below or add one }

Satish Talim November 12, 2011 at 6:25 am

Thanks to Luis Lavena for pointing out to the Troubleshooting page.

Reply

Alan April 5, 2013 at 9:57 pm

Hello Satish,
Thanks for this, very helpful.
I completed all the steps and it all seemed to work. But I still got “Failed to build gem native extension.”

Reply

Satish Talim April 6, 2013 at 6:26 am

Alan, did you check out the Troubleshooting page?

Reply

Toshi May 30, 2013 at 4:03 am

I’ve checked Troubleshooting page and follow all steps but none of the possible causes were the reason of the error.

Still getting this: “Failed to build gem native extension.”

Reply

Basti November 22, 2013 at 5:45 pm

Great solution, though I’m having the same problem as Alan. Followed instructions on the Troubleshooting page, though nothing helped me.

Reply

Joseph November 23, 2013 at 11:53 am

My specific error is with “atomic 1.1.14″, and I’m on Windows 7.
Despite following the directions here, I still received the same error.
ERROR: Error installing atomic:
ERROR: Failed to build gem native extension.
I also checked out the two different scenarios in Satish’s troubleshooting page, and I was fine on both counts.
I was trying it with Windows to avoid using a virtual machine, but I no longer think it’s worth the trouble. If I find a solution to people who still have issues, I’ll try to make it back here to share it, but I don’t see that happening.

Reply

Leave a Comment

{ 1 trackback }