Saturday 3 February 2007

Using Rails with JRuby

In this post I'm going to discuss how to get a rails application running using JRuby. It's not actually not that difficult to just get it running. (The real difficulties arrive when you get to databases and other areas of rails that arent fully covered by JRuby) I'm only going to be launching the application using the WEBrick server at script/server. If you were putting this into production mode you would want to use one of the many many deployment solutions like using glassfish, tomcat and stuff like that. I'm not going to get into that stuff in this post. WEBrick is good enough for development work and as a first step.

If you don't already have JRuby installed, have a look at my previous post.

If you've just installed JRuby and stroll into a rails application and type
# jruby script/server
You will get this error
Cannot find gem for Rails ~>1.2.1.0:
Install the missing gem with 'gem install -v=1.2.1 rails', or
change environment.rb to define RAILS_GEM_VERSION with your desired version.
This is what I did and I was stumped by it at first because I already had version 1.2.1 or rails installed on my system. What was wrong with JRuby? Why didn't it realise this?

I then discovered that actually JRuby has its own gem system. You need to install rails as a JRuby gem.

Check out http://www.headius.com/jrubywiki/index.php/JRuby_on_Rails for more detail but basically you use

# $JRUBY_HOME/bin/gem install rails -y --no-ri --no-rdoc

to install rails as a gem for JRuby. This should produce this (assuming you have JRUBY_HOME defined):
Bulk updating Gem source index for: http://gems.rubyforge.org
Successfully installed rails-1.2.1
Successfully installed rake-0.7.1
Successfully installed activesupport-1.4.0
Successfully installed activerecord-1.15.1
Successfully installed actionpack-1.13.1
Successfully installed actionmailer-1.3.1
Successfully installed actionwebservice-1.2.1
Great!

Now go into a rails app:
$ cd ~/webprojects/mygreatnewrailsapp.com
$ jruby script/server
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2007-02-03 13:48:03] INFO WEBrick 1.3.1
[2007-02-03 13:48:03] INFO ruby 1.8.5 (0) [java]
[2007-02-03 13:48:03] INFO WEBrick::HTTPServer#start: pid=29324282 port=3000

Woohoo!
Go http://localhost:3000/ in your browser and if its a brand new application you should see the "Welcome to Rails page"

WEBrick is OK, but what about Mongrel?
While Mongrel is mostly Ruby, but it also contains some C (for speed reasons). JRuby can't do anything with C so for now Mongrel is out (More about that here.). What is necessary is the conversion of the C components in Mongrel to Java but as far as I know no-one has done that yet.

Installing JRuby on Ubuntu

This post is going to detail the steps I had to take in installing JRuby on my Ubuntu system. Note: these steps would probably work just fine on most other Linux systems but I haven't tried it so I cant say for sure.

You will probably need to install all this as root.

Prerequisites

The first thing you need is Java. You need the Java JRE and JDK (at least version 1.4 according to this http://svn.codehaus.org/jruby/trunk/jruby/README). I'm not going to go through the details so Ill just point you towards some tutorials
https://jdk-distros.dev.java.net/ubuntu.html - the easy way
http://java.sun.com/javase/downloads/index.jsp - the less easy way

To verify you have Java, just check the version. You should get something like this:
# java -version
java version "1.5.0_08"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_08-b03)
Java HotSpot(TM) Client VM (build 1.5.0_08-b03, mixed mode, sharing)
Next thing to install is Ant, its a prerequisite for building JRuby.
# apt-get install ant
Verify you have ant:
# ant -version
Apache Ant version 1.6.5 compiled on July 5 2006
Building JRuby

If you have Subversion you can just go
# svn co http://svn.codehaus.org/jruby/trunk/jruby
If you dont have Subversion you can download it here: http://dist.codehaus.org/jruby/ and extract it using:
# tar -xzvf jruby-src-LATEST.VERSION.tar.gz
(At this point it might be a good idea to move the jruby folder to somewhere like /usr/local/lib just to make it more globally available and less specific to any particular user. This location, where ever you decide, will be called JRUBY_HOME)

Then:
# cd ./jruby
# ant clean jar
You should get output like this:
Buildfile: build.xml

init:

clean:
[delete] Deleting directory /usr/local/lib/jruby/build
[delete] Deleting 2 files from /usr/local/lib/jruby/lib
[delete] Deleting directory /usr/local/lib/jruby/docs/api

init:

prepare:
[mkdir] Created dir: /usr/local/lib/jruby/build
[mkdir] Created dir: /usr/local/lib/jruby/build/classes
[mkdir] Created dir: /usr/local/lib/jruby/build/classes/jruby

blahblahblabh java ant creation stuff filler text blahblahblah

jar-jruby:
[jar] Building jar: /usr/local/lib/jruby/lib/jruby.jar

jar-openssl:
[jar] Building jar: /usr/local/lib/jruby/lib/jruby-openssl.jar

jar:

BUILD SUCCESSFUL
Total time: 11 seconds
If you didn't get a Build Successful, you've got problems, loser! Woah, thats probably a bit harsh. But seriously, you're on your own.

Ok, great you've built JRuby but you can't run it from the commandline yet!

Add JRuby to the path
# jruby
bash: jruby: command not found

Duh-oh! You need to add jruby to your path. I'm going to assume you use Bash, but it should be similar enough for whatever shell you use.

# vi ~your_username/.bashrc

Add this:

JRUBY_HOME=/usr/local/lib/jruby # or where ever you put it.
PATH=$PATH:$JRUBY_HOME/bin
to the end of the file.

Now reload your bash profile
# source ~yourusername/.bashrc

(Note to add JRUBY_HOME to the global path, add
JRUBY_HOME=/usr/local/lib/jruby   # or where ever you put it.
PATH=$PATH:$JRUBY_HOME/bin
to /root/.profile and (/root/.bash_profile or /root/.bashrc)
http://www.troubleshooters.com/linux/prepostpath.htm )

Now try this:
# jruby -v
ruby 1.8.5 (0) [java]
As Millhouse says: Radical!

Well thats about it. JRuby is now installed and you can use it as a ruby interpreter in the same way that you would use the official ruby interpreter.
# jirb
irb(main):002:0> puts "moo!"
moo!
=> nil
irb(main):003:0>