How to deploy a built Rails app with SQlite3 to Heroku

At Flatiron Software Engineer Intensive Bootcamp, we have worked on many projects, especially big projects after each module. In module 2, we learned how to build a Rails app with full features. I and my partner built a great app for travelers for a project of module 2. With Bootstrap supported on Rails, our Rails app looks like a modern website. That’s why after finishing the project, I was excited to deploy it on Heroku to share my project with everyone. But we all know, Heroku only supports deploying an app using Postgresql database. That is the experience I want to share in this blog.

There are 3 main steps in deploying a built Rails app!

Step 1: Local Setup

First of all, go to the root directory of your app in the terminal, install Heroku

$ brew tap heroku/brew

$ brew install heroku

After successfully installed Heroku, you need to login to your Heroku account

heroku login

It will bring you to your browser for login or sign up for a Heroku account

After successful login, your terminal will show a message like: “Logged in as ${your_email}”. If you see that message, you are good to go.

Step 2: Create a Heroku app

Heroku relies on Git. So before starting your Heroku app, you need to initialize and commit your code to Git. Do you remember the way to set up a repository?

$ git init #Initializing a new repository

$ git add . #Add any new or modified files to the working tree

$ git commit -m “init” #Commit the added files to working tree

Now this time to create a Heroku app by running this

$ heroku create

Yay, your Heroku app has just been created. Now you can go to the above URL (whatever URL Heroku created for you), you will see the greeting message. In this case, the URL of my app is https://hidden-oasis-79821.herokuapp.com/. (By the time you read this blog, it has nothing here because I change the URL at the end of the blog.)

While writing this blog, the latest stack is Heroku-20 which supports ruby-2.6.6. If your ruby version is different, change it on your Gemfile file at the app’s root directory and also delete Gemfile.lock file. Then “bundle install” again.

If your app is using Postgresql, you can move to Step 3. Otherwise, we need to change our environment before we go.

In your Gemfile, you will see a gem name ‘sqlite3’, delete it, and add gem ‘pg’ (PostgreSQL) like the above picture. Besides, we also need to change “config/databse.yml” file in your root directory like below.

The reason for changing the database in the production part is because, in Heroku, we already have the add-on in place database Postgresql. If we don’t change that, we can not run the rails setup on the Heroku remote.

Step 3: Deploy your application

Then we ready to deploy our code:

$ git push heroku master

Depend on the project you are deploying uses “master” or “main”. If the default branch name is main, you “git push Heroku main” instead.

Now we need to migrate our database to Heroku remote server

$ heroku run rake db:migrate

Congratulations you deploy successful your application. You can open our Heroku website by type

$ heroku open

But now probably you don’t like your URL, you can definitely change it.

Go to Heroku dashboard and log in, you will see all your applications here. Click to the app you just built, then setting.

You will see it like this. But be careful, don’t change your app name here, it can break your git remotely.

Go to your app’s root directory, type

$ heroku apps:rename newname

And tada tada, see we have now

NOTE: If your app uses Bootstrap and it is not rendered correctly on Heroku, go to “config/environments/production.rb” file and change as below:

And don’t forget to “git add”, “git commit”, and “git push Heroku master” each time you change or modify any file in your local machine to update it on Heroku.

Feel free to check out my app deployment on Heroku and Github repo.

Happy Coding!

Full Stack Developer Connect with me here: https://www.linkedin.com/in/chi-nguyen-swe/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store