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!
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 https://github.com/eyefodder/spex.git
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
http://ftp.ruby-lang.org/pub/ruby/2.1/ruby-2.1.2.tar.gz. Now check out the code in
rm -rf /opt/vagrant_ruby echo 'downloading' curl --remote-name http://ftp.ruby-lang.org/pub/ruby/2.1/ruby-2.1.2.tar.gz >/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:
rm -rf /opt/vagrant_rubyremoves the Ruby installation that comes with the base Vagrant box
curl --remote-name http://ftp.ruby-lang.org/...downloads the ruby package with the same name as on the ftp site
tar zxf ruby-2.1.2.tar.gzunzip the file and jump into the created directory before…
- Compiling the code using
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' ] then echo "ruby already installed, skipping" else echo "Installing ruby 2.1.2"
This does a check against the current installed Ruby version, and skips installation if we can.