How to install JRuby on Rails on Mac and Linux

It is simpler than you might think to install JRuby on Rails on your Mac or Linux box. Note: Mac comes with Java and Apache Ant pre-installed, thank god.  Linux users need to make sure Java and Ant are installed before attempting to install JRuby on Rails.

Just follow these simple steps and you’ll be running JRuby on Rails on Mac or Linux in no time.

I’m going to explain the steps first, then show the code last…

1. Download the latest JRuby source code tarball or zip from http://dist.codehaus.org/jruby/, then extract it and put the JRuby directory ANYWHERE YOU WANT on your Mac.

2. Run the “ant” command from within the extracted JRuby source directory.  This will generate the necessary jruby.jar file and other configuration settings to run JRuby on your computer.

3. Add a $JRUBY_HOME environment variable to your shell, then add $JRUBY_HOME/bin to your $PATH environment variable.  (NOTE: Some users may need to refresh their environment by typing “source ~/.bash_profile”, but I edit my environment variables directly in /etc/bashrc, so all I need to do is close and re-open my Terminal for my environment variables to refresh.)

4. Install Rails as a gem using JRuby. You can now create JRuby on Rails applications, but don’t try to run them until you have a database installed for use with your JRuby on Rails application…

5. Install SQLite3-Ruby as a gem using JRuby. This Gem is a pure Ruby package, so it will work with JRuby as well as regular Ruby (”CRuby”).

Once SQLite3 is installed, you can create JRuby on Rails applications by entering the command “jruby -S rails myapp”.  You can run this application by entering the command “jruby script/server”.  The built-in WEBrick server should boot up your JRuby on Rails application on port 3000, while Rails automagically creates a SQLite3 database for the myapp application under the “db” folder - cool.

6. Install MySQL JDBC adapter as a gem using JRuby. This JRuby gem is rightfully called “activerecord-jdbcmysql-adapter”, and links Java, MySQL, and (Rails) ActiveRecord.  This gem will install two other gems as dependencies: “jdbc-mysql” and “activerecord-jdbc-adapter”.

Prerequisite:
This requires that you have MySQL 5+ installed already.  This is just the MySQL Java adapter for JRuby, it is not MySQL.  To get MySQL 5+ on your computer, download Sun’s MySQL 5.0 for free and install it.

History:
This Java database adapter gem has a C-binding counterpart gem in the CRuby on Rails world, un-rightfully named “mysql”, which probably should’ve been called “activerecord-mysql-c-bindings” or something closer to the truth.  And with the JDBC adapter gem you don’t have to specify where to look for your MySQL library, hehe.  Remember the dreaded “sudo gem install mysql — –with-mysql-lib=/usr/local/mysql/lib” command?  Yikes.  In this instance, I thank the simpler Java approach of hooking up MySQL to my JRuby on Rails applications.

Rails developers:
Remember to update your config/database.yml file to use this “jdbcmysql” adapter and not the default CRuby “mysql” adapter. More info on Java DataBase Connectivity can be found here.

Here are the commands:

cd /Users/me/Downloads
wget http://dist.codehaus.org/jruby/jruby-src-1.1.4.tar.gz
tar -xzvf  jruby-src-1.1.4.tar.gz

cd jruby-1.1.4
ant
#wait for this to complete successfully :-)

sudo nano /etc/bashrc
export JRUBY_HOME=/Users/me/Downloads/jruby-1.1.4
export PATH=$JRUBY_HOME/bin:$PATH
#remember to refresh your environment source by closing and re-opening Terminal or running “source ~/.bash_profile”)

jruby -v
> jruby 1.1.4 (ruby 1.8.6 patchlevel 114) (2008-09-05 rev 6586) [i386-java]
which jruby
> /Users/me/Downloads/jruby-1.1.4/bin/jruby

jruby -S gem install –no-rdoc –no-ri rails
which rails
/Users/me/Downloads/jruby-1.1.4/bin/rails

# install these essential gems too
jruby -S gem install –no-rdoc –no-ri jruby-openssl
jruby -S gem install –no-rdoc –no-ri activerecord-jdbcmysql-adapter

# optional cool pure-ruby gems
jruby -S gem install –no-rdoc –no-ri warble
jruby -S gem install –no-rdoc –no-ri ruboss4ruby

Now we can generate our JRuby on Rails applications:

jruby -S rails -d mysql testapp

# OR, if you know rails is available to jruby, simply:
rails -d mysql testapp

# OR, using the default sqlite3 database:
jruby -S rails testapp

At this point, you should be able to run your on the built-in development server called WEBrick:

jruby script/server -p 3000

Done!

Where to now?
For future discussion: Java Enterprise Production Environment…

7. To serve your JRuby on Rails applications in a production environment, you’ll need something more powerful than WEBrick.  Two great options come to mind: Tomcat and Glassfish (forget about Mongrels and Mongrel_jClusters, welcome to easy java enterprise server world).  On this blog post, I am going to recommend Apache Tomcat 6.0.x over Glassfish Application Server v3 Prelude because of many different reasons - mostly because Glassfish has many bugs and is far slower than Tomcat, and things seem to run a lot better with Tomcat.  So until Glassfish v3 is truly released as a competitor to Tomcat, and the Glassfish Gem is closer to it’s Application Server parent, I will recommend Tomcat over Glassfish.  However, I always recommend doing your own tests, so why not download both Tomcat 6.0 and Glassfish v3 and deploy side-by-side.  You may like the built-in Rails deployment feature of Glassfish, or you may prefer the warble war deployment to Tomcat, only you can tell.

Comments are closed.