In previous posts I've covered using multiruby to switch between Ruby 1.8 & 1.9 and how to add JRuby to your multiruby environment. This post describes using multiruby as a way to install and experiment with Rubinius in Mac OS X.

In this post I assume you have git and a current version of multiruby, which is part of the popular ZenTest gem. If you do have it but your revision is older than 3.11.1 then you should update it. If ZenTest is not installed then see Switching Between Ruby 1.8 & 1.9 with multiruby for information about getting started with multiruby.

Installing Rubinius in ~/.multiruby

If multiruby is ready to go, then the place to start is cloning Rubinius to the multiruby vendor directory:

$ cd ~/.multiruby/versions
$ git clone git://github.com/evanphx/rubinius.git 

Rubinius support is built-in to multiruby but before letting multiruby do the build take a look at the ~/.multiruby/versions/rubinius/doc/getting_start.txt to see if you're missing any required software. In my case I was missing the ParseTree gem:

$ sudo gem install ParseTree

After getting any known missing prerequisites, the next step is to execute multiruby which will see rubinius in its versions directory and automatically try to build it:

$ multiruby -e "p 1+1"
building and installing rubinius
Running command: rake &> log.build

If you get an error check the log.build file in ~/.multiruby/build/rubinius. The most common problem will be a missing prerequisite, something that should be obvious in the log. If you don't get any errors then after a fairly lengthy build you should see a successful execution:

...
VERSION = rubinius
CMD     = ~/.multiruby/install/rubinius/bin/rbx -e p 1+1

2

RESULT = 0
...

TOTAL RESULT = 0 failures out of 5

Passed: 1.8.6-p287, jruby-1.1.6, 1.9.1-preview2, rubinius, 1.8.7-p72
Failed: 

That's it, if executing Rubinius via multiruby is all you want to do then you're done. If you want to execute Rubinius independently of multiruby read on about adding it to your PATH.

Running Rubinious Outside of multiruby

Rubinius runs Ruby code with its own command name (rbx). To make the multiruby version of rbx available you need to add the rubinius/bin directory to the PATH in your login initialization file, i.e. your .bash_login file:

22
23
24
25
# manage ruby verisions
export PATH=$PATH~/.multiruby/install/rubinius/bin
export ORIGPATH=$PATH
...

I would recommend adding it to the end of your PATH variable, or at least past your existing ruby command path just in case you ever decide to add the ruby symlink that I'll discuss next.

Rubinius as Ruby via Alias Switching

In Switching Between Ruby 1.8 & 1.9 with multiruby I created aliases to set the path in the .bash_login to select which version of Ruby gets used:

22
23
24
25
26
27
28
# switch between ruby verisions
export ORIGPATH=$PATH
alias mr186p287='export PATH=~/.multiruby/install/1.8.6-p287/bin:$ORIGPATH'
alias mr187p72='export PATH=~/.multiruby/install/1.8.7-p72/bin:$ORIGPATH'
alias mr191pre2='export PATH=~/.multiruby/install/1.9.1-preview2/bin:$ORIGPATH'
alias mrjruby='export PATH=~/.multiruby/install/jruby-1.1.6/bin:$ORIGPATH'
alias mroff='export PATH=$ORIGPATH'

Since multiruby recognizes the rbx command you can use Rubinius anytime as long as you added it to your path and setting up an alias to switch over to Rubinius as your default Ruby implementation isn't required. However, if you have applications that launch ruby or you just want you to be able to execute Rubinius with the ruby command then you can still use the same alias technique I described in my previous post. First, create a ruby symlink with:

$ ln -fs ~/.multiruby/install/rubinius/bin/rbx \
> ~/.multiruby/install/rubinius/bin/ruby

Second, setup a new alias in your .bash_login with something like:

28
alias mrrub='export PATH=~/.multiruby/install/runius/bin:$ORIGPATH'

Now, after you reopen your terminal session, $ mrrub will use Multiruby whenever you execute the ruby command. You can switch back to your default Ruby implementation with $ mroff.

Again, since Rubinius executes via rbx and multiruby is aware of that, so symlinking and aliasing is totally optional. With JRuby the symlink was required for multiruby compatibility but the alias was optional. When I first setup JRuby I didn't bother with an alias but decided to add it later when I wanted to test scripts that launch ruby as a subprocess, i.e. `ruby -e .... This time I created the symlink and alias right from the start.

Sorry, comments are closed for this article.