There are many benefits of using JRuby with Rails, among others: great thread-support via JVM, easy integration with the vast amount of functionality provided by existing Java libraries, streamlined database access and deployment. We have blogged about installing latest Rails with native Ruby and NginX. In the following blog post we will go through the steps needed to install and configure Rails using JRuby instead.

Make sure you have epel Yum repo installed, if you don’t:

$ sudo rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
$ sudo rpm -Uvh http://download.elff.bravenet.com/5/x86_64/elff-release-5-3.noarch.rpm

Please note that the above instructions are for a 64-bit architecture, on a 32-bit architecture:

$ sudo rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm
$ sudo rpm -Uvh http://download.elff.bravenet.com/5/i386/elff-release-5-3.noarch.rpm

Make sure you have working version of JDK 6 (including the compiler), if not install it by running:

$ sudo yum install java-1.6.0-openjdk-devel

Check that installation was successful:

$ java -version
java version "1.6.0_17"
OpenJDK Runtime Environment (IcedTea6 1.7.5) (rhel-1.16.b17.el5-x86_64)
OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode)

Proceed with the installation of JRuby:

$ cd /opt/
$ wget http://jruby.org.s3.amazonaws.com/downloads/1.6.0.RC1/jruby-bin-1.6.0.RC1.tar.gz
$ sudo wget http://jruby.org.s3.amazonaws.com/downloads/1.6.0.RC1/jruby-bin-1.6.0.RC1.tar.gz
$ sudo tar xzvf jruby-bin-1.6.0.RC1.tar.gz 
$ sudo ln -s jruby-1.6.0.RC1 jruby
$ sudo chown -R irakli jruby-1.6.0.RC1
$ sudo chmod -R 775 jruby-1.6.0.RC1

Please make sure to replace “irakli” with your non-root username (the one you will be using for development) in the above sample code.

Next, you need to edit /etc/profile and add /opt/jruby/bin to your $PATH with a line similar to:

pathmunge /opt/jruby/bin

typically it will be around line 45, right before “export PATH …” statement. Logout and log back in, to let Linux re-initialize with the new PATH variable. Then proceed to installing required gems:

$ jruby -S gem install bundler rake rails

Note: you could have just used the familiar “gem install…” if you were sure that there are no other ruby/gem instances on the server, hence no chance of confusing system about what “gem” stands for, but generally it’s safer to use “jruby -S” syntax instead.

Let’s create a default Rails app and run it:

$ jruby -S rails new yournewappname --template http://jruby.org
$ jruby -S bundle install
$ jruby -S rake db:create:all
$ jruby script/rails server

As you can see we ran the rails app with an embedded server. When you are deploying your app in production, you, obviously should not do it. Use Warbler to package your rails app as a JEE “war” archive and deploy it into a servlet container such as Tomcat, GlassFish or Jetty:

$ jruby -S gem install warbler
$ jruby -S warble