Get Started with Sinatra on Heroku

This post will give you a brief explanation on getting started with Ruby and the Sinatra web framework deployed to Heroku. It’s important that you have basic Ruby knowledge, including an installed version of Ruby 1.9.2, Rubygems, and Bundler.

Other prerequisites are as below:

Basic Git knowledge

Your app needs to run on Ruby (MRI) 1.9.2

Your app should use Bundler

Heroku account

Set Up Your Local Workstation

To get access to the Heroku command-line client, Git distributed version control, and Foreman, install the Heroku Toolbelt on your local workstation.

Log in using your email ID and password by typing heroku command in the command shell.

$ heroku login
Enter your Heroku credentials.
Email: adam@example.com
Password:
Could not find an existing public key.
Would you like to generate one? Yn
Generating new SSH public key.
Uploading ssh public key /Users/adam/.ssh/id_rsa.pub

When it says ‘Could not find an existing public key’, type y to create a new ssh key. This key allows you to push code to Heroku. Using the heroku command, you can add public key in Heroku.

Getting Started

If you have an existing Sinatra app, feel free to use it. Otherwise, here’s a “Learning Ruby on Heroku” sourcefile you can use:

web.rb

require 'sinatra'
get '/' do
&quot;Learning Ruby on Heroku&quot;
end

Declaring Dependencies with a Gemfile

The existence of a Gemfile tells Heroku that this is a Ruby application.

Gemfile

source &quot;https://rubygems.org&quot;
gem 'sinatra', '1.1.0'

Run bundle install to set up bundle on your local workstation.

Declaring Process Types with a Procfile

Foreman uses a text file named ‘Procfile’ in the root directory of your app to declare what processes need to run.

Since all Sinatra apps are Rack-compliant, we can create a config.ru file in the root of the directory and tell Foreman to use it.

A single process type, web has been declared, along with the command required to execute it. The name “web” has significance here, as it states that this process type would be connected to the HTTP routing stack of Heroku, and handle web traffic when implemented.

Now, you’re all set to get started with your app locally using Foreman (installed within the Toolbelt):

It shows that 1 dyno of the app is running. With command heroku open, you can visit your app.

$ heroku open
Opening blazing-galaxy-997... done

Scaling Up

Your first web dyno for a Heroku application is free. However, if you don’t scale beyond that free dyno, Heroku reserves the right to kill the dyno after a period of non-use. You can avoid this by adding a second dyno:

$ heroku ps:scale web=2

You can go for maximum 750 dyno-hours for free for each application. Make sure you don’t run out of free monthly allowance. To scale back to one dyno:

$ heroku ps:scale web=1

Checking Out the Log

Heroku facilitates the viewinng of log files with the command heroku logs:

Console

You can run commands in a one-off dyno, such as scripts that just need to be executed once, with the heroku run command. Using it, you can launch an interactive Ruby shell ( bundle exec irb ) linked to the local terminal for testing in the app’s environment: