This guide is for a first-time Rails developer to deploy their app to Heroku, a popular web-hosting service with strong Rails support. This guide assumes you already have a Heroku account and have installed the Heroku Toolbelt.
- Make sure you've setup an SSH key for Heroku. Follow this simple guide to create an SSH key and send it to Heroku if needed: Heroku: Managing Your SSH Keys
- Navigate into the folder for your Rails app.
- Run
$ heroku create <optional app name>
. If you are asked to login with username and password, SSH isn't setup. Follow the wizard and reference step 1 of this guide for help, if needed. - Verify your heroku git remote was created by running
$ git remote
. You should seeheroku
as one of your remotes.
-
Navigate to your Gemfile.
-
Specify
group: :development
for your SqLite3 gem and add the Postgres gem (pg
) below SQLite3 withgroup: :production
. Your Gemfile should look like this:source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.1.6' # Use sqlite3 as the database for Active Record gem 'sqlite3', group: :development # Added development group. gem 'pg', group: :production # Added postgres and made it production only. # Use SCSS for stylesheets gem 'sass-rails', '~> 4.0.3' ...
-
Add the 'rails_12factor' gem to your Gemfile. This is a gem made by Heroku to make tweaks to your app for deployment on their platform. Your Gemfile should now (and finally) look like this:
source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.1.6' # Use sqlite3 as the database for Active Record gem 'sqlite3', group: :development # Added development group. gem 'pg', group: :production # Added postgres and made it production only. gem 'rails_12factor' # Use SCSS for stylesheets gem 'sass-rails', '~> 4.0.3' ...
-
Run
bundle install
and commit those changes to git. (git add .
thengit commit -m "<your commit message>"
). If you get an error bundling thepg
gem follow this guide to install Postgres locally using Herkou's Postgres.app: Heroku Postgres - Set up Postgres on Mac
- Make sure you've successfully bundled your dependencies with bundler and you've committed all your changes to git.
- Run
$ git push heroku master
. This will deploy the master branch of your git repository to Heroku. - If you get a successful message after Heroku finishing deploying, you're good! You can open your app in your browser with
$ heroku open
. - You'll probably notice an error. This is because your database isn't setup yet. To do that, run
$ heroku run rake db:migrate
. This will runrake db:migrate
remotely on your Heroku server. - Done! Return to the browser and bask in the glory of your code being run on the world-wide web.
- To use Rails Console on your server, run
$ heroku run rails console
. It will be a little slower because you're running a console in a computer across the country, but it's the same as running Rails Console on your local machine. - To add a better, custom vanity domain to your server, follow these instructions: Heroku: Custom Domains
- Although we are using SQLite3 locally and Postgres on Heroku, this can result in some issues once your project gets larger and more sophisticated. As a best practice, all developers working on production code (and not hobby projects) insist on running the same DB locally and in production. You can setup your machine to run and use Postgres by following these instructions: Heroku Postgres. Once setup, you can make all new Rails app with PG from the get-go by using this Rails command:
$ rails new myapp --database=postgresql
.
"gem 'sqlite3', group: :development # Added development group." =>This thing doesn't work now
I've updated it in my fork.