Other Pages

Expand All

Deploying To Heroku

Begin by saving your code in a version control system called git: Add the Project to a Git RepoThen, if you haven't yet deployed to heroku, start at First-time setup. Otherwise, start at Every time.

First-time setup

Step 1: Sign up for heroku

Go to Heroku's webpage and sign up for a free account.

Step 2: Create a heroku application

Type this in the terminal:

Terminal
heroku create

This command will ask you for your credentials - type in your Heroku email and password, hitting Enter after each one.

heroku create registers a new application on Heroku's system. You should see some output including your new app's URL, which will look like http://sushi-island-6128.herokuapp.com/ .

Step 3: Create a Heroku key

Creating a Heroku key will let Heroku know who is creating the app.

Type this in the terminal:

Terminal
heroku keys:add

The output will look like this:

Could not find an existing public key.
Would you like to generate one? [Yn]

Type "Y" for "Yes" and hit "Enter".

The output will look like this:

Generating new SSH public key.
Uploading SSH public key /home/vagrant/.ssh/id_rsa.pub... done

Great, now you have a Heroku key!

Step 4: Edit the Gemfile

Each application has its own Gemfile. Be sure you're opening the one inside your app's folder.

Heroku will run our application slightly differently than our development computer does, which requires us to make a small change to our Gemfile.

Open the file called Gemfile in Sublime Text (or your preferred editor) and find the line containing:

gem 'sqlite3'

Remove that line and replace it with:

group :development, :test do
  gem 'sqlite3'
end

group :production do
  gem 'pg'
  gem 'rails_12factor'
end

Step 5: Apply the Gemfile changes

Type this in the terminal:

Terminal
bundle install --without production

Every time the Gemfile changes, you need to run bundle install for the changes to be processed. The processed version of the changes is stored in another file called Gemfile.lock.

Step 6: Commit the Gemfile changes

There are now changes to Gemfile and Gemfile.lock that need to be committed before we can push to heroku.

Type this in the terminal:

Terminal
git add .
git commit -m "Changed Gemfile for heroku"

There is a period after the word add in the first line.

Every time

Step 1: Commit any pending changes to git

Heroku will only receive the files we've committed into our local git repository. So we need to make sure all changed files have been committed.

Type this in the terminal:

Terminal
git status

git status shows you any pending changes you've created. If it has no output, you're already ready to deploy! Otherwise...

Type this in the terminal:

Terminal
git add .
git commit -m "Some helpful message for your future self"

Your commit message should reference whatever your outstanding changes are: something like 'added votes to the topics index'.

Step 2: Push changes to heroku

Type this in the terminal:

Terminal
git push heroku master

This takes all changes you've committed locally and pushes them to heroku.

Step 3: Run database migrations on Heroku

Type this in the terminal:

Terminal
heroku run rails db:migrate

This tells Heroku to run your migrations on its database, like running rails db:migrate locally. Heroku's database is separate from the one on your computer, which means it needs to be updated every time you make changes to the structure of your database. It also means that you'll not see any of the data you entered into the sqlite3 database on your computer.

Step 4: Visit your application

Type this in the terminal:

Terminal
heroku open

This displays the URL of your application to open in your browser.

Explanation

First, we had to do some work to make Heroku happy with our application. This required updating the Gemfile and bundling.

  • The Gemfile is a list of all the Ruby libraries your application needs. What we've declared here is that we want to use the sqlite3 library while we're developing on our computer (the development group) but when deploying to heroku (the production group) we want to use the pg library, which is made for the type of database that heroku uses.

  • Bundler is how Ruby projects keep track of the gems that they use. We told bundler what we wanted to use in the Gemfile, now we need to make sure those gems are installed. Since we don't have the type of database heroku does, we skip the production gems. Don't worry though! Bundler still logs them so Heroku will install them when they get your code.

You should be able to deploy your application any time it's in a good, working state. Your typical workflow will look like:

Diagram showing git workflow of making changes, committing them, and pushing to Heroku.
  1. Add or change some code

  2. Commit your changes (git commit)

  3. (repeat)

Any time your changes are committed, you should feel free to git push heroku master and boom! Your changes are live!