Deploying a Sinatra App to Heroku
Heroku is a cloud application platform that is designed to take the worry out of server administration. You just build your app, push it to Heroku via Git, and relax. What about deploying a Sinatra app though? Let’s see…
If you followed along with my post about building a data driven app with Sinatra you might be ready to try a deployment. Since Sinatra is a Ruby app, deployment it not quite as straightforward as say, a PHP app, but it’s still not hard.
Getting started with Sinatra…
You will need a (free) account at Heroku.
Assuming that you are happy with your app locally, the next thing to do is make sure you have a couple of files:
The Gemfile I described in the post above includes references to all the Ruby gems used in the app:
source :rubygems gem 'sinatra' gem 'rdiscount' gem 'data_mapper' gem 'dm-migrations' gem 'pony' gem 'dm-postgres-adapter', :group => :production gem 'dm-sqlite-adapter', :group => :development group :production do gem 'pg', '0.14.1' end
You may not need some of these: rdiscount, and Pony for example. I’ll discuss those in a later post. Take note of the groups for production and development. Heroku doesn’t support Sqlite.
Sinatra is a Rack app, but Heroku has no problems with those. We just need a file to explain how we want our app to run:
In config.ru you need the following:
require './main' run Sinatra::Application
You deploy to Heroku via Git, the source code management system. You can initialise a Git repository from the command line:
Install the Heroku Gem
You will also need the Heroku gem:
gem install heroku
You will need to create SSH keys. You can learn how to do that here
While you are on the command line, you will need to connect your keys to Heroku for secure communication between your computer at Heroku:
You’ll be asked to add the credentials that you created your Heroku account with.
Create the Heroku App
Back on the command line do:
heroku create nameofapp
Where nameofapp is what ever name you want for your app, bearing in mind that that Heroku will create your app with a url like this: nameofapp.herokuapp.com.
Then, assuming that you have committed your latest updates, you can do:
git push heroku master
You’ll see Heroku receive the push, compress files, install the Ruby gems needed, and then, with any luck, deploy your app.
All you need to do now, is continue to develop your app, commit, and the Git push to Heroku to deploy.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)