Moving an Existing Rails App to Run on JRuby
Last updated 10 April 2020
Table of Contents
JRuby is an implementation of Ruby written to run on the JVM. JRuby is written to be compatible with MRI ruby. There are some differences in the implementations to be aware of. One of the most notable is that since the JVM does not have a global VM lock, you can run multiple threads of Ruby code concurrently. MRI is written in C and supports native C extensions, while JRuby does have support for running these extensions it can be a point of incompatibility in code.
This article shows how to convert an existing Rails project that works on Heroku to run on JRuby. It uses JRuby
1.7.16 for consistency, to see all supported versions please reference the supported Ruby Versions article. Using the latest supported version will likely provide the best application performance.
Install JRuby locally
If you use RVM you can install JRuby using the latest RVM version
$ rvm get latest
Once you have the latest RVM installed install jruby locally. If you wanted to install version
1.7.16 you could run:
$ rvm install jruby 1.7.16
Now you should be able to use JRuby locally:
$ rvm use jruby-1.7.16 $ ruby -v jruby 1.7.16 (1.9.3p203) 2012-10-22 ff1ebbe on Java HotSpot(TM) 64-Bit Server VM 1.6.0_33-b03-424-11M3720 [darwin-x86_64]
Specify JRuby in your gemfile
Once you’ve got JRuby installed locally you can add this line to your
ruby '1.9.3', :engine => 'jruby', :engine_version => '1.7.16'
Here you are telling bundler that you intend the app to run on JRuby version 1.7.0 under compatablity mode with Ruby 1.9.3. There are going to be a few changes we will have to make to your app before we can run
bundle install. Let’s take a look at them now:
Replace your server with JRuby compatible Server
Many popular Ruby server libraries rely on C bindings. There are several recommended servers on the JRuby github page. You will need one that can run your JRuby application inside of a dyno without exceeding available memory. You can use Puma the pure ruby rack server:
Also Trinidad is known to work well, though may have a slightly larger memory footprint:
You will need to change the commands in your Procfile to start your app using the JRuby server of your choice. An example for the Puma webserver would be:
web: bundle exec rails server puma -p $PORT -e $RACK_ENV
JDBC Database Drivers
JDBC drivers for most popular databases such as Postgresql are already available for the JVM. The majority of JDBC drivers are very mature and have been heavily optimized for speed. You will need to remove the regular
pg gem from your Gemfile:
And replace it with the
Once you have made the replacements to your Gemfile make sure you are using jruby:
$ ruby -v jruby 1.7.16
$ bundle install. If you get failures read the next section.
Replace jRuby Incompatible Gems
While many gems in the community are JRuby compatible, you may be relying on some that are not. Many gem authors choose to enable JRuby tests on TravisCI, so you could check compatibility using their most recent builds. If no records exist of whether a project will run on JRuby you can always try installing it through bundler:
$ bundle install
$ gem install puma
Even if a gem installs correctly there may be subtle bugs that exist in the library. It is always a good idea to run tests on your project and perform some manual testing locally to confirm compatibility.
There have been reports of incompatibilities using different versions of the twitter-bootstrap-rails gem with JRuby. You may need to upgrade to a more recent version of the gem. You can find installation instructions in the twitter-bootstrap-rails README.
Deploy to Heroku
Once you’ve got your Rails project working locally on JRuby you will want to commit your code to git, spin up a new Heroku app and deploy.
$ git add . $ git commit -m "trying out JRuby on Heroku" $ heroku create --remote jruby-heroku
Now you can deploy by pushing to the
$ git push jruby-heroku master Counting objects: 692, done. Delta compression using up to 4 threads. Compressing objects: 100% (662/662), done. Writing objects: 100% (692/692), 141.01 KiB, done. Total 692 (delta 379), reused 0 (delta 0) -----> Heroku receiving push -----> Ruby/Rails app detected -----> Using Ruby version: ruby-1.9.3-jruby-1.7.16 -----> Installing JVM: openjdk7-latest -----> Installing dependencies using Bundler version 1.2.1 # ...
If you’ve been making your changes in a branch you can push to heroku using the
my-branch:master convention. For instance if you made all your changes in a branch called
jruby-experiment you could push to heroku using
$ git push jruby-heroku jruby-experiment:master
If you get any errors or warnings use the messages to debug, and try to reproduce the issue locally. If you get stuck you can ask any application specific questions on Stack Overflow.
See Specifying a Ruby Version’s troubleshooting section for some general advice.