Getting started with JRuby on Rails on OS X

I’ve been an independent web developer for over a month now, and I am enjoying every moment of it! If the first step in the liberation of programmers is getting Java- or .NET-free, the second step must be to get employer-free. Of course, being an employee has many benefits and doesn’t hold much risk (especially not if you live in one of these countries where it is practically impossible to fire people, such as Sweden or France), and being independent doesn’t mean – at least not yet for me – that you don’t have to do boring or tedious work.

But it does mean that any time during work hours where I am not doing work for a customer is my own time, and on my own time I get to decide what to work on. I am building a product during these free hours, and while I’d like to hold the details a bit longer (“don’t tell – show!” someone smart has said), I can say that it is a Ruby on Rails application allowing manipulation of PDF documents. Ruby has some decent PDF libraries, and Prawn holds a great potential, but none of these allow me to manipulate existing PDF documents.

The Java library iText is capable of doing so, and so is the .NET port iTextSharp, but would I really have to build some kind of web service in one of these forsaken frameworks? I was complaining about it to a friend, and promptly asked me: Why don’t you build it in JRuby? The more I thought about it, the more this seemed to be the perfect excuse for trying out JRuby, and so I set out to build my first ever JRuby on Rails application. It also helped that I got a free OpenSolaris/Glassfish VPS from Sun and Layered Technologies.

Now, this long introduction was meant to disguise the fact that it is really very easy to get started on some JRuby on Rails development on Mac OS X. All I did was to follow this great installation guide. I do however have a few corrections and recommendations of my own, which I recommend you read before you go on to Robert Dempseys blog.

First, the guide doesn’t tell you where to install JRuby, and that’s because you can install anywhere you want. I chose to put it in /Library/Java/, because it makes sense to me to place it with the other Java stuff. Also, make sure you download the newest JRuby version from their download page – that’s not the first in the list. Second, the guide assumes that you have Ant – a tool for building Java apps – installed, but I bet you haven’t. To do this, you can either follow this guide, or you can simply download Ant and put it in /Library/Java/. Remember to put JAVA_HOME and ANT_HOME in your .bash_profile as Robert’s guide says.

If you need to work with ordinary Ruby applications alongside your JRuby apps, I wouldn’t recommend putting the JRuby lib in your default path. While it is easy to distinguish between the ruby and jruby commands, JRuby also have its own version of other well-known tools such as gem and rake, and since JRuby for instance keeps its own gem repository, you can end up being quite confused as to what gems you are seeing. My solution is not the most elegant, but it works for me. I have created a file with the contents below in my home directory, and when I want to work with JRuby, I simply have to write source ./~.jruby in my console:

  export PATH="/Library/Java/jruby-1.1.4/bin/:$PATH"

There is one part of Robert’s guide that is slightly outdated. The name and structure of the ActiveRecord gems that allow using MySQL through JDBC has changed. Instead of installing the now non-existing activerecord-jdbc gem, you should install activerecord-jdbc-adapter and activerecord-jdbcmysql-adapter:

  sudo gem install activerecord-jdbc-adapter activerecord-jdbcmysql-adapter

This article explains the various JDBC adapters available in depth.

Also, I didn’t need to add the lines in environment.rb starting with if RUBY_PLATFORM =~ /java/. With the gems installed and the MySQL jar in place, it worked out of the box for me.

By now you should hopefully be able to build and run your JRuby application. But what you want to debug? I like to use ruby-debug in my Rails applications, because it allows me to break anywhere in the code by adding a debugger statement. For JRuby, the official ruby-debug gem won’t work because it relies on compiled C code, but luckily a JRuby compatible verison has been released as described in the JRuby wiki. With this installed, you simply add the -d when you call jruby, and the code will break at any debugger like we are used to.

Next up is interacting with Java libraries and deploying JRuby on Rails applications, but that’ll have to be in separate articles …

Leave a Reply

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


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>