EvanRPavone
Posted on June 3, 2021
So you finished your rails application and you want everyone to be able to experience your masterpiece. This is where Heroku comes in. Heroku is an application hosting service that is free to use and is very helpful for those who want to demo and show off their applications. This guide will walk you through on how to install the HerokuCLI and how to push your code to go live. Before we dive into setting up and hosting your Rails Application, make sure you have your github repository for your project setup using Git Version Control, this is the link I use to set it up.
Installation
Before we begin the installation process, I would go ahead and make a Heroku account. Installing the Heroku Command Line Interface (CLI) can be easy but if you run into errors, googling the errors will give you the fixes you are looking for. To begin, go to the Heroku Dev Center to get the installation instructions for installing the command line interface. Installing this CLI will allow your terminal to connect to Heroku. Depending on what system you are running (MacOS, Windows, Linux), choose your installation process and go through the steps. I am on mac so I would copy the line of code,
brew tap heroku/brew && brew install heroku
, and paste it into my terminal. Follow any prompts it asks you and you shouldn’t have any issues. To check if you have heroku installed correctly, in your terminal type heroku --version
, if a version pops up, you have HerokuCLI installed!
Hosting
First things first, login to heroku in your terminal heroku login
, it will prompt you to press any key to open your browser, do it. Once you are logged in, your terminal will say logged in as (your email). Now you need to create your app by running heroku create
, when you run this command, it will give you the generated link to your application. There is nothing on your page yet because we haven’t pushed your code to Heroku yet. If you want to rename your domain, heroku rename (whatever name you want)
. Now that you created your heroku application, we need to add your ssh keys to heroku. To do this, put heroku keys:add
and follow the prompts, easy enough. Now we need to go into your project gemfile and find sqlite3 (should be at the top), if you don’t see sqlite3 and see pg then you don’t really have to do anything to your gemfile. If you have sqlite3, move it into your development group in your gemfile and put gem ‘pg’ …
where sqlite3 originally was. This is your database, heroku can’t use sqlite3 and postgresql helps with the database for production, make sure you bundle install
and commit your changes to your github repo. You are now ready to push your code to heroku, to do this, put git push heroku (whatever branch, either main/master)
, when you hit enter it will take some time and you will probably run into some errors like You are trying to install ruby-2.6.3 on heroku-20.
Don’t worry, all you need to do is set your stack to heroku-18 by pasting this into your terminal heroku stack:set heroku-18
and then try to push your code to heroku again. If you think that your terminal has froze when pushing to heroku, it hasn't, it just takes some time. Once it is done, your application is now hosted but you aren’t done yet. We need to migrate our database by running heroku run rails db:migrate
. Congrats you have hosted your rails application to Heroku!
Heroku Rails Console
Let’s say you have devise for your application and you want to give admin permission to a user. In order to do this, you need to run heroku run rails console
. This will give you your normal rails console but it will be connected to your heroku application. So if you need add a user to admin just put User.find(1).update(admin: true)
into your console. If you want to do other rails stuff just run heroku run rails (whatever). Thanks for taking the time to read through this! I hope it helped and good luck on your future projects!
Posted on June 3, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.