Saturday, May 11, 2013

How to Run Rails 4.0.0.rc1 on JRuby


Getting Rails 4.0.0.rc1 running on JRuby isn't that different from running it on MRI, but there are a few minor things you'll need to adjust. Let's start with a new Rails4 app. Make sure you're using JRuby and install the Rails4 release candidate like this:

$ ruby -v
jruby 1.7.4.dev (1.9.3p392) 2013-05-01 fffffff on Java HotSpot(TM) 64-Bit Server VM 1.7.0_09-b05 [darwin-x86_64]
$ gem install rails -v 4.0.0.rc1
Successfully installed rails-4.0.0.rc1
1 gem installed
view raw example1 hosted with ❤ by GitHub
Then use the rails command to generate a new app.

$ rails new my_app
...
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
view raw example2 hosted with ❤ by GitHub
Move into the newly created my_app directory so we can tweak some settings. Open the Gemfile in an editor and look for these lines:

# Use jdbcsqlite3 as the database for Active Record
gem 'activerecord-jdbcsqlite3-adapter'
gem 'jruby-openssl'
view raw Gemfile1.rb hosted with ❤ by GitHub
First, we'll remove jruby-openssl because it's included in JRuby 1.7 (this has been fixed in Rails master). Then we need to set the version of activerecord-jdbcsqlite3-adapter to 1.3.0.beta1.  Next, you should thank Karol Bucek (@kares) for all the hard work he's put into activerecord-jdbc and jruby-rack. Your Gemfile should now have just these lines in in place of the code shown above:

# Use jdbcsqlite3 as the database for Active Record
gem 'activerecord-jdbcsqlite3-adapter', '1.3.0.beta1'
view raw Gemfile2.rb hosted with ❤ by GitHub
Now return to the console and update bundler like this:

$ bundle update
...
Your bundle is updated!
view raw example3 hosted with ❤ by GitHub
Do some basic housekeeping

$ bundle exec rake db:create
view raw example4 hosted with ❤ by GitHub
Now we can smoke test the app by starting the server like this:

$ bundle exec rails server
=> Booting WEBrick
=> Rails 4.0.0.rc1 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2013-05-11 09:25:05] INFO WEBrick 1.3.1
[2013-05-11 09:25:05] INFO ruby 1.9.3 (2013-05-01) [java]
[2013-05-11 09:25:05] INFO WEBrick::HTTPServer#start: pid=3146 port=3000
view raw example5 hosted with ❤ by GitHub
Point a browser to http://localhost:3000 and there's a chance you'll see the app running.  But you might encounter the error shown below:

OpenSSL::Cipher::CipherError: Illegal key size: possibly you need to install Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files for your JRE

If you see this error it means you need to install the unrestricted policy files for the JVM. You can find these at the Oracle Website. Download the zip file, and extract the two important files it contains: local_policy.jar and US_export_policy.jar. Move these files into your $JAVA_HOME/jre/lib/security directory, and replace the existing files of the same name.  On Mac OS X they are probably located here:

/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/security/

With the unrestricted policy files installed, restart the server and you'll be good to go. But this may present a problem in deployment. For example, you won't be able to update the JVM on a Heroku dyno. Another option may be to downgrade cryptography as described in this JIRA issue, but I haven't tried that. Hopefully this will all get worked out.

And of course, you probably won't want to use WEBrick in production. Warbler 1.3.8 may work for you, but try the rails4 branch if it doesn't.  I have an example of a working Rails4 app on BitBucket in my warbler-examples repo.

I haven't attempted to run a Rails4 app on Trinidad, Puma or TorqueBox. I would love to hear your results.

Please give this a go, and report back with any problems you find.  We would love to have Rails4 working on JRuby the day it's released.