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

Follow me on Twitter to communicate and stay connected

{ 3 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

Leave a Comment