How to Deploy a Ruby On Rails Backend to Heroku

kpete2017

Kyle Petersen

Posted on October 4, 2020

How to Deploy a Ruby On Rails Backend to Heroku

Introduction

Ruby on Rails for many years now has been an extremely popular and influential framework that allows a programmer to build up and deploy a REST API extremely quickly. With that in mind, many developers will choose to deploy using Heroku do to its simplicity and generous pricing for smaller projects.

Getting Started

The first thing we are going to need is PostgreSQL as our database management system. This is Heroku's preferred database management system and Heroku will not work with Ruby on Rail's default sqlite3. Installation instructions for your operating system can be found here.

We also want to download and install the Heroku CLI which can be found here

Before we start getting Heroku set up in our project, we are going to need to make sure that our Ruby on Rails is using PostgreSQL as our database technology instead of its default sqlite3. If you have already built your backend around sqlite3 you can read about migrating it over to PostgreSQL here. Otherwise, if you are just starting a Ruby on Rails backend you can automatically configure it for PostgreSQL by adding the --database=postgresql flag when creating your project like so rails new project-name --api --database=postgresql.

Deploying our Backend

Once your rails backend is ready to be deployed you are going to need to create a Heroku Account. Once finished we need to click on the "new" button in the top right corner of the web page and then click "create a new app" within the drop-down where it will then ask you to pick a name for the app. Name the app and then click "Create App"

Alt Text

Once your app is finished being created you will want to enter your Ruby on Rails directory and then run the command heroku login.

Alt Text

Once logged into your Heroku account from your terminal you are going to run these commands in order.

  • git init
  • heroku git:remote -a <name of your project>
  • git add .
  • git commit -am "any message you want"
  • git push heroku master

Once this series of commands is finished, your code is now deployed up to Heroku. You can run any of your Rails command line commands as long as you preface it with heroku run. For example, if we wanted to create our database within our Heroku server we would run heroku run rake db:create or if we wanted to enter our console we could run heroku run rails console from our command line.

If you wanted to access the address for your newly deployed server you can click on the overview tab and click the open app button located in the top right corner of the webpage and it will take you to the hosted web address.

Alt Text

💖 💪 🙅 🚩
kpete2017
Kyle Petersen

Posted on October 4, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related