Compiling Ruby from source for your development environment

For most of us, downloading the development package of Ruby for your platform will suffice. For the curious, or those needing a Ruby version that doesn’t have a pre-built package available you have to resort to compiling ruby from source code. Fortunately, as we have built ourselves a clean development environment using Vagrant, this is actually a pretty simple task!

Advertisement for a kit home

Whilst most of us are fine with buying an existing home, some of us want to build our own; and so it is with Ruby

Getting started

For this example we are just going to build a machine and install Ruby, and not worry about installing a Rails app just like we did for our basic example. To get started, checkout the source code from github:

git clone -b building_ruby_from_source --single-branch

Getting the source code for compiling Ruby

So, the first step is to figure out the url for grabbing the source code for the version you need. In the example code, we’re using the latest stable version of Ruby: 2.1.2, but that maybe different by the time you read this. Best thing to do is go check out the Ruby ftp site and find the tarball you need. Our package can be found at Now check out the code in

  rm -rf /opt/vagrant_ruby
  echo 'downloading'
  curl --remote-name >/dev/null 2>&1
  tar zxf ruby-2.1.2.tar.gz >/dev/null 2>&1
  cd ruby-2.1.2/
  echo 'configure'
  ./configure >/dev/null 2>&1
  echo 'make (this could take a while, while I make ruby, you should make tea...)'
  make  >/dev/null 2>&1
  echo 'install'
  make install >/dev/null 2>&1

If you’re going to be compiling a different version of Ruby, then go ahead and change the url in line 13, along with the commands on line 14 and 15. Let’s understand what this is doing step-by-step:

  1. rm -rf /opt/vagrant_ruby removes the Ruby installation that comes with the base Vagrant box
  2. curl --remote-name downloads the ruby package with the same name as on the ftp site
  3. tar zxf ruby-2.1.2.tar.gz unzip the file and jump into the created directory before…
  4. Compiling the code using ./configure, make and finally make install

Compilation can take a good while, but trust that your box will get there eventually. The last step after compiling Ruby is to gem install bundler. We could have done this through Puppet, but as we need to have Ruby installed first, it is simpler just to install it here.

Idempotence, and saving time

In my post on setting up a base rails environment I talk about the need for Idempotence; that is, ensuring your scripts do no harm if they are run multiple times. Strictly speaking, the code above for compiling Ruby can be run multiple times without harm, and is idempotent. However, the build process takes so long that it’s really worth doing a check here and only installing if actually necessary. You see that in action in the following code:

if [ "$(ruby -e 'print RUBY_VERSION')" = '2.1.2' ]
  echo "ruby already installed, skipping"
  echo "Installing ruby 2.1.2"

This does a check against the current installed Ruby version, and skips installation if we can.

This entry was posted in Engineering, Rails, Ruby, Software Craftsmanship. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.