Last post I demonstrated setting up a Ruby on Rails app on Heroku, running with the Puma server. As I mentioned before, in order to get the most out of Puma you should use a Ruby implementation with real threads like JRuby or Rubinius 2. In this post we’ll repeat most of the previous steps, but this time we’ll use a Third-Party Buildpack and get setup with JRuby.
Note: I’m not covering the specifics other than telling you to use RVM or rbenv to setup JRuby (I’m running 1.6.7.2)
First create a new rails app
~$ jruby -S rails new heroku-jruby-puma
create
...
~$ cd heroku-jruby-puma
~$ git init
If you check your Gemfile, you should see some key differences:
1
sqlite3
gem you have 1
activerecord-jdbcsqlite3-adapter
1
jruby-openssl
included1
assets
group includes 1
therubyrhino
gemSome adjustments are needed to get this new Rails app to play nice on
Heroku with Puma. First, we can’t just use the
gem; You need to use
the 1
pg
instead. Add 1
activerecord-jdbcpostgresql-adapter
and set
some configuration variables for Rails and we’re set.1
puma
Gemfile
...
gem 'activerecord-jdbcpostgresql-adapter'
gem 'puma'
...
group :development do
gem 'foreman'
gem 'activerecord-jdbcsqlite3-adapter'
end
Note: here I left
in the
1
activerecord-jdbcsqlite3-adapter
group just as an example.1
development
config/application.rb
...
config.assets.initialize_on_precompile = false
config/production.rb
...
config.serve_static_assets = true
...
config.threadsafe!
# Heroku log stuff
STDOUT.sync = true
config.logger = Logger.new(STDOUT)
Heroku’s Cedar stack has no native language or framework; instead your
sever is setup via a set of scripts called Buildpacks. Heroku
provides buildpacks for Ruby, Node.js, Python and a handful of others,
but you can also create or use your own Buildpack by using the
flag and providing a URL. Here we’ll use JRuby’s Heroku Buildpack from github.com/jruby/heroku-buildpack-jruby.1
--buildpack
~$ heroku create heroku-jruby-puma \
--buildpack https://github.com/jruby/heroku-buildpack-jruby.git
Creating heroku-jruby-puma ... done, stack is cedar
BUILDPACK_URL=https://github.com/jruby/heroku-buildpack-jruby.git
...
~$
Now when you push your code up, Heroku’s slug compiler knows how to package things an build them to use JRuby.
As I mentioned last time, a
tells Heroku how to run your app:1
Procfile
web: bin/puma -p $PORT -e $RACK_ENV
Thats it! Commit your changes and push to Heroku like before:
~$ git push
-----> Heroku receiving push
-----> Fetching custom buildpack... done
-----> JRuby app detected
-----> Vendoring JRuby into slug
-----> Installing dependencies with Bundler
...
-----> Writing config/database.yml to read from DATABASE_URL
-----> Precompiling assets
...
-----> Discovering process types
Procfile declares types -> web
-----> Compiled slug size is 44.5MB
-----> Launching... done, v7
http://heroku-jruby-puma.herokuapp.com deployed to Heroku
To git@heroku.com:heroku-jruby-puma.git
6bced4f..383a7cc master -> master
~$
Within a few moments you should be running on JRuby/Puma on Heroku. Notice that your slug size is significantly larger than a normal stock Rails app running on MRI. That’s because of 4th item you see above, “Vendoring JRuby into slug”.
Congratulations! You’re now running Rails with JRuby and Puma on Heroku.