Deploy your web app today with Heroku!

Been developing a website or web-based application and need somewhere to host it? Try out Heroku, the Platform as a Service that provides fault-tolerance and a myriad of additional services as add-ons! Heroku’s dynos (unit of computational power) act as independent instances of your application and one is provided for free. Scaling your application is as easy as increasing the number of dynos running and means that your service can be distributed across regions, decreasing latency between your users and running instances.

This tutorial assumes you are very comfortable with the command line and have a basic familiarity with git. We will be running through creating a barebones “app” and mentioning ways to move forward from our simple “Hello World” example. Finally, we’ll be using Ruby because it’s fancier than PHP and more stable than Node.js. Those unfamiliar with Ruby should not fret, there won’t be much and all will be explained.

Setup

Obviously you’ll need Ruby, RVM is recommened on *nixs, but compiling from source or using your favorite package manager is also an option. For Windows environments the RubyInstaller has yet to fail me (don’t forget to install the corresponding DevKit). Additionally, the Heroku Toolbelt is necessary for managing keys and interacting with your app once its been published. We’ll also need a place to put a folder, and a text editor. The Bundler gem is also needed, so make sure to run “gem install bundler” once you’ve gotten Ruby set up. For the curious, gems are ruby extensions and bundler is a gem for managing gems. Meta.

Creating an account with Heroku is extremely simple. Once you’ve done that, sign in and create your first app:

create app dialog

Copy the git clone command given and run it. Be aware that this will create a folder with your app’s name where you run it.

create app dialog

Don’t forget to log into heroku:

heroku login

before attempting to push your changes. If you’ve run git-clone command you should see that the output complains about cloning an empty repo, so let’s fix that by adding some code.

create app dialog

Sinatra – The Simplest Server

You may have expected Rails since this is Ruby but we’re aiming for simplicity here. Enter sinatra, the (micro)framework that handles responding to HTTP requests without the massive architecture that Rails imposes. While Rails provides a framework rooted in the Model-View-Controller paradigm, Sinatra imposes no such principles or commandments. With Sinatra, you can add whatever form of templating you want, or none at all! There is no reason to have to manage database migrations or fiddle with a database-to-object library if you’re only trying to learn how dynamic websites work.

Greet the World!

Let’s tackle the simplest thing in programming, make a file (preferably app.rb) with these contents in the folder created when you cloned:

create app dialog

Do the usual git dance (status-add-commit) and push with

git push heroku master

If you see an error like this one

heroku push error

don’t fret! That just means Heroku couldn’t tell what kind of application we gave it and Heroku supports many languages. For example PHP, Node.js, and Python are also supported and have their own way of being identified. Gemfiles specifically are required because Bundler is used to handle dependencies, so create one (it must be named “Gemfile”) containing these lines:

source 'https://rubygems.org'
gem 'thin'
gem 'sinatra'

which should be self-explanatory. You’ll also need a “config.ru” file (this tells Heroku how to run your app) with these contents:

require './app'
run Sinatra::Application

The second line should yield some insight into the way Sinatra works, the globally defined functions used to handle routes (get/post/etc..) work because the script acts as the server when you require the sinatra library. For better encapsulation (and a cleaner global namespace) you can require ‘sinatra-base’ instead and define your app as a class that derives from the Sinatra::Base class. Finally, run “bundle install” to generate a Gemfile.lock which describes the app and all of its dependencies. Use git to add the files and changes, commit, and push the repository which should now include app.rb, config.ru, Gemfile, and Gemfile.lock. Navigate to your app’s URL (in this case ‘unique-name.herokuapp.com’) and you should see a greeting! If not, the ‘heroku logs’ command will be of some use.

Moving Forward

Now that you’ve completed the setup process, your imagination is the limit! Go through the Sinatra README to learn about all the crazy stuff you can do with Sinatra. Once you feel comfortable with HTML, try using a templating library like ERB or Haml to make your site actually a site instead of a line of text. Use Heroku’s own PostgresSQL addon to give your application a backing database (and don’t forget to use config-vars to keep your credentials out of your commits) and add an ORM like DataMapper or ActiveRecord to make working with the database easier. Keep in mind that “I just use <ORM>” is no excuse to not learn SQL.