The Ultimate Heroku Deployment guide For Rails applications

Salma Elmasry
4 min readMay 19, 2018

--

Introduction

It is important to get feedback rom other developers after developing your application. However, if your application is heavy coded, it will be hard for them to understand and troubleshoot it. Heroku is a cloud hosting platform that allows you to deploy and host your application, and create a URL that you can share with anyone. Therefore, using Heroku to develop your application make it easier to get feedback from other developer, and thus improve your application and make it more robust. In this post, I will walk you through how to deploy your rails (or Sinatra) app to Heroku after developing it locally.

Note: the steps are the same for deploying your sinatra app

Understanding Heroku environment

When you start developing your application, the environment assumes that you are using “Sqlite3". The problem that the database that is used for production in Heroku is “Postgresql”, not Sqlite3. The advantage of using postgresql is that it is more mature and robust . You can ask your environment at the beginning of creating an app to use postgresql by running

$rails new <app-name> --database=postgresql

this will solve most of the issues in the deployment process. The next option is to modify your database.yml and your gemfile, which I will demonstrate later.

Must do it before start deploying

  1. Your application folder should be a git repository, if not, please do the following steps in the terminal:
$cd <Your-app-folder>
$git init
$git add .
$git status
$git commit -m "Your message"
  1. Create an account at heroku — link.
  2. Install heroku CLI. If you are on mac, run the following command at your terminal.
$brew install heroku/brew/heroku

Otherwise, please follow the instructions at this link.

3. To login to heroku from the terminal, run the following command line

$heroku login

After that, it will ask you to enter your credentials — Email and password.

Adjusting the database to be compatible with heroku

We will sort our Gemfile into three gem categories: development, test, and production. Both development and test will use sqlite gem. The production will use postgresql — gem pg. Alternatively, you may set the three ge categories to postgresql, it’s up to you.

  1. In your Gemfile add the following lines
# the development block
group :development, :test do
gem 'sqlite3'end
# the production block
group :production do
gem 'pg'end

2. Save your file, then run

$bundle install

3. Open up your config folder, then open database.yml. Your file will looks like that:

SQLite version 3.xgem install sqlite3Ensure the SQLite 3 gem is defined in your Gemfilegem 'sqlite3'default: &defaultadapter: sqlite3pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>timeout: 5000development:<<: *defaultdatabase: db/development.sqlite3# 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:<<: *defaultdatabase: db/test.sqlite3

4. Replace the previous lines and paste the following lines instead. Then, save it.

development:adapter: mysql2encoding: utf8database: my_app_developmentpool: 5username: rootpassword:test:adapter: sqlite3database: db/development.sqlite3pool: 5timeout: 5000production:adapter: postgresqldatabase: my_database_productionpool: 5timeout: 5000

5. Stage your changes and commit it to your remote.

For sinatra, the config folder doesn’t have the database.yml, you have to create it.

$git add .
$git commit -m "Adjust database.yml and gemfile for heroku deployment."

The deployment process

At this point, your application is ready to be deployed without any problems. To start your application, run the following:

$heroku create   #this is the heroku remote branch

Heroku will create a brand new branch with a random name — ex. damp-wave-35943

Next, push your application directory to heroku by running

$git push heroku master

Now, your terminal will load all your files to heroku. It may take a while to finish the uploading depending on your application size.

Then, you have to migrate your tables into heroku.

$heroku run rake db:migrate 

Now run

$heroku restart$heroku open

You can see your application running in the web browser. Congratulations!

Some Issues you may face

If you create a heroku remote before login into heroku through the terminal, you may face an issue in the deploying process. You may encounter some errors like:

fetal: you don't have access to this remote

So, the best way I found was to delete your remote then login and recreate your remote. To do so, follow the following two steps

To delete heroku branch, run the following command line.

# Check your branches
$git remote -v
# you should see :origin
heroku
$git remote rm heroku# check branches again$git remote -vorigin

Conclusion

As a beginner developer, it is important to have feedback about your application in terms of the performance, the routing, the breaking registrations, etc.. If you had any issue with any of these processes, it will be difficult to be figured out by solely allowing other developers to revise it. Instead, most of the issues will appear while other people use the application. That’s why it is useful to deploy your application in Heroku to get comments from your peers before lunching it to public. Hope this post help you get your app deployed on heroku successfully, Please follow me on twitter @salmaeng

--

--

No responses yet