Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JRuby with Sinatra on Heroku

I'm cloning this repo:

https://github.com/freeformz/sinatra-jruby-heroku.git

to try and use JRuby/Sinatra on Heroku's Cedar stack. I follow the included instructions and everything runs great locally with a 'foreman start'. I then git push to Heroku and it initially loads up fine but when I try to access the site I get an error in the logs:

jruby: No such file or directory -- trinidad (LoadError)

So it seems jruby can't find the "/app/.gems/bin/trinidad" file. I initially thought it wasn't there because .gems/ is in the .gitignore file, but I'm pretty sure Heroku creates that server side on a git push.

$APPDIR/.gems is added to the PATH so Heroku should be able to see the trinidad script. I've also tried to change the Procfile around to play with the path like:

web: script/jruby -S bin/trinidad -p $PORT

But no dice. Has anyone had any success deploying anything JRuby to Heroku cedar?

Thanks

like image 201
Brian Avatar asked Mar 04 '26 02:03

Brian


1 Answers

As of Bundler 1.2 you are now able to specify the Ruby implementation and version in your Gemfile. The nice thing about this is that Heroku will understand these settings and prepare the your Heroku application for your environment.

Take this Gemfile for example:

source "https://rubygems.org"

ruby "1.9.3"

gem "rails"
gem "puma"

What's cool about this is that by default Celadon Cedar uses Ruby 1.9.2. However, when you specify ruby "1.9.3" in the Gemfile it'll actually compile Ruby 1.9.3 for your Heroku environment.

Now, if you want to add a different Ruby implementation to your Heroku environment, you can do so like this:

source "https://rubygems.org"

ruby "1.9.3", :engine => "jruby", :engine_version => "1.7.0.preview1"

gem "rails"
gem "puma"

Now it'll install and use JRuby 1.7.0.preview1 in Ruby 1.9 mode for your Heroku application upon deployment. It'll also even define the proper JVM options in the Heroku environment variables.

Best of all is that this comes with the official Heroku buildpack, so there is no need to switch to a 3rd party buildpack to get the JRuby/JVM going on Heroku. Although I haven't gotten it to work yet, this should also work with Rubinius, but I believe it's currently bugged. Either that, or I'm doing it wrong.

This is in my opinion an awesome and scalable feature. Just define the Ruby implementation/version/mode you're using in your Gemfile along with your other dependencies and Heroku will ensure the environment is prepared.


Now, with all this in place, Heroku should create binstubs (through Bundler) in APP_ROOT/bin so what you can do is for example this:

web: bin/trinidad -p $PORT -e $RACK_ENV --threaded

Just don't use bundle exec since JRuby doesn't play nice with that. Always use the binstubs provided by Bundler which are always located in APP_ROOT/bin on Heroku.

like image 74
Michael van Rooijen Avatar answered Mar 05 '26 16:03

Michael van Rooijen