Saturday, June 18, 2016

Building native extensions for Ruby gems

I decided to write about this because I visit StackOverflow everyday and this is one of the most common questions about RoR there.

When you run bundle install in your application directory, you will see something like this:

You may notice that we have two kinds of installation messages. The first, and more common, says only

Installing <gem_name> <gem_version>

while the other says

Installing <gem_name> <gem_version> with native extensions

This is the installation where people use to have problems sometimes. But don't despair!

It happens that some of these "native extensions" are written in C and you need to build them in your machine before using that gem. And most of the times this is happening just because you haven't installed all development packages needed to build C programs.

Once you have all the tools needed to build programs in your computer, everything will be alright. Well... most of the times!

Let's consider two different situations: 1) You are a smart developer and uses Linux or other Unix-like Operational System in your computer; 2) You use any flavor of Windows in your computer because you haven't learned anything better.

1) You are a smart developer and uses Linux or other Unix-like Operational System in your computer

If this is your case, the solution is very simple, indeed.

Just find out in your distribution the package group containing the development tools and install it.

In the Debian box I use mostly, I just have to do

$ sudo apt-get --install-recommends install build-essential

This command will install all build tools needed and after it you'll run your build install perfectly well, if nothing else is wrong with your installation.

If you use Ubuntu, it's just the same. Ubuntu is a Debian-like Linux and most of what applies to Debian will apply to it too.

If you use Slackware Linux and you have installed it FULL, you won't have problems building anything. Slackware contains all the packages needed. If you haven't installed it full, just re-run the installer and install all packages from "D" series. This "D" stands for development and this group contains everything you need.

If you use Fedora, just run

$ sudo dnf groups list

and you'll see a list of groups of packages. Install all those groups for development and you just can't fail. You may install a group doing

$ sudo dnf install group "group_name"

with group_name being the full name of the group desired. Cut and past from the list to be sure.

I use these three Linux distributions mostly, then I'm not sure about yours if it's not among these three. But don't be lazy and do a little research yourself and you'll soon find out how to install your development packages, whatever your distribution is.

2) You use any flavor of Windows in your computer because you haven't learned anything better

The best to do here is moving to Linux and going back to (1) above. If you don't want to perform this radical change at once, try at least install a Linux Virtual Machine in your computer with Oracle VirtualBox and develop using this VM.

The main reason to tell you this is the fact Windows is not made for RoR developers, as I explain in this article here.

But if you insist in using Windows, there we go...

Your best choice is visiting and do everything listed there.

The second best is installing DevKit after having used RubyInstaller.

In both cases you'll have problems. Versioning problems mostly. It happens that the libraries these two solutions offer are seldom updated to the latest versions of Ruby and Rails. Maybe you'll have to make some changes to your code in order to run it, putting aside the newest Ruby resources. This is never a good idea.

Well... here we are. I hope this helps you when having this problem in installing gems with native extensions.

See you!