DEV Community

Cover image for Deploying Rails Apps with Heroku
rwparrish
rwparrish

Posted on • Edited on

Deploying Rails Apps with Heroku

Deploying your first app is exciting! Let's take a look at what it takes in a Rails case.

Intro

First, a couple of key disctions between a local server and a production server:

  1. A production server needs to be accessible at all times so anyone can go check it out.

  2. The database will not be transmitted so once deployed, you need to create your own production database - only application code gets transmitted.

Quick aside: all of the instructions covered will be for Mac users. No worries Windows users! I will provide links to help guide you to the info you need along the way. This blog also assumes that you are currently using a popular version control system - Git.

Installing Heroku

Once you create your free Heroku account, you'll need the Heroku command-line interface (CLI) tools. It's time to install the Heroku CLI. To do so run the following command:

brew tap heroku/brew && brew install heroku
Enter fullscreen mode Exit fullscreen mode

You can verify the installation went smoothly but running heroku in the command line. If installed correctly you will get the version as well as a list of commands available to you. Windows users check here.

Next, you will need to log into the Heroku account you just created. Run this command:

heroku login
Enter fullscreen mode Exit fullscreen mode

Then follow the instructions in the terminal. It should take you to the browser to log in.

Now, following along with the instructions which can be found here the next step is to create a new rails app. For this blog, I have already created a simple Rails app and I am assuming you have already made one too if you are reading this - I am skipping this step.

Creating Heroku App and Prepping Local Code for Deployment

So, now it's time to deploy the application to Heroku. If you are following along here, you should scroll down until you find the title "Deploy your application to Heroku". Run the following:

heroku create
Enter fullscreen mode Exit fullscreen mode

Great! Now we have a shell app created by Heroku with a link available to us. However, before our application code is deployed we need to get it production-ready which involves setting up our database gem for production.

Ruby comes with SQLite which is fine for development (especially small apps) but not so great for production (it's not a production-ready database) and will not work with Heroku. Heroku uses PostgreSQL as its default. To solve this we will change some things in the Gemfile of our project:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.1'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.3', '>= 6.0.3.4'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~> 1.4'
# Use Puma as the app server
gem 'puma', '~> 4.1'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 4.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
...

group :production do
  gem 'pg'
end

Enter fullscreen mode Exit fullscreen mode

The bit that was added to the Gemfile was:

group :production do
  gem 'pg'
end
Enter fullscreen mode Exit fullscreen mode

The gem for PostgreSQL (aka Postgre) was added.

Now, look back at the Gemfile, or the code snippet of mine above, and notice this line - gem 'sqlite3', '~> 1.4'. At this position in the Gemfile, this gem is made available universally in our app and we no longer want SQLite enabled for production. We want SQLite to be available only for development and test environments. Cut gem 'sqlite3', '~> 1.4' and move it:

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'sqlite3', '~> 1.4'
end
Enter fullscreen mode Exit fullscreen mode

Because we have altered our Gemfile it's time to run bundle install. However, we do not want the gem we added for production - gem 'pg' to be installed locally. So, we run this:

bundle install --without production
Enter fullscreen mode Exit fullscreen mode

This step makes the necessary updates to the Gemfile.lock for production and our application code is ready to be deployed. Now is a good time to make a commit.

Deploy App!

Now it's finally time to push our master/main branch to Heroku:

git push heroku main
Enter fullscreen mode Exit fullscreen mode

A quick note about the code above. If you see an error message that looks like this:

error: src refspec main does not match any
error: failed to push some refs to 'https://git.heroku.com/sheltered-ocean-64484.git'
Enter fullscreen mode Exit fullscreen mode

Try running:

git push heroku master
Enter fullscreen mode Exit fullscreen mode

I won't go into exactly why this happens right now. Just know that some in the industry are changing terminology from "master" to "main".

Once it is working, it is going to deploy to the Heroku app that we created! This step can take a minute or two and when finished you should see a line in the terminal that looks something like this:

remote:        https://sheltered-ocean-64484.herokuapp.com/ deployed to Heroku
Enter fullscreen mode Exit fullscreen mode

Go to that link to check out your deployed app!

Now let's change the auto-generated name Heroku gave our app, "sheltered-ocean-64484" in my case, by running the following from the terminal while in your app's directory:

heroku rename myappname
Enter fullscreen mode Exit fullscreen mode

Now you can go to: http://mynameapp.herokuapp.com/ to view your app!

That's it for now. If we added some more features like database tables and migration files we would need to perform a couple more steps from our terminal. Check here for that info. Scroll to the "Migrate your database" section.

Reacp

This blog covered:

  1. Installing Heroku
  2. Creating a Heroku app
  3. Making the necessary changes to our application code in preparation for deployment
  4. Deploying the app and changing its name

I hope you found this tutorial to be useful. Please ask questions, leave feedback, and share.

Happy coding!

Top comments (0)