I pushed my Sinatra application to heroku this week and thought others might want a short explanation of how it’s done.
Here are the basic steps that I had to take:
1) Create a Gemfile.rb file
2) Create a Config.ru file
3) Get a Heroku Account & Install Heroku Toolkit
4) Set up Heroku in your terminal
5) Deploy to Heroku
Create a Gemfile
First, what is a gem? A gem is a packaged Ruby application or library. It has a name (e.g. rake) and a version (e.g. 0.4.16). Gems are very useful for not reinventing the wheel and avoiding duplication. Many Ruby developers create and publish awesome gems, or packages of files and code, which address specific requirements, solve specific problems or add specific functionality. Anyone who comes across similar requirements or problems can use them in order to avoid writing duplicate code.
What is a Gemfile? A Gemfile is a file that you add to the root directory of your application that contains a list of all the gems needed to run the application. I used a handful of gems in my Sinatra App which other people will need to use if they want to run my app. By creating the gemfile, I am making it easy for someone else to clone and run my application.
How does it work? First, create a file names Gemfile.rb. This is what mine looks like:
source 'https://rubygems.org' gem 'rspec' gem 'sinatra' gem 'rack' gem 'rack-test' gem 'capybara'
Then, enter ‘bundle install’ into the command line. This will create a file called Gemfile.lock for you which keeps track of the versions you are using. Here’s is my Gemfile.lock
GEM remote: https://rubygems.org/ specs: capybara (2.4.1) mime-types (>= 1.16) nokogiri (>= 1.3.3) rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) diff-lcs (1.2.5) mime-types (1.25.1) mini_portile (0.6.0) nokogiri (188.8.131.52) mini_portile (= 0.6.0) rack (1.5.2) rack-protection (1.5.3) rack rack-test (0.6.2) rack (>= 1.0) rspec (3.0.0) rspec-core (~> 3.0.0) rspec-expectations (~> 3.0.0) rspec-mocks (~> 3.0.0) rspec-core (3.0.4) rspec-support (~> 3.0.0) rspec-expectations (3.0.4) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.0.0) rspec-mocks (3.0.4) rspec-support (~> 3.0.0) rspec-support (3.0.4) sinatra (1.4.5) rack (~> 1.4) rack-protection (~> 1.4) tilt (~> 1.3, >= 1.3.4) tilt (1.4.1) xpath (2.0.0) nokogiri (~> 1.3) PLATFORMS ruby DEPENDENCIES capybara rack rack-test rspec sinatra
Now anyone who wants to clone your app can simply run bundle install and all of the necessary gems will be downloaded to their machine.
Creating a config.ru
In order to run your application on Heroku you need to also add a config.ru file to your root directory. Here is my config.ru file:
require_relative './lib/sinatra/sinatra_app.rb' run SinatraApp
Get a Heroku Account & Install Heroku Toolkit
If you don’t already have one, you should go make a Heroku account. It’s free until you have a high volume of traffic, at which point you need to pay Heroku by usage.
Next, you should install Heroku Toolkit.
These instructions can be found here.
Set up Heroku in your terminal
If this is the first time you are using Heroku on your computer, you need to add SSH keys to Heroku.
You do this by just entering ‘heroku keys:add’ in the command line. If you don’t have any SSH keys generated yet, first run ‘ssh-keygen -t rsa’.
Next you should add heroku as a remote to your application.
$ heroku create
Then check to make sure you have a heroku remote by entering
$ git remote -v
Deploy to Heroku
Then push your master branch to heroku assuming you’ve already committed everything and pushed it to github.
git push heroku master
And, you’re done! Use ‘$ heroku open’ to open your new app from the command line. Now you can also go into your heroku account and change the name of your application (since it will have a random weird name) or update the DNS of a domain to point to your heroku app.
Check out the Heroku instructions here.