In Switching Between Ruby 1.8 & 1.9 with multiruby I covered setting up and using Ruby 1.8 and 1.9 versions in multiruby. This post describes adding JRuby to the list of multiruby versions in Mac OS X.

Install JRuby in ~/.multiruby

multiruby doesn't have built-in support for JRuby but it works by tracking its installations in the ~/.multiruby/versions and ~/.multiruby/install directories. Setting up the directory structure to look like a MRI build from a tarball does most of what's necessary:

$ curl -O http://dist.codehaus.org/jruby/1.1.6/jruby-bin-1.1.6.tar.gz
$ tar -C ~/.multiruby/install -zxf jruby-bin-1.1.6.tar.gz
$ mv jruby-bin-1.1.6.tar.gz ~/.multiruby/versions/jruby-1.1.6.tar.gz

That will provide access to the jruby command but multiruby only knows about using ruby so we'll need to fool it with a symbolic link:

$ ln -fs ~/.multiruby/install/jruby-1.1.6/bin/jruby \
> ~/.multiruby/install/jruby-1.1.6/bin/ruby

Now verify that it works:

$ multiruby -e "p 1+1"

Although that installation was from a binary distribution it wouldn't be much more difficult to do it from source. You just need to make sure the base names in the versions and install directories match when you're done.

Adding JRuby to the PATH

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

22
23
24
25
26
27
# 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 mroff='export PATH=$ORIGPATH'

If we wanted to execute JRuby with the ruby command then we could add an alias for JRuby too but since JRuby has its own command names (jruby, jgem,jirb) we can allow them to always be available if we just make sure that the ruby symlink we created doesn't come before any real ruby commands by adding it to our path:

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

BTW: If you want an alias that you can execute to make the ruby command executes JRuby:

25
alias mrjruby='export PATH=~/.multiruby/install/jruby-1.1.6/bin:$ORIGPATH'

then you'll probably also want to setup symlinks for gem -> jgem and irb -> jirb. That setup could be done instead of or in addition to the PATH change on line 23 above. I didn't bother with the alias or the additional symlinks; to me using the JRuby commands in their native jXXX format seems like the way to go.

That's it, JRuby is now a first class citizen in multiruby and multiruby_setup and jruby, jgem, and jirb are all available.

Sorry, comments are closed for this article.