RoR Deploy to Heroku

Heroku is a cloud platform that makes deploying Rails applications straightforward. You push your code with Git and Heroku handles servers, infrastructure, and scaling. It is ideal for getting your first real application live quickly without managing Linux servers manually.

What Heroku Does for You

You push code with: git push heroku main
                           |
                           v
Heroku detects Rails app
                           |
                           v
Heroku runs: bundle install
             rails assets:precompile
             rails db:migrate
                           |
                           v
App is live at: https://yourapp.herokuapp.com

Prerequisites

  • A Heroku account (free at heroku.com)
  • Heroku CLI installed
  • Git installed and your app in a Git repository
  • Your app uses PostgreSQL (Heroku does not support SQLite in production)

Switch to PostgreSQL

If your app uses SQLite, switch to PostgreSQL before deploying:

Gemfile:
  gem "pg"                ← add this
  # gem "sqlite3"         ← remove or move to development only

Better Gemfile setup:
  group :development, :test do
    gem "sqlite3"         ← use SQLite locally
  end

  group :production do
    gem "pg"              ← use PostgreSQL on Heroku
  end
bundle install

Install Heroku CLI

macOS:
  brew tap heroku/brew && brew install heroku

Ubuntu:
  curl https://cli-assets.heroku.com/install.sh | sh

Windows:
  Download from devcenter.heroku.com/articles/heroku-cli

Verify:
  heroku --version

Log In to Heroku

heroku login

This opens a browser window to authenticate. Once done, your terminal is connected to your Heroku account.

Create a Heroku App

Inside your project folder:
heroku create myapp-name

Output:
  Creating ⬢ myapp-name... done
  https://myapp-name.herokuapp.com/ | https://git.heroku.com/myapp-name.git

Heroku adds a git remote automatically:
  git remote -v
  heroku  https://git.heroku.com/myapp-name.git (fetch)
  heroku  https://git.heroku.com/myapp-name.git (push)
  origin  https://github.com/you/myapp.git (fetch)

Configure Environment Variables

Set secret keys and API credentials on Heroku using config vars:

heroku config:set RAILS_MASTER_KEY=$(cat config/master.key)
heroku config:set SECRET_KEY_BASE=$(rails secret)
heroku config:set STRIPE_SECRET_KEY=sk_live_your_key
heroku config:set SENDGRID_API_KEY=your_key

View all config vars:
heroku config

Remove a config var:
heroku config:unset OLD_VARIABLE

Deploy Your App

Make sure all changes are committed:
  git add .
  git commit -m "Prepare for Heroku deployment"

Push to Heroku:
  git push heroku main

Heroku build output:
  -----> Building on the Heroku-22 stack
  -----> Detecting buildpack... Ruby
  -----> Installing Ruby 3.2.2
  -----> Running: bundle install
  -----> Running: rails assets:precompile
  -----> Launching...
         Released v3
         https://myapp-name.herokuapp.com/ deployed to Heroku

Set Up the Database

Heroku adds PostgreSQL automatically. Run your migrations:
  heroku run rails db:migrate

Seed initial data:
  heroku run rails db:seed

Open a Rails console on Heroku:
  heroku run rails console

Open Your App

heroku open

This opens your deployed app in the browser. Visit https://myapp-name.herokuapp.com.

View Logs

heroku logs --tail

This streams live logs from your running app. Use it to debug crashes and errors in production.

Heroku Procfile

A Procfile tells Heroku which processes to run. Create one in your project root:

Procfile:

web: bundle exec puma -C config/puma.rb
worker: bundle exec sidekiq -C config/sidekiq.yml

The web process handles HTTP requests. The worker process runs Sidekiq for background jobs.

Add-ons

Heroku provides add-ons for common services:

Add PostgreSQL (free tier):
  heroku addons:create heroku-postgresql:essential-0

Add Redis (for Sidekiq / caching):
  heroku addons:create heroku-redis:mini

Add scheduled tasks (like cron):
  heroku addons:create scheduler:standard

View all add-ons:
  heroku addons

Updating Your App

Every update follows the same steps:
  1. Make changes locally
  2. Test everything
  3. git add . && git commit -m "Add feature X"
  4. git push heroku main
  5. heroku run rails db:migrate  (if you added a migration)
  6. heroku open                  (check it works)

Heroku Deployment Checklist

CheckCommand
PostgreSQL gem addedCheck Gemfile
RAILS_MASTER_KEY setheroku config:set RAILS_MASTER_KEY=...
Migrations runheroku run rails db:migrate
Assets compiledAutomatic during git push
App loads without errorsheroku logs --tail

Heroku removes the complexity of server management and lets you focus on building your app. Once you understand the deploy cycle — commit, push, migrate, verify — you can deploy new features in under two minutes.

Leave a Comment

Your email address will not be published. Required fields are marked *