In this post, I'll walk you through the basic steps for setting up Rails 4 to run with Puma under JRuby. Then you'll learn how to deploy that app on Heroku.
Creating a New App
First, let's create a brand new Rails 4 application. Make sure you have JRuby installed and are using it, like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rvm use jruby-1.7.16.1 | |
Using /Users/jkutner/.rvm/gems/jruby-1.7.16 | |
$ ruby -v | |
jruby 1.7.16 (1.9.3p392) 2014-09-25 575b395 on Java HotSpot(TM) 64-Bit Server VM 1.7.0_51-b13 +jit [darwin-x86_64] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ gem install rails -v 4.1.8 | |
... | |
Successfully installed rails-4.1.8 | |
1 gem installed | |
$ rails new my_app --database=postgresql | |
... | |
Use `bundle show [gemname]` to see where a bundled gem is installed. |
But the new application does not have a production ready web server. That's were Puma comes in.
Using and Configuring Puma
Move into the root directory for your new application and open the Gemfile. You'll probably see a commented out line for the "unicorn" gem. Delete it. JRuby and Unicorn are a bad combination (if they even work together at all).
Now add the following line to the Gemfile to make Puma a dependency:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem "puma", "2.10.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
port ENV['PORT'] || 3000 | |
environment ENV['RACK_ENV'] || 'development' | |
threads (ENV["MIN_PUMA_THREADS"] || 0), (ENV["MAX_PUMA_THREADS"] || 16) | |
preload_app! |
Because Puma is a multi-threaded server, and JRuby has real threads, you'll want to configure your database connection pool size appropriately. To do this, create a config/initializers/database_connection.rb file, and put the following code in it:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.config.after_initialize do | |
ActiveRecord::Base.connection_pool.disconnect! | |
ActiveSupport.on_load(:active_record) do | |
config = ActiveRecord::Base.configurations[Rails.env] || | |
Rails.application.config.database_configuration[Rails.env] | |
config['pool'] = ENV['MAX_PUMA_THREADS'] || 16 | |
ActiveRecord::Base.establish_connection(config) | |
end | |
end |
Now run the application with this command:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ bundle exec puma -C config/puma.rb |
Deploying to Heroku
Heroku is a Platform-as-a-Service that supports both Ruby and Java applications. Naturally, it also supports JRuby. To start using Heroku, follow this guide for installing the Heroku toolbelt and creating an account. This will provide you with a heroku command that you can use to create and deploy apps for free!
To start, make sure your project is under version control with Git by running these commands:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ git init | |
$ git add . | |
$ git commit -am "first commit" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
web: bundle exec puma -C config/puma.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby '1.9.3', :engine => 'jruby', :engine_version => '1.7.16' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ git add Procfile Gemfile | |
$ git commit -m "prepared app for heroku" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ heroku create | |
Creating jkutner-test... done, stack is cedar-14 | |
https://jkutner-test.herokuapp.com/ | https://git.heroku.com/jkutner-test.git | |
Git remote heroku added | |
$ git push heroku master | |
Counting objects: 34, done. | |
... | |
-----> Ruby app detected | |
-----> Compiling Ruby/Rails | |
-----> Using Ruby version: ruby-1.9.3-jruby-1.7.16 | |
-----> Installing JVM: openjdk7-latest | |
Picked up JAVA_TOOL_OPTIONS: -Djava.rmi.server.useCodebaseOnly=true | |
-----> Installing dependencies using 1.6.3 | |
Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment | |
Picked up JAVA_TOOL_OPTIONS: -Djava.rmi.server.useCodebaseOnly=true | |
... | |
Your bundle is complete! | |
Gems in the groups development and test were not installed. | |
It was installed into ./vendor/bundle | |
Bundle completed (16.73s) | |
Cleaning up the bundler cache. | |
-----> Preparing app for Rails asset pipeline | |
Running: rake assets:precompile | |
Picked up JAVA_TOOL_OPTIONS: -Djava.rmi.server.useCodebaseOnly=true | |
I, [2014-12-04T20:09:48.558000 #970] INFO -- : Writing /tmp/build_4314fe0fe5c1c451df6755a9edded670/public/assets/application-3977927cf8f68a5ebafbe59011904fad.js | |
Asset precompilation completed (61.06s) | |
Cleaning assets | |
Running: rake assets:clean | |
Picked up JAVA_TOOL_OPTIONS: -Djava.rmi.server.useCodebaseOnly=true | |
WARNING: | |
Include 'rails_12factor' gem to enable all platform features | |
See https://devcenter.heroku.com/articles/rails-integration-gems for more information. | |
-----> Discovering process types | |
Procfile declares types -> web | |
Default types for Ruby -> console, rake, worker | |
-----> Compressing... done, 112.5MB | |
-----> Launching... done, v7 | |
https://jkutner-test.herokuapp.com/ deployed to Heroku | |
Verifying deploy... done. | |
To https://git.heroku.com/jkutner-test.git | |
7288c10..7814b0d master -> master |
There is a great deal more that Heroku can do for you. You can provision a database, a queuing system, a scheduler, monitoring tools, or even telephony services. And of course, you can begin to leverage the power of the JVM for your Ruby app.
You can download the complete source code for the application presented in this article from the heroku-jruby-rails-4 repo on Github.
Thanks for the post. I think you forgot to include the code snippet after this line: "You'll also need to configure the Ruby runtime in your Gemfile (in this case its JRuby) by adding this line "
ReplyDeleteThanks! Fixed it.
DeleteA crucial piece missing from your recipe is configuring the size of the database connection pool to match the number of threads. This article describes that: https://devcenter.heroku.com/articles/concurrency-and-database-connections
ReplyDeleteGood call, that is important. I'll add a note. Thanks again.
DeleteGreat article, thank you. What about JRuby's performance in comparison to MRI(2.1.5) ?
ReplyDeleteI tried JRuby in production for a server with about 100 req/seq.(moved from MRI) I didn't notice any speed up.
Is there any point to move to it at all?
I think it really depends on your application, and what it is doing during each request cycle. At a lower load, such as you tried, there may not be a big difference. But I suspect most apps will see improvements as they start to scale up. The JVM has better memory characteristics and better GC. And because Puma can use threads instead of separate processes to achieve true concurrency, the threads can actually share memory instead of the processes replicating your app (and running multiple garbage collectors).
DeleteI'd like to know how can we perform a graceful restart while deploying? Since it's clearly written in the official readme file (https://github.com/puma/puma#platform-constraints), I'm having second thoughts on choosing Puma server for Jruby.
ReplyDeleteThanks for the post,Really you given a valuable information.worth to read this type of articles .
ReplyDeleteThank you.
oracle R12 training
Thanks you!! very useful for me to study and very simple and easy to undersatnd...
ReplyDeleteWebDschool
UI UX Design Courses in Chennai
simple and neat article...thanks for sharing with us
ReplyDeleteUI UX Design Courses in Chennai
Thanks you!! very useful for me to study and very simple and easy to undersatnd...
ReplyDeletedigital marketing courses in delhi
This is an incredibly detailed guide on deploying Rails 4 with Puma and JRuby on Heroku—super helpful for developers exploring JRuby! 🚀 The insights on configuring Puma for JRuby’s real threading capabilities and tweaking the database connection pool are spot on. I particularly liked the step-by-step breakdown for Heroku deployment and configuring the Procfile.
ReplyDeleteFor those diving into modern tech stacks like this, it might also be a great time to explore data Science courses in Delhi . Learning data science can complement web development skills by enabling developers to integrate analytics, machine learning, and data-driven decision-making into their applications.
loved the post.
ReplyDeleteMedical Coding Courses in Kochi
thanks for sharing such a nice blog to us
ReplyDeleteMedical Coding Courses in Kochi
This is a fantastic guide for deploying Rails 4 with Puma on JRuby to Heroku! The step-by-step breakdown makes it easier for developers to navigate the setup process, especially those transitioning from MRI-based guides. The emphasis on configuring Puma correctly and managing database connections for JRuby’s threading capabilities is particularly useful.
ReplyDeleteOn a different note, if you're looking to explore opportunities beyond development, consider checking out Medical Coding Courses in Delhi. These courses offer valuable skills in the growing healthcare industry.
Great article—looking forward to more insights on JRuby deployments! 🚀
thanks for the post
ReplyDeleteMedical Coding Course in Hyderabad
it is actually nice was reading
ReplyDeleteMedical Coding Course in Hyderabad
Great guide on setting up Rails 4 with Puma and JRuby on Heroku! 🚀 The step-by-step breakdown makes it easy to follow, especially for those transitioning from MRI to JRuby. The inclusion of database connection pooling optimizations is a nice touch for handling concurrency effectively.
ReplyDeleteFor anyone interested in Medical Coding Courses in Delhi, there are excellent opportunities to build a career in healthcare data management while mastering coding skills in a high-demand field.
Thanks for sharing this—Heroku deployment just got a lot smoother! 💡🔥
Medical Coding Course in Hyderabad VERY informational blog
ReplyDeleteThis guide is super helpful! Deploying Rails 4 apps with Puma on Heroku can be tricky, but your step-by-step breakdown makes it much easier to follow.
ReplyDeleteMedical Coding Courses in Delhi
We are one of the best digital marketing, UI UX and video editing course in bangalore. We offer course with affordable fees. Digitalents academy is one of the top institute in bangalore. This digital marketing course is highly informative and well-structured! The lessons are practical, and the insights shared are very useful for beginners as well as professionals. Highly recommended for anyone looking to upskill in digital marketing! Visit Digitalents Academy
ReplyDelete