An Afternoon with Sinatra and Heroku

This is a quick summary of my experience today getting my first “hello world” Web API written in Sinatra and deployed to Heroku.  For those of you who are already experts, you’ll probably find lots of things I did wrong or could have done differently – please tell me or point me to resources.  I want to get better at this.  It was a great experience that left me wanting to do more of it…

Getting setup to develop my first Sinatra application on Heroku was basically a 2 step process.  First I needed to get a Ruby environment that I could run on Windows.  This was the first point of confusion because it seems like there are quite a few different options.  However, it seems like has basically risen to the top (at least in terms of Google results).  Ruby installer is a single Windows MSI which installs Ruby, some initial gems, and a bunch of other stuff that I’m not sure about.  By install, as best I can tell, it lays files down on disk and adds entries to the path environment variable.  After installing, I can test my ruby files by simply running Ruby myfile.rb in mingw32.  The second thing that I installed via MSI was the Heroku toolbelt.  Again, I’m not sure what all this installation did (I suspect more files and path mods), but afterwards I can run commands such as heroku login.

First, here were the resources I followed to get setup on Heroku:

The quickstart took me through the process of signing up (which I had already done), installing the tools, logging in through the command line which would setup ssh keys if I didn’t already have them (I did, and that was a little problematic), and then it linked off to the platform specific getting started guide.

The getting started with Ruby guide walked me through the process of developing a hello world api in Sinatra, testing it locally and then deploying it to Heroku.  The first step was setting up the Heroku tools again, which I think may have installed Git again on my system.  Need to check that.  Writing my first Sinatra app was crazy easy – in fact, this was all it consisted of:

require ‘sinatra’

get ‘/’ do

“Hello, world”


Testing locally consisted of simply running ‘ruby -rubygems web.rb’.   The next part of the setup dealt with creating a gemfile (using Bundler?).  This part still seems a little too magical for me, but I went through the tutorial and created a gemfile declaring a single dependency on Sinatra.  The sample also had a dependency on a gem called thin, but I tried installing that gem a couple times and it failed because I didn’t have some kind of Ruby developer kit.  I’m not sure what that meant, but I can run and deploy without thin, so that’s what I did.  On a quick glance, it appears that thin is a Web server?

The next step was to create a procfile which from what I can tell instructs the runtime (Cedar, is the runtime that Heroku is using I believe) how to launch my application in production.  The template also mentioned Rack, and I’m not sure how that relates to the other stuff, but it wasn’t a blocker.

Now, I tried installing foreman because the getting started guide made it sound like by doing this, I could get a more realistic test of how my app would run in Heroku.  The problem was that I couldn’t get foreman to run on Windows, and a thread on github made it sound like foreman simply doesn’t run on Windows at all.  Looks like I may be buying a Mac in the not-too-distant future.  At any rate, not having foreman didn’t keep me from being able to deploy.

The final step was super easy once I resolved an SSH issue – though I think that this was because I’m already comfortable with Git.  I simply initialized a new Git repo in my working folder and then ran the following command:

heroku create –stack cedar

This caused heroku to do a bunch of provisioning stuff, including create a new remote repository – which it then kindly added to my local Git config.  This meant that deploying my app was simply:

git push heroku master

Initially, the remote Git repo hung up on me because I had created a new SSH public key on my laptop and it wasn’t the key that I originally configured with Heroku – a search, however, lead me to the right heroku command to add my SSH key:

heroku keys:add

After running this, I was able to push to heroku, and saw that heroku spun up a Web server and launched my app.  4 seconds later, I ran ‘heroku ps’ and saw that it had crashed.  This was because I initially got something wrong in my procfile – so I fixed it and simply updated/pushed my repo.  It then redeployed and worked beautifully (at least as far as cUrl is concerned).

In general, there are still some things I need to know better before I’ll feel good about everything I’m doing – there were a lot of terms thrown out there in my getting started experience, and I’m confident that I barely scratched the surface on all of them – specifically, I’m thinking of:

  • Sinatra
  • Bundler
  • Cedar
  • Rack
  • Thin
  • Foreman

And of course, I need to continue to get better at Ruby in general.  However, this was my first experience – and all in all, it was a good one.

About Howard Dierking

I like technology...a lot...
This entry was posted in Heroku, Ruby, Sinatra, Web, Web API. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • saranya

           Hi I registered in top freelancing sites and
    found the bidding system is not at all helpful for any new freelancer. All
    these top freelancing sites they sell your skills for lower price. I came
    across and
    found it is the best site for any freelancer. Hireprogrammers help freelancers
    in negotiating with client to get the right quote for the project. Thanks to
    the hire programmers team for awarding a new project to me with hire rate as

  • Rita Joel2011

    your first experience  for getting your first hello world is interesting as well as informative… !! I should simply say that you did great.

  • James Kovacs

    To answer some of your questions… The Gemfile declares your dependencies. It’s equivalent to your packages.xml in NuGet. Running “bundle install” will look at your Gemfile and download/install those dependencies on your local machine. (If you’re going to do serious Ruby development, you want to dive into the world of pik (Windows), rvm, or rbenv (Linux/OSX). They make it easy to install different versions of Ruby and manage gemsets.)

    And you’re right that thin is a webserver. Ruby has a metric ton of different webservers. WEBrick is often used in development. I believe that Heroku runs thin on its servers. Hence the recommendation. To compile thin, you need the RubyInstaller DevKit, which includes a variety of compiler-related tools. No, thin isn’t necessary, but it does make your local dev environment closer to Heroku.

    Rack is a framework that abstracts HTTP request/response processing. Rails, Sinantra, and a variety of other frameworks are built on top of Rack. It plays a similar role as the HTTP modules/handlers infrastructure in ASP.NET.

    Foreman is a Ruby gem for process management. Without Foreman you would run your Rails or Sinatra app directly as well as any additional components, such as mailers, batch processors, work queues, or other daemon jobs. With Foreman, you declare which processes make up your application in a Procfile and then launch Foreman. Foreman handles process management for you.

    If you want to learn more about any of these topics, the fastest way to figure them out IMHO is to watch Ryan Bates (@rbates:disqus) episode on the topic.

    Happy Hacking!