A collection of computer systems and programming tips that you may find useful.
Brought to you by Craic Computing LLC, a bioinformatics consulting company.

Friday, February 4, 2011

Running a Ruby 1.9 Sinatra app on Heroku

I just got my first significant application running on Heroku. It uses Sinatra instead of Rails and uses Ruby 1.9 - as a result the steps to get the application up and running were slightly different from the Heroku Quickstart Guide, which is tailored towards Rails apps.

1. Setup
Setup a Heroku account and set up the SSH keys
Create your app and make sure it runs correctly on your local machine.
Make sure that all paths are relative to the application root.
Run the Sinatra app from a config.ru file.

2. Setup Bundler
I had an issue with this originally but this is what worked. I only need the sinatra gem so my Gemfile is:
source :gemcutter
gem 'sinatra'
and my config.ru file is:
require 'bundler'
require 'sinatra'
require './my_app.rb'
run MyApp.new
Note that although you need to have the heroku gem installed on your system in order to upload to Heroku, you do not 'require' it in your app.

3. Setup git and commit the project.

4. Create the Heroku app
You want to specify an application name, otherwise Heroku will give you an arbitrary one. You also want to specify the run environment at Heroku that will be used. They refer to this as the 'stack' and for Ruby 1.9 you want to specify this directly and currently the correct option is 'bamboo-mri-1.9.2'. The create command is:
$ heroku create my_app_name --stack bamboo-mri-1.9.2

5. Push the git repository to Heroku with:
$ git push heroku master
The messages that follow should look something like this (some lines removed):
-----> Heroku receiving push
-----> Sinatra app detected
-----> Gemfile detected, running Bundler version 1.0.7
Unresolved dependencies detected; Installing...
Fetching source index for http://rubygems.org/
Installing rack (1.2.1)
Installing tilt (1.2.2)
Installing sinatra (1.1.2)
Using bundler (1.0.7)
Your bundle is complete! It was installed into ./.bundle/gems/
Compiled slug size is 500K
-----> Launching... done
http://my_app_name.heroku.com deployed to Heroku
Now go to that URL and your app should be running.

If there was a problem and the app failed to start then look at the logs:
$ heroku logs -n 100

You can set up apps with minimal (or no) database storage for free on Heroku. This is a great service as it lets you experiment to your heart's content.

The idea behind Heroku is to remove from you the burden of server configuration. For my simple application this seems to work remarkably well.


1 comment:

Blogger said...

BlueHost is ultimately the best hosting company for any hosting plans you might need.

Archive of Tips