Hurray! Using Ruby on Rails, you've built the world's best and one-of-a-kind app that you know everyone would love. The one problem is how to get everyone to be able to use it. Fear not! Today we're going to go into deploying your super sweet Rails app to Heroku!
Project Setup
This is very important... In order to deploy to Heroku, you must be using PostgreSQL. Basically, Sqlite3 uses database files (marked by *.db files) to store the database information; however, PostgreSQL uses a client-server model to store the database. In short, PostgreSQL can support simultaneous connections to the database while Sqlite3 cannot. If you're interested in reading more on Sqlite3 vs PostgreSQL, you may find the article here interesting.
If you have already created your app using Sqlite3, you may follow the instructions by Mohammad Khan here to convert it.
If you would like to remake your project using PostgreSQL, you can set up a new project to use PostgreSQL using this article by Alex Ghiculescu.
Heroku CLI Setup
Alright. Now that we're using PostgreSQL with our Rails application, we now need to install the Heroku CLI. To do this, use the installation instructions provided here. If you're on Mac, the easiest method to installing the Heroku CLI is through Homebrew. If you do not have Homebrew installed on your machine, use the following terminal command to install it.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Once installed, you can use the brew installation method shown in the Heroku installation instructions.
Quick note: Since I have not used Windows to instantiate Heroku Rails apps, I will not provide instructions on using the Heroku CLI in Windows.
Sample project
Just in case you don't have your own Rails application at the ready, I have created a small app that you may use to practice deploying to Heroku. This app is super simple and only displays superheroes and their super powers. I call it superheroku-app, and you may find it here. Just fork and clone this application to your computer.
Heroku Deployment
Once you've got your app ready, and it's using PostgreSQL, you are now able to deploy to Heroku. First, navigate to the root of your Rails project in your terminal.
If you have not done so already, be sure to create an account with Heroku. You may do so here.
Now that that's done, we need to log into Heroku on the CLI. Make sure you're in the root directory of your Rails app and run the following in your terminal:
heroku login
This should prompt you to press any key to open up a browser to login. Follow the login instructions, and you should be greeted with a login success message in the terminal.
Now we must create our Heroku app. To do this run:
heroku create [APP_NAME]
"APP_NAME" is whatever you would like your app to be called. After your app is created, you will be able to access it by going to [APP_NAME].herokuapp.com, so make sure to name your app something that makes sense. Alternatively, you can leave the app name blank and Heroku will assign you a random app name.
Next, we need to push our master branch to Heroku. To do this, run:
git push heroku master
This could take some time as Heroku is setting up your Rails application on its server system. This includes all gem requirements that you have in your gem file as well as some overhead installations. After everything finishes, you should see something similar to:
Hurray! We have officially deployed our app to Heroku. However, out of the gate, your app will not be working correctly. This is because we have to migrate the database and seed it. To do this, first run:
heroku run rails db:migrate
then run:
heroku run rails db:seed
For each of these commands, Heroku will display the response similarly to how Rails does when running database commands locally in your terminal. Therefore, all of the response messages (commits, migrations, etc.) should be fairly familiar to you.
Quick note: "heroku run" allows you to run terminal commands very similar to running them on your local machine. "heroku run" can be very powerful, but be careful when using this as you are running scripts on your Heroku instance.
Heroku Troubleshooting
Unfortunately, there's a chance that something may go wrong while migrating or seeding your database. When this happens, you can the following in your terminal to view your Heroku logs.
heroku logs --tail
Heroku logs can be daunting at first; however, they will quickly become your best friend as you deploy more Heroku apps.
Also, you may dive into a console session with the Heroku. Just run the following, and you can test your Rails application in the console just like you would on your own machine.
heroku run rails c
A Quick Note on Free Heroku
Heroku is a wonderful tool to quickly and easily get your applications running; however, there are some drawbacks with this service. The main drawback comes with Heroku's free tier instances (called Dynos) falling asleep after 30 minutes of no use. Because of this, connecting to your Heroku app may initially take 15 seconds or so. Heroku does offer a solution to this; however, it is by joining a paid tier plan.
If you really need your Heroku app to be live 24/7 but still do not want to pay, there is an app called Kaffeine that will wake your application for you every 30 minutes or so. You may find that app here. Be careful when doing this. While this sounds great, Heroku only gives you a set pool of free hours per month that all your Dynos may use. After you use your hours, you Dynos are put to sleep until the next month. Only use the Kaffeine service if you have one app, or if you only wish to keep it awake for a particular amount of time.
Conculsion
I hope this post has inspired you to start deploying your rails applications. Once you get the hang of it, the process is fairly straight forward and easy to do. I hope you now have the confidence and expertise to start deploying your super awesome Rails applications.
Top comments (0)