# Rails Tutorial: Deploy to Heroku

Setting up the Application for deployment:

Before pushing to Heroku, we need to build the project for production. To do that, run the following command from your terminal with rails-todolist as your working directory.

$ npm run build:prod -s

Then we need to change our Database settings to make use of Postgresql instead of Sqlite3 as Heroku does not allow you to use Sqlite3 for your backend database. First, please install Postgres locally for your particular development platform: https://devcenter.heroku.com/articles/heroku-postgresql#local-setup

Once you have installed Postgres, the changes that are needed to be made are as follows:

Edit rails-todolist/Gemfile and change this line:

gem 'sqlite3'

to this:

gem 'pg'

Then open your rails-todolist/config/database.yml in your favorite editor.

You will need to change the adapter from

adapter: sqlite3

to

adapter: postgresql

then, change the database: to a custom name like so:

database: my_database_production

A final version might look something like this:


default: &default
  adapter: postgresql
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: my_database_development

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: my_database_test

production:
  <<: *default
  database: my_database_production

At the end of the rails-todolist\Gemfile add this line:

ruby "2.3.1"

Once you have done that, run:

bundle install

Once the above command finishes, initialize a git repository for your project like so:

$ git init
$ git add .
$ git commit -m 'first commit'

Deploying to Heroku

Make sure you are in the root of the rails-todolist directory. From within that directory run:

$ heroku create

You can verify that the remote was added to your project by running:

$ git config --list | grep heroku

Then set the buildpack to ruby by running the following command:

$ heroku buildpacks:set heroku/ruby

Then deploy your code:

$ git push heroku master

Migrate your database

You can migrate by running the following command:

$ heroku run rake db:migrate

Visit your application

You’ve deployed your code to Heroku. You can now instruct Heroku to execute a process type. Heroku does this by running the associated command in a dyno, which is a lightweight container that is the basic unit of composition on Heroku.

Let’s ensure we have one dyno running the web process type:

$ heroku ps:scale web=1

We can now visit the app in our browser by running the following command:

$ heroku open