Skip to main content

Installing JRuby for Rails

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:
  1. 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.)
  2. A Git-hub and a Heroku account set up with SSH keys. (See the TheRubyists.org for info).
  3. A text editor (or IDE) of choice. 
  4. 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 --template http://jruby.org

The Gemfile on the rails project will now look something like this:
#gem 'sqlite3'
gem 'activerecord-jdbcsqlite3-adapter'
Instead of this:
gem 'sqlite3'


Optional:

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).


for f in $JRUBY_HOME/bin/*; do
      f=$(basename $f)
      case $f in jruby*|jirb*|*.bat|*.rb|_*) continue ;; esac
      eval "alias j$f='jruby -S $f'"
    done
.

Comments

  1. very informative blog and useful article thank you for sharing with us , keep posting Ruby on Rails Online Course

    ReplyDelete

Post a Comment

Popular posts from this blog

Building my own home.

I've decided. I want to build my own home. There is something special about building your own things. I built a desk for my tiny room when I first moved to L.A. My room was so small that I had to sit on the bed to use the computer so I build a high desk so I could sit on the bed and work on the computer. My roommate Trentity helped me cut the ply-wood to the right side. I still have that desk. It now sits on the living room covered by a cloth hiding the surplus of costume parts my current roommate Sean uses in his creations. Learning to build and fix things continue. And the feeling of satisfaction from fixing even small things is great. So a few years ago I heard on the NPR program the Story about a couple of educators that moved to a tent in their back-yard so they could rent their house and afford to send their kids to college. They had a special type of tent called a yurt and cooked and showered in an RV they had parked next to it. I thought I could do that. Housing in Lo

Contrasting Styles of Writing: English vs. Spanish

There is interestingly enough a big difference between what's considered good writing in Spanish and English . V.S. Naipul winner of the 2001 Nobel prize for literature publish an article on writing . In it he emphasizes the use of short clear sentences and encourages the lack of adjectives and adverbs. Essentially he pushes the writer to abandon florid language and master spartan communication . This is a desired feature of English prose , where short clipped sentences are the norm and seamlessly flow into a paragraph. In English prose the paragraph is the unit the writer cares about the most. This is not the case in Spanish where whole short stories (I'm thinking this was Gabriel Garcia Marquez but maybe it was Cortázar) are written in one sentence. Something so difficult to do in English that the expert translator could best manage to encapsulate the tale in two sentences. The florid language is what is considered good writing in Spanish but unfortunately this has lead t

My Fake Resume

Inspired by the over aggrandized bio of Joseph Rakofsky I want to write my own. If you don't know who he is; Joseph Rakofsky is a lawyer who earned a mistrial for a criminal client due to his (alleged) incompetence as reported on the Washington Post . There has been quite a few commentaries on his "Streisand-house" approach of suing all the bloggers and even the Washington Post and American Bar Association for reporting his (alleged) ineptitude. ("Streisand-house" is what happened to Barbara Streisand who wanted to have a picture of her mansion removed from the internet and she sued to have it removed. Unfortunately suing requires the filing of public documents with a picture of her house. The lawsuit had the direct opposite effect it intended. Everybody now could see legally, since it was a public document, a picture of her house.) But all that internet gossip aside I'm most impressed by his resume. Here is a quote from the website: Prior to stud