Deploying Sinatra Apps on Heroku
You’ve built your amazing web app, but its no use if nobody can see it. You need to get it out there on the world wide web!
Heroku
By far the easiest way to deploy Sinatra apps is to use Heroku. This is an excellent service that can be used to host ruby-based apps (such as Rails, Merb, Rameze and of course Sinatra) in the cloud. It is free for basic sites, includes database storage and will scale as your app becomes more popular. It takes all of the hassle out of hosting sites and can be used for large scale production sites. They also have excellent documentation and support.
Before you go any further, make sure that you have Git installed because you will need it to deploy to Heroku.
Sign Up
You can sign up for a free Heroku account at http://heroku.com/signup.
Install Heroku gem
Make sure that you have installed Ruby Gems, then enter the following command in a terminal:
$ sudo gem install heroku
This is a simple command-line tool that allows you to interact with your site. You can read more about it here.
If you are using Windows then things are not so simple because the dependencies of the gem don’t always play nicely. If you encounter problems, try following these instructions.
(Thanks to Lucas for the help with Windows!)
Upload Your SSH Key
To be able to communicate with Heroku,you need to use a SSH key. If you don’t have one already, you can generate one by typing the following command at the terminal:
$ ssh-keygen -t rsa -C “yourname6@gmail.com”
More information can be found about generating SSH keys over at github.
Now you need to register your key to your Heroku account:
$ heroku keys:add
You should get the following message:
Enter your Heroku credentials.
Email: youremail@gmail.com
Password:
Answer the questions correctly and you should get the something similar to following message:
Uploading ssh public key /Users/daz/.ssh/id_rsa.pub
The previous steps only need doing once. From now on you will only need to perform the following steps ever time you want to put one of your Sinatra apps online.
Rackup File
All a Sinatra app needs for deployment is what’s called a rackup file. This goes in the root folder of your application and is always titled ‘config.ru’ (so the web server can find it easily). Go into the reverse folder and create a new file called and type the following:
require 'main'
run Sinatra::Application
Now save it as ‘config.ru’. You can see this page on github.
Add Your Site to Heroku
The last step is to actually put your site on Heroku. Open up a terminal and navigate into your reverse folder. You’ll need to choose a name for your app (you can’t have reverse, because I have already taken it!). Enter the following lines of code:
$ heroku create appname
$ git push heroku master
You should see something similar to the following message:
Counting objects: 7, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 441 bytes, done.
Total 4 (delta 3), reused 0 (delta 0)
-----> Heroku receiving push
-----> Sinatra app detected
Compiled slug size is 588K
-----> Launching...... done
http://appname.heroku.com deployed to Heroku
To git@heroku.com:appname.git
3b3ed7c..350b521 master -> master
Now all you need to do is navigate to ‘http://appname.heroku.com’ to see your app live on the Internet!
Updating Your Site
You can deploy any changes you make to your app with just one simple command (assuming that it is the master branch that you want to deploy):
$ git push heroku master
This is where git’s branching functionality comes in handy. You can develop on one branch and use another branch for the live site.
Hopefully this post has shown just how easy it is to deploy Sinatra apps and what a great service Heroku is. Have fun putting your apps live and leave any links in the comments below.