A problem people less familiar with Ruby often face is how to install Ruby to get started with their work, be it developing a new application with one of the Ruby web development frameworks or running test automation suites written by other people.

This is especially a problem with people starting with test automation or developers mainly using other languages, for example Scala, and the situation is complicated by the fact that there are several ways to accomplish in effect the same result, for example:

  • default system Ruby
  • operating system package on Linux
  • homebrew on Mac
  • RVM
  • rbenv
  • chruby

There are two tasks related to setting up a Ruby environment on a Mac / Linux box:

  • installing Ruby versions
  • switching between Ruby versions

RVM handles both of these tasks while ruby-build can be installed as a plugin with rbenv. The chruby github page has instructions for using either ruby-install or ruby-build to install the rubies.

Each of these methods have their own pros and cons.

Which Ruby version to choose?

System Ruby

Some operating systems have a usable version of Ruby installed by default. The system Ruby is always a version of MRI, i.e. Matz's Ruby Interpreter = original Ruby version and almost never a recent one.

There are new features and performance improvements in the newer versions and it is advisable to install something decent on your system. That is the reason why there are a number of tools to get a proper version installed. The alternative Ruby versions, JRuby/Rubinius/... are not installed on any Linux distribution by default to my knowledge and getting a recent version requires one of the tools listed here.

MRI / JRuby / Rubinius?

This question is two-fold. First, should you use the regular version of Ruby (MRI)
or go with one of the alternative versions with better concurrency mechanisms.

This depends on the project but if you need to concurrency provided by JRuby or Rubinius,
chances are someone very familiar with Ruby has already made the choice for you. If you
want to decide which Ruby to use for your next dotcom startup, you need to do more research than reading this blog post. Having said that, JRuby seems to come on top on many of performance benchmarks and system performance depends on many factors other than language performance.

When running test automation, it doesn't really matter as long as the gems needed are compatible. All of the ones I use regularly are, so I don't believe this to be a big issue. Also, switching between versions is really easy with one of the tools listed here, chruby, rbenv or RVM, so you can change the version later if you find problems. Performance is not an issue with any of them for regular test automation tasks.

MRI has been used in all test automation projects that I have worked in and works well. For web development and running web services I use Rubinius when I can.

Version?

New Ruby versions have been very good in keeping the backwards compatibility. Ideally you should be using the latest version of the Ruby interpreter you are using, which at the time of writing this are the following:

  • MRI - 2.1.2
  • Rubinius - 2.2.10
  • JRuby - 1.7.13

Back to version managers

My personal preference and what I recommend others to use is chruby with ruby-install. It is least intrusive, doesn't change the shell environment too much and makes installing and switching to new Ruby versions very easy.

RVM comes with building gemsets by default which is handy, but much of the gem management is done with bundler and a Gemfile anyway so it doesn't really matter too much.

Installing ruby-install

You need to be able to install rubies before switching between them. One good tool to do that is ruby-install. The instructions to install it can be found on ruby-install github page.

Mac OS X requires the XCode command line tools to be installed to compile sofware.

Installing rubies

A new version of Ruby can be installed with the following commands. The first command installs Rubinius in ~/.rubies directory, the second specifies the installation location to be under /opt/rubies, which in my opinion is a good place install, especially when installing on a server and not a personal workstation:

# Default location
$ ruby-install rbx 2.2.10

# Specific location
$ ruby-install rbx 2.2.10 --install-dir /opt/rubies/rubinius-2.2.10

Installing chruby

The process is described in detail on chruby's github page. Consider enabling automatic switching between Ruby versions, especially if there is a chance that you need to work on things with different rubies.

Once Ruby versions are installed switching between them can be done in two ways:

  • manually
  • automatically by adding a .ruby-version file in the project directory

Manually switching to a specific Ruby version can be done with the chruby command. The version matching is fuzzy. All of the following commands would switch to Rubinius 2.2.10 if that is the only Rubinius version installed.

$ chruby 2.2.10 
$ chruby rubinius
$ chruby rubinius-2.2.10

The same fuzzy logic applies to selecting a Ruby version with the .ruby-version file.

Summary

Installing a Ruby version manager is a good way to get up to date versions of Ruby on your system. It also makes switching between the versions easy should you need to do that.