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

Popular posts from this blog

How to configure Ubuntu's keyboard to work like a Mac's

Typing accents on a PC is a complicated Alt + three numbered code affair. One feels like a sorcerer casting a spell. "I summon thee accented é! I press the weird magical key Alt, and with 0191 get the flipped question mark!" For a bilingual person this meant that writing on the computer was a start-and-stop process. With Mac's it a whole lot easier, just Alt + e and the letter you wanted for accents and alt + ? for the question mark. No need to leave the keyboard for the number pad and no need to remember arcane number combinations or have a paper cheat sheet next to the keyboard, as I've seen in virtually every secretaries computer in Puerto Rico.

Linux has a interesting approach to foreign language characters: using a compose key. You hit this key which I typically map to Caps Lock and ' and the letter you want and voilá you get the accent. Kinda makes sense: single quotation mark is an accent, double gets you the ümalaut, works pretty well. Except for the ñ, wh…

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 to what …

Fixing Autocomplete in Github's Atom Text Editor for Ruby

I really like Github's Atom Text Editor. I really like that it's multi-platform allowing me to master one set of skills that is transferable to all platforms and all machines. 

On thing that just burns me of the default set-up in Atom is the Autocomplete feature that seems to change my words as a type them. Because Ruby uses the end of line as a terminus for a statement you usually finish a word with pressing the return button and you get really annoying changes to your finished typed word a la MS Word. I find myself yelling "No that's not what I wrote!" at the screen in busy coffee shops.

I disabled autocomplete for a while but it is a very useful function. Then I found out they changed the package that gave the autocomplete to a new one called "Autocomplete Plus" that gives you more options. All that I needed to change to make autocomplete sane again:

1. Open Atom's Preferences
2. Search the bundled packages for "Autocomplete Plus"

3. Go to t…