Test driving JRuby 1.6

Posted by – April 7, 2011

I’ve been hacking away at ruby on rails for some time now, but I’ve never stopped to try out JRuby. That’s probably because I initially fell in love with rails after an eight-year stint in the java insane asylum. However, with the release of JRuby 1.6, I decided it was finally time to forgive Java, and see how hard it’d be to run our fairly mature ruby (1.9.2) on rails codebase with JRuby.

I already used RVM, so I used that to install JRuby 1.6:

$ rvm install jruby-1.6.0

Two minutes later, JRuby was installed. Easy enough. Let’s use it:

$ rvm jruby
$ which ruby

OK, we’re in business. I’m still not sure how different JRuby is, so let’s see what gems I have to start with:

$ gem list

*** LOCAL GEMS ***

bouncy-castle-java (1.5.0145.2)
jruby-launcher (1.0.7 java)
jruby-openssl (0.7.3)
rake (0.8.7)
sources (0.0.1)

Let’s get bunder installed:

$ sudo gem install bundler

And now, before I switch to my project and try a bundle install, let’s just see if I can easily install the rails gem:

$ sudo gem install rails
Successfully installed rails-3.0.6
23 gems installed

Easy! Alright, enough of this tip-toeing around, let’s see what happens when I switch to my project and try a bundle install:

$ bundle install
Fetching git://github.com/nhowell/subdomain-fu.git
Fetching git://github.com/flori/json.git
Fetching git://github.com/adamrubin/acts-as-taggable-on.git
Fetching source index for http://rubygems.org/
Using rake (0.8.7) 
Using abstract (1.0.0) 
Using activesupport (3.0.6) 
Using builder (2.1.2) 
Installing mysql2 (0.2.6) with native extensions Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

Ugh. An error with the mysql gem. Is this the beginning of the end? Well, maybe not. A few minutes of googling and I discovered that JRuby needs a different gem for mysql. And there’s an easy way to conditionalize my Gemfile:

if defined?(JRUBY_VERSION)
  gem 'jdbc-mysql'
    gem 'activerecord-jdbc-adapter', :require => false
    gem 'rmagick4j'
    gem 'mysql2'
    gem 'rmagick'

(I ran into the similar problem with rmagick)

After making that change and running bundle install again, there were no issues reported. Hrm. This is too easy. Let’s see what happens when I try to start the server:

$ rails s
JRuby limited openssl loaded. http://jruby.org/openssl
gem install jruby-openssl for full support.
=> Booting WEBrick
=> Rails 3.0.6 application starting in development on
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2011-04-07 20:39:37] INFO  WEBrick 1.3.1
[2011-04-07 20:39:37] INFO  ruby 1.8.7 (2011-03-15) 
[2011-04-07 20:39:37] INFO  WEBrick::HTTPServer#start: pid=50128 port=3000

The server started up fine. I opened the browser and did a smoke test of the app and couldn’t find a single problem. HOLY CRAP that was easy! JRuby is an option. I’ll sleep a little better tonight knowing that.

  • Hooligan495

    I like jruby. It's also useful if you need to build ruby scripts for folks. You can package the whole kit and kaboodle into a single executable, and give it to folks this way they don't have to officially install ruby. (yes they need java already installed)