JRuby, Ruby gem command conflict

I would have to assume that there are plenty of developers out there that want to install JRuby and Ruby on the same machine. There is a tragic flaw with this, the gem command is the same for both. If you add both to the path there is no way to distinguish between the two version, so by default the most recent gem command included on the path will be used. I have come up with a solution that I am happy with to solve this problem so I thought I would share it with the world.

First I am on a Mac, but this should work on any *nix based system. Also I am using bsh, so if you use a different shell make sure to adjust the setting for that shell.

The process is quite simple, download/install Ruby and JRuby. There are numerous tutorials for getting Ruby installed I would recommend the install guide put out by Dan Benjamin at Hive logic. I typically tweak Dan’s tutorials and put my binaries in their own directory. So instead of

/usr/local

I would install the binaries to

/usr/local/ruby/ruby-1.8.4

Then I would make a symlink from /usr/local/ruby/current to the most recent binary.

cd /usr/local/ruby
ln -s /usr/local/ruby/ruby-1.8.4 current

And add /usr/local/ruby/current to my path.

RUBY_PATH="/usr/local/ruby/current"
export RUBY_PATH
PATH="${RUBY_PATH}/bin:${PATH}"
export PATH

I would typically add these lines to my ~/.bash_profile script to be executed whenever my shell starts.

All is fine and dandy until I install JRuby. To install JRuby simply download the latest flavor. JRuby 1.1 was just released so I will use that as my example. Unpack the tar into /usr/local/jruby/jruby-1.1. Now repeat the step above for creating a symlink to /usr/local/jruby/current.

cd /usr/local/jruby
ln -s /usr/local/jruby/jruby-1.1 current

Now here is the first tricky part. Because I want ruby to be my default command line ruby executable I don’t want to add JRuby to the path. I do however, add the JRUBY_HOME environment variable. To do this simply add the following lines to your ~/.bash_profile script.

JRUBY_HOME="/usr/local/jruby/current"
export JRUBY_HOME

Now I need to create a way to add JRuby to the path as I need it. To do this I created a script and placed it in a folder located in my User folder

~/Scripts

I named the script load_jruby but you could name the file whatever you like. In the file I placed the following contents

PATH="${JRUBY_HOME}/bin:${PATH}"
export PATH

The final piece to the equation is called sourcing the script. This basically means you want to run the script in the current process. You need to do this because by default when you execute a script from the command line it spawns a new thread and any environment variables you change would only be affected inside of that script. To source the script you simply prefix the command with a “.”. So instead of

~/Scripts/load_jruby

you would type

. ~/Scripts/load_jruby

Now if you type

jruby --version

You should get output describing the version of JRuby you are running.

Obviously if you would prefer to have JRuby be your default Ruby installation just reverse the instructions. Also this will not affect any defaults inside of IDEs like Aptana, so make sure you set those up for their respective environments.

There you have it, you can now have Ruby and JRuby installed on the same machine and work with them independently from the command line. Whenever you want to work with Ruby open a new terminal and start typing. If you want to work with JRuby open a new terminal and run your script to start rocking the JRuby.

This entry was posted in development, languages, programming and tagged , , , , , , , , , , , . Bookmark the permalink.

2 Responses to JRuby, Ruby gem command conflict

Leave a Reply to Nick Sieger Cancel reply

Your email address will not be published. Required fields are marked *