JRuby is Ruby that runs on the Java Virtual Machine (JVM). It has the advantage of being cross platform (Linux, Mac and Windows) and being able to run on a 64-bit machine (important if you have a Windows 64-bit machine since the normal Ruby is 32-bit only for now on Windows). It at present installs both Ruby 1.8.7 and 1.9.2 but defaults to 1.8.7, but in the future the default will change to 1.9. The Windows installer meshes really well with the operating system and aside for the first time start-up time (it loads Java first) it's equally fast. However to run Rails on JRuby some things need to be taken care of. This set up should have you in good shape for Michael Hartl's book: Ruby on Rails 3 Tutorial.
1. Preliminary things you need.
JRuby on Rails is a mostly platform agnostic installation. It can be installed on any OS that runs the JVM or Java as it is commonly known. You probably already have it on your machine. But in case you don’t, you need it to install JRuby. Verify that you have java on your computer by running this command:
$ java –version
Which should spit out something like this:
>java version "1.6.0_26"
JRuby is slightly different from normal Ruby (or C-Ruby) the differences are listed on this site: MRI-JRuby Differences.
Additionally for JRuby on Rails there are a few gems (essentially plugins for Ruby) that don’t work with Java so they use Java alternatives, these are listed below and how to alter your Rails project to use these is also explained.
2. Getting all you need.
Go to jruby.org and download the installer for Windows or Mac and install it.
For Linux you will probably find it in your distribution repositories, or may need to use an alternate method. I have only used the repository to install JRuby on Ubuntu so if you have something different, you’ll have to research that. In Linux be aware that you need Java not OpenJava.
Verify that you have JRuby running with this command:
$ jruby -v
Which should output this or something like it:
>jruby 1.6.4 (ruby-1.8.7-p330)
If not verify that your PATH is set correctly (instructions on the jruby.org website) and try again.
For the Rails 3 Tutorial you will also need:
- Git (Download Git) Follow the instructions on ProGit to install. (On Windows make sure you select the third option to allow Git to be accessed from the regular command line.)
- A Git-hub and a Heroku account set up with SSH keys. (See the TheRubyists.org for info).
- A text editor (or IDE) of choice.
- If on Windows, I highly recommend you get Console2. Which is portable and you can drop into your rails project folder to have quick access to that directory. Particularly useful if you need admin privileges.
3. Setting up JRuby for Rails
With the Git bash or at the bash terminal type the following command:
jgem install rails –v=3.0.10
jgem install bundler heroku activerecord-jdbcsqlite3-adapter taps-jruby jruby-openssl spoon annotate
Depending on where you installed JRuby you may need to run the command prefaced with ‘sudo’ or ‘run as administrator’ on Windows.
**Note: on Windows you may need a sqlite3.dll file to be dropped on the bin folder inside the JRuby directory. To get the sqlite3.dll follow instructions on TheRubyists.org.
4. Setting up Rails to work under JRuby.
The sqlite3, openssl and taps gems need java version adapters to work under JRuby. You will notice that we installed a jruby-openssl not openssl. Now every Rails program that is created comes with a Gemfile file that outlines the gems needed for it to run. If we create a rails program normally it will automatically default to the normal Ruby gems that don’t work with JRuby so when creating a new rails project we need to add some info to get the Gemfile configured for JRuby out of the bag. Otherwise you’d have to change it on the Gemfile manually.
To access a gem like rails on JRuby we preface the command with “jruby -S” then the command as normal. If you only have JRuby installed you may be able to dispense with this but it’s good practice to put it. So to install a gem you’d write
jruby –S gem install
A shortcut exists for gem we used above called jgem, but for rails we have to use the whole thing.
Additionally by adding a template we will get rails to configure the Gemfile for JRuby from the get-go.
To create a new Rails Project the command should look like this:
jruby -S rails new
#Note Newest JRuby doesn't need this but for old installations use:#jruby –S rails new
The Gemfile on the rails project will now look something like this:
Instead of this:
Install the following gems for testing: (this list may be incomplete as I'm not using testing)
jgem install rspec cucumber autotest-growl spork autotest-java autotest-standalone autotest-rails-pure
You can add the following code to your .bashrc file (instructions to follow) to get "j" versions of commands like jrails. (From this website).