Getting Started on Heroku with Rails 6.x
Last updated December 05, 2024
This article is a work in progress, or documents a feature that is not yet released to all users. This article is unlisted. Only those with the link can access it.
Table of Contents
- Local Setup
- Create a New or Upgrade an Existing Rails App
- Add the pg Gem
- Create a Welcome Page
- Specify the Ruby Version
- Create a Procfile
- Store The App in Git
- Fix a babel regression
- Create a Heroku App
- Provision a Database
- Deploy the App to Heroku
- Migrate The Database
- Scale and Access the Application
- View Application Logs
- Optional Steps
- Rails asset pipeline
- Remove Heroku Gems
- Troubleshooting
- Next Steps
- Deleting Your App and Add-on
This article is archived. It is no longer receiving updates. It is presented here for historical reference only. We cannot guarantee that any statements made are still true or that the instructions will still work. This version of Rails is no longer supported by Rails core. If you are starting a new application, we recommend you use the most recently released version of Rails.
The latest version of Rails available is Rails 8. If you’re starting a new application, we recommend you use the most recently released version.
Ruby on Rails is a popular web framework written in Ruby. This guide covers using Rails 6 on Heroku. For information on running previous versions of Rails on Heroku, see the tutorial for Rails 5.x or Rails 4.x.
The tutorial assumes that you have:
- Basic familiarity with Ruby/Rails and Git
- A locally installed version of Ruby 2.5.0+, Rubygems, Bundler, and Rails 6+
- A locally installed version of the Heroku CLI
- A verified Heroku Account
- A subscription to the Eco dynos plan (recommended)
Using dynos and databases to complete this tutorial counts towards your usage. We recommend using our low-cost plans to complete this tutorial. Eligible students can apply for platform credits through our new Heroku for GitHub Students program.
Local Setup
After installing the Heroku CLI, log in through your terminal:
$ heroku login
heroku: Press any key to open up the browser to login or q to exit
› Warning: If browser does not open, visit
› https://cli-auth.heroku.com/auth/browser/***
heroku: Waiting for login...
Logging in... done
Logged in as me@example.com
This command opens your web browser to the Heroku login page. If your browser is already logged in to Heroku, click the Log in
button on the page.
This authentication is required for the heroku
and git
commands to work correctly.
Create a New or Upgrade an Existing Rails App
Ensure that you’re using Rails 6.x by running rails -v
. If necessary, install it with this command:
$ gem install rails -v 6.1.7.3 --no-document
Successfully installed rails-6.1.7.3
1 gem installed
Create a Rails app:
$ rails _6.1.7.3_ new myapp --database=postgresql
Move into the application directory and add the x86_64-linux
and ruby
platforms to Gemfile.lock
.
$ cd myapp
$ bundle lock --add-platform x86_64-linux --add-platform ruby
Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies.....
Writing lockfile to ./myapp/Gemfile.lock
Create a local database:
$ bin/rails db:create
Database 'myapp_development' already exists
Database 'myapp_test' already exists
Add the pg Gem
For new or existing apps where --database=postgresql
isn’t defined, confirm the sqlite3
gem doesn’t exist in the Gemfile
. Add the pg
gem in its place.
gem 'sqlite3'
To this:
gem 'pg'
Heroku highly recommends using PostgreSQL locally during development. Maintaining parity between development and deployment environments prevents introducing subtle bugs due to the differences in environments.
Install Postgres locally. For more information on why Postgres is recommended instead of Sqlite3, see why Sqlite3 is not compatible with Heroku.
With the Gemfile
updated, reinstall the dependencies:
$ bundle install
The installation also updates Gemfile.lock
with the changes.
In addition to the pg
gem, ensure that config/database.yml
defines the postgresql
adapter. The development section of config/database.yml
file looks something like this:
$ cat config/database.yml
# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
# gem install pg
# On macOS with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS with MacPorts:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: myapp_development
# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user running Rails.
#username: myapp
# The password associated with the postgres role (username).
#password:
# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost
# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432
# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public
# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# Defaults to warning.
#min_messages: notice
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: myapp_test
# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
# production:
# url: <%= ENV['MY_APP_DATABASE_URL'] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
Be careful here. If the value of adapter
is postgres
and not postgresql
, the application won’t work.
Create a Welcome Page
Rails 6 no longer has a static index page in production by default. When you’re using a new app, there isn’t a root page in production, so you must create one. Create a controller called welcome
for the home page:
$ rails generate controller welcome
In file app/views/welcome/index.html.erb
write:
<h2>Hello World</h2>
<p>
The time is now: <%= Time.now %>
</p>
Create a Rails route to this action. Edit config/routes.rb
to set the index page to the new method:
In file config/routes.rb
, on line 2 add:
root 'welcome#index'
You can verify that the page is there by running your server:
$ rails server
Visit http://localhost:3000 in your browser. If you don’t see the page, use the logs to debug. Rails outputs logs in the same terminal where rails server
started.
Specify the Ruby Version
Rails 6 requires Ruby 2.5.0 or above. Heroku has a recent version of Ruby installed by default. Specify an exact version with the ruby
DSL in Gemfile
. For example:
ruby "3.0.6"
Always use the same version of Ruby locally. Confirm the local version of ruby with ruby -v
. Refer to the Ruby Versions article for more details on defining a specific ruby version.
Create a Procfile
Use a Procfile, a text file in the root directory of your application, to explicitly declare what command to execute to start your app.
This Procfile declares a single process type, web
, and the command needed to run it. The name web
is important here. It declares that this process type is attached to Heroku’s HTTP routing stack and receives web traffic when deployed.
By default, a Rails app’s web process runs rails server
, which uses Puma in Rails 7. When you deploy a Rails 7 application without a Procfile, this command executes. However, we recommend explicitly declaring how to boot your server process via a Procfile.
In file Procfile
write:
web: bundle exec puma -C config/puma.rb
The Procfile
filename is case sensitive. There is no file extension.
If config/puma.rb
doesn’t exist, create one using Heroku’s Puma documentation for maximum performance.
A Procfile can contain additional process types. For example, you can declare a background worker process that processes items off a queue.
Store The App in Git
Heroku relies on Git, a distributed source control management tool, for deploying applications. If the application is not already in Git, first verify that git
is on the system with git --help
:
$ git --help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
[--super-prefix=<path>] [--config-env=<name>=<envvar>]
If the command produces no output or command not found
, install Git.
Navigate to the root directory of the Rails app. Use the ls
command to see its contents:
$ ls
Gemfile
Gemfile-e
Gemfile.lock
Procfile
README.md
Rakefile
app
babel.config.js
bin
config
config.ru
db
lib
log
node_modules
package.json
postcss.config.js
public
storage
test
tmp
vendor
yarn.lock
Within the Rails app directly, initialize a local empty Git repository and commit the app’s code:
$ git init
$ git add .
$ git commit -m "init"
Verify everything committed correctly with git status
:
$ git status
On branch main
nothing to commit, working tree clean
With the application committed to Git, it’s ready to deploy to Heroku.
Fix a babel regression
Rails 6 relies on the webpacker gem which uses babel as a dependency. Version 7.22.0 of babel released a breaking change in a minor version that caused new Rails 6.1.x apps to break. Rails 6 and webpacker are no longer receiving bug fixes so unless babel reverts the change, then you will need to workaround this bug in your new app.
Test to see if your application is affected by this bug by running this command:
$ rake webpacker:clobber webpacker:compile 2>&1 | grep "Cannot find package '@babel/plugin-proposal-private-methods'"
Error: Cannot find package '@babel/plugin-proposal-private-methods' imported from ./myapp/babel-virtual-resolve-base.js
To fix this issue modify babel.config.js
, replace plugin-proposal-private-methods
with plugin-transform-private-methods
:
- '@babel/plugin-plugin-private-methods',
+ '@babel/plugin-transform-private-methods',
Verify that it works correctly:
$ rake webpacker:clobber webpacker:compile
Commit the fix to git:
$ git add .
$ git commit -m "fix babel error"
Create a Heroku App
Using a dyno and a database to complete this tutorial counts towards your usage. Delete your app, and database as soon as you’re done to control costs.
To create an app on Heroku, use the Heroku CLI Inside the Rails app’s root directory:
$ heroku create --stack heroku-20
Creating app... done, serene-fortress-54097, stack is heroku-20
https://serene-fortress-54097.herokuapp.com/ | https://git.heroku.com/serene-fortress-54097.git
When you create an app, a git remote called heroku
is also created and associated with your local git repository. Git remotes are versions of your repository that live on other servers. You deploy your app by pushing its code to that special Heroku-hosted remote associated with your app. Verify the remote is set with git config
:
$ git config --list --local | grep heroku
remote.heroku.url=https://git.heroku.com/serene-fortress-54097.git
remote.heroku.fetch=+refs/heads/*:refs/remotes/heroku/*
If the current directory is incorrect or Git isn’t initialized, Git returns fatal: not in a git directory
. If Git returns a list of remotes, it’s ready to deploy.
Following changes in the industry, Heroku has updated our default git branch name to main
. If the project you’re deploying uses master
as its default branch name, use git push heroku master
.
Provision a Database
Provision a Heroku Postgres database, one of the add-ons available through the Elements Marketplace. Add-ons are cloud services that provide out-of-the-box additional services for your application, such as logging, monitoring, databases, and more.
An essential-0
Postgres size costs $5 a month, prorated to the minute. At the end of this tutorial, we prompt you to delete your database to minimize costs.
$ heroku addons:create heroku-postgresql:essential-0
Creating heroku-postgresql:essential-0 on ⬢ serene-fortress-54097... ~$0.007/hour (max $5/month)
Database should be available soon
postgresql-acute-36546 is being created in the background. The app will restart when complete...
Use heroku addons:info postgresql-acute-36546 to check creation progress
Use heroku addons:docs heroku-postgresql to view documentation
Your Heroku app can now access this Postgres database. The DATABASE_URL
environment variable stores your credentials, which Rails connects to by convention.
Deploy the App to Heroku
Using a dyno to complete this tutorial counts towards your usage. Delete your app as soon as you’re done to control costs.
Deploy your code. This command pushes the main
branch of the sample repo to your heroku
remote, which then deploys to Heroku:
$ git push heroku main
remote: Updated 101 paths from 59c4854
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Building on the Heroku-20 stack
remote: -----> Determining which buildpack to use for this app
remote: ! Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used.
remote: Detected buildpacks: Ruby,Node.js
remote: See https://devcenter.heroku.com/articles/buildpacks#buildpack-detect-order
remote: -----> Ruby app detected
remote: -----> Installing bundler 2.3.25
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-3.0.6
remote: -----> Installing dependencies using bundler 2.3.25
remote: Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4
remote: Fetching gem metadata from https://rubygems.org/..........
remote: Fetching rake 13.0.6
remote: Installing rake 13.0.6
remote: Fetching zeitwerk 2.6.8
remote: Fetching builder 3.2.4
remote: Fetching concurrent-ruby 1.2.2
remote: Fetching minitest 5.18.0
remote: Installing concurrent-ruby 1.2.2
remote: Installing minitest 5.18.0
remote: Installing zeitwerk 2.6.8
remote: Fetching erubi 1.12.0
remote: Fetching mini_portile2 2.8.2
remote: Installing erubi 1.12.0
remote: Fetching racc 1.6.2
remote: Installing mini_portile2 2.8.2
remote: Fetching crass 1.0.6
remote: Fetching rack 2.2.7
remote: Installing racc 1.6.2 with native extensions
remote: Installing builder 3.2.4
remote: Fetching nio4r 2.5.9
remote: Installing rack 2.2.7
remote: Installing crass 1.0.6
remote: Fetching websocket-extensions 0.1.5
remote: Installing nio4r 2.5.9 with native extensions
remote: Fetching marcel 1.0.2
remote: Installing marcel 1.0.2
remote: Installing websocket-extensions 0.1.5
remote: Fetching mini_mime 1.1.2
remote: Fetching date 3.3.3
remote: Installing date 3.3.3 with native extensions
remote: Installing mini_mime 1.1.2
remote: Fetching timeout 0.3.2
remote: Installing timeout 0.3.2
remote: Fetching msgpack 1.7.1
remote: Installing msgpack 1.7.1 with native extensions
remote: Using bundler 2.3.25
remote: Fetching ffi 1.15.5
remote: Installing ffi 1.15.5 with native extensions
remote: Fetching method_source 1.0.0
remote: Installing method_source 1.0.0
remote: Fetching pg 1.5.3
remote: Installing pg 1.5.3 with native extensions
remote: Fetching thor 1.2.2
remote: Installing thor 1.2.2
remote: Fetching tilt 2.1.0
remote: Installing tilt 2.1.0
remote: Fetching semantic_range 3.0.0
remote: Installing semantic_range 3.0.0
remote: Fetching turbolinks-source 5.2.0
remote: Installing turbolinks-source 5.2.0
remote: Fetching i18n 1.14.0
remote: Installing i18n 1.14.0
remote: Fetching tzinfo 2.0.6
remote: Installing tzinfo 2.0.6
remote: Fetching rack-test 2.1.0
remote: Installing rack-test 2.1.0
remote: Fetching rack-proxy 0.7.6
remote: Installing rack-proxy 0.7.6
remote: Fetching sprockets 4.2.0
remote: Installing sprockets 4.2.0
remote: Fetching websocket-driver 0.7.5
remote: Installing websocket-driver 0.7.5 with native extensions
remote: Fetching net-protocol 0.2.1
remote: Installing net-protocol 0.2.1
remote: Fetching nokogiri 1.15.2
remote: Installing nokogiri 1.15.2 with native extensions
remote: Fetching puma 5.6.5
remote: Installing puma 5.6.5 with native extensions
remote: Fetching bootsnap 1.16.0
remote: Installing bootsnap 1.16.0 with native extensions
remote: Fetching turbolinks 5.2.1
remote: Installing turbolinks 5.2.1
remote: Fetching activesupport 6.1.7.3
remote: Installing activesupport 6.1.7.3
remote: Fetching net-pop 0.1.2
remote: Installing net-pop 0.1.2
remote: Fetching net-smtp 0.3.3
remote: Installing net-smtp 0.3.3
remote: Fetching net-imap 0.3.4
remote: Installing net-imap 0.3.4
remote: Fetching sassc 2.4.0
remote: Installing sassc 2.4.0 with native extensions
remote: Fetching globalid 1.1.0
remote: Installing globalid 1.1.0
remote: Fetching activemodel 6.1.7.3
remote: Installing activemodel 6.1.7.3
remote: Fetching mail 2.8.1
remote: Installing mail 2.8.1
remote: Fetching activejob 6.1.7.3
remote: Installing activejob 6.1.7.3
remote: Fetching activerecord 6.1.7.3
remote: Installing activerecord 6.1.7.3
remote: Fetching rails-dom-testing 2.0.3
remote: Fetching loofah 2.21.3
remote: Installing rails-dom-testing 2.0.3
remote: Installing loofah 2.21.3
remote: Fetching rails-html-sanitizer 1.6.0
remote: Installing rails-html-sanitizer 1.6.0
remote: Fetching actionview 6.1.7.3
remote: Installing actionview 6.1.7.3
remote: Fetching jbuilder 2.11.5
remote: Fetching actionpack 6.1.7.3
remote: Installing actionpack 6.1.7.3
remote: Installing jbuilder 2.11.5
remote: Fetching actioncable 6.1.7.3
remote: Fetching activestorage 6.1.7.3
remote: Fetching actionmailer 6.1.7.3
remote: Installing activestorage 6.1.7.3
remote: Installing actionmailer 6.1.7.3
remote: Installing actioncable 6.1.7.3
remote: Fetching sprockets-rails 3.4.2
remote: Fetching railties 6.1.7.3
remote: Fetching actionmailbox 6.1.7.3
remote: Installing sprockets-rails 3.4.2
remote: Fetching actiontext 6.1.7.3
remote: Installing railties 6.1.7.3
remote: Installing actiontext 6.1.7.3
remote: Installing actionmailbox 6.1.7.3
remote: Fetching webpacker 5.4.4
remote: Fetching rails 6.1.7.3
remote: Installing rails 6.1.7.3
remote: Installing webpacker 5.4.4
remote: Fetching sassc-rails 2.1.2
remote: Installing sassc-rails 2.1.2
remote: Fetching sass-rails 6.0.0
remote: Installing sass-rails 6.0.0
remote: Bundle complete! 17 Gemfile dependencies, 63 gems now installed.
remote: Gems in the groups 'development' and 'test' were not installed.
remote: Bundled gems are installed into `./vendor/bundle`
remote: Bundle completed (91.68s)
remote: Cleaning up the bundler cache.
remote: -----> Installing node-v16.18.1-linux-x64
remote: -----> Installing yarn-v1.22.19
remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote: Running: rake assets:precompile
remote: yarn install v1.22.19
remote: [1/4] Resolving packages...
remote: [2/4] Fetching packages...
remote: [3/4] Linking dependencies...
remote: [4/4] Building fresh packages...
remote: Done in 13.41s.
remote: I, [2023-06-02T17:33:30.634554 #1761] INFO -- : Writing /tmp/build_3c4f2a0f/public/assets/manifest-b4bf6e57a53c2bdb55b8998cc94cd00883793c1c37c5e5aea3ef6749b4f6d92b.js
remote: I, [2023-06-02T17:33:30.635400 #1761] INFO -- : Writing /tmp/build_3c4f2a0f/public/assets/manifest-b4bf6e57a53c2bdb55b8998cc94cd00883793c1c37c5e5aea3ef6749b4f6d92b.js.gz
remote: I, [2023-06-02T17:33:30.635816 #1761] INFO -- : Writing /tmp/build_3c4f2a0f/public/assets/application-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css
remote: I, [2023-06-02T17:33:30.635966 #1761] INFO -- : Writing /tmp/build_3c4f2a0f/public/assets/application-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css.gz
remote: I, [2023-06-02T17:33:30.636085 #1761] INFO -- : Writing /tmp/build_3c4f2a0f/public/assets/welcome-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css
remote: I, [2023-06-02T17:33:30.636165 #1761] INFO -- : Writing /tmp/build_3c4f2a0f/public/assets/welcome-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css.gz
remote: Compiling...
remote: Compiled all packs in /tmp/build_3c4f2a0f/public/packs
remote: Hash: 13bc5769bbba2660d04a
remote: Version: webpack 4.46.0
remote: Time: 3198ms
remote: Built at: 06/02/2023 5:33:35 PM
remote: Asset Size Chunks Chunk Names
remote: js/application-5f7b4eeb39bbcc778fef.js 68.2 KiB 0 [emitted] [immutable] application
remote: js/application-5f7b4eeb39bbcc778fef.js.br 15.2 KiB [emitted]
remote: js/application-5f7b4eeb39bbcc778fef.js.gz 17.5 KiB [emitted]
remote: js/application-5f7b4eeb39bbcc778fef.js.map 203 KiB 0 [emitted] [dev] application
remote: js/application-5f7b4eeb39bbcc778fef.js.map.br 43.3 KiB [emitted]
remote: js/application-5f7b4eeb39bbcc778fef.js.map.gz 49.9 KiB [emitted]
remote: manifest.json 364 bytes [emitted]
remote: manifest.json.br 129 bytes [emitted]
remote: manifest.json.gz 142 bytes [emitted]
remote: Entrypoint application = js/application-5f7b4eeb39bbcc778fef.js js/application-5f7b4eeb39bbcc778fef.js.map
remote: [3] ./app/javascript/packs/application.js 480 bytes {0} [built]
remote: [4] ./app/javascript/channels/index.js 205 bytes {0} [built]
remote: [5] ./app/javascript/channels sync _channel\.js$ 160 bytes {0} [built]
remote: + 3 hidden modules
remote:
remote: Asset precompilation completed (19.96s)
remote: Cleaning assets
remote: Running: rake assets:clean
remote: -----> Detecting rails configuration
remote:
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote: Default types for buildpack -> console, rake
remote:
remote: -----> Compressing...
remote: Done: 95.9M
remote: -----> Launching...
remote: ! The following add-ons were automatically provisioned: . These add-ons may incur additional cost, which is prorated to the second. Run `heroku addons` for more info.
remote: Released v6
remote: https://serene-fortress-54097.herokuapp.com/ deployed to Heroku
remote:
remote: This app is using the Heroku-20 stack, however a newer stack is available.
remote: To upgrade to Heroku-22, see:
remote: https://devcenter.heroku.com/articles/upgrading-to-the-latest-stack
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/serene-fortress-54097.git
* [new branch] main -> main
If the output displays warnings or error messages, check the output and make adjustments.
After a successful deployment, complete these tasks as necessary:
- Database migrations
- Scale your dynos
- Check the app’s logs if issues arise
Migrate The Database
If you’re using a database in your application, trigger a migration by using the Heroku CLI to start a one-off dyno. You can run commands, typically scripts and applications that are part of your app, in one-off dynos using the heroku run
command. You can trigger a database migration with this command:
$ heroku run rake db:migrate
To use an interactive shell session instead, you can execute heroku run bash
.
Scale and Access the Application
Heroku runs application code using defined processes and process types. New applications don’t a process type active by default. The following command scales your app up to one dyno, running the web
process:
$ heroku ps:scale web=1
Use the Heroku CLI’s ps
command to display the state of all app dynos in the terminal:
$ heroku ps
=== web (Basic): bundle exec puma -C config/puma.rb (1)
web.1: up 2023/06/02 12:33:57 -0500 (~ 1s ago)
By default, apps use Eco dynos if you’re subscribed to Eco. Otherwise, it defaults to Basic dynos. The Eco dynos plan is shared across all Eco dynos in your account and is recommended if you plan on deploying many small apps to Heroku. Eco dynos sleep if they don’t receive any traffic for half an hour. This sleep behavior causes a few seconds delay for the first request upon waking. Eco dynos consume from a monthly, account-level quota of eco dyno hours. As long as you haven’t exhausted the quota, your apps can continue to run.
To avoid dyno sleeping, upgrade to a Basic or higher dyno type as described in the Dyno Types article. Upgrading to at least Standard dynos also allows you to scale up to multiple dynos per process type.
To launch the app in the browser, run heroku open
:
$ heroku open
The browser displays the “Hello World” text. If it doesn’t, or there’s an error, review and confirm the welcome page contents.
Heroku provides a default web URL for every application during development. When the application is ready for production, add a custom domain.
View Application Logs
The app logs are a valuable tool if the app is not performing correctly or generating errors.
View information about a running app using the Heroku CLI logging command, heroku logs
. Here’s example output:
$ heroku logs
2023-06-02T17:31:33.299436+00:00 app[api]: Initial release by user developer@example.com2023-06-02T17:31:33.299436+00:00 app[api]: Release v1 created by user developer@example.com2023-06-02T17:31:33.496522+00:00 app[api]: Enable Logplex by user developer@example.com2023-06-02T17:31:33.496522+00:00 app[api]: Release v2 created by user developer@example.com2023-06-02T17:31:36.007624+00:00 app[api]: Attach DATABASE (@ref:postgresql-acute-36546) by user developer@example.com2023-06-02T17:31:36.007624+00:00 app[api]: Running release v3 commands by user developer@example.com2023-06-02T17:31:36.020015+00:00 app[api]: Release v4 created by user developer@example.com2023-06-02T17:31:36.020015+00:00 app[api]: @ref:postgresql-acute-36546 completed provisioning, setting DATABASE_URL. by user developer@example.com2023-06-02T17:31:38.000000+00:00 app[api]: Build started by user developer@example.com2023-06-02T17:33:47.861945+00:00 app[api]: Set LANG, RACK_ENV, RAILS_ENV, RAILS_LOG_TO_STDOUT, RAILS_SERVE_STATIC_FILES, SECRET_KEY_BASE config vars by user developer@example.com2023-06-02T17:33:47.861945+00:00 app[api]: Release v5 created by user developer@example.com2023-06-02T17:33:48.497066+00:00 app[api]: Deploy e187b9b6 by user developer@example.com2023-06-02T17:33:48.497066+00:00 app[api]: Release v6 created by user developer@example.com2023-06-02T17:33:48.512927+00:00 app[api]: Scaled to console@0:Basic rake@0:Basic web@1:Basic by user developer@example.com2023-06-02T17:33:52.000000+00:00 app[api]: Build succeeded
2023-06-02T17:33:54.830828+00:00 heroku[web.1]: Starting process with command `bundle exec puma -C config/puma.rb`
2023-06-02T17:33:55.673903+00:00 app[web.1]: Puma starting in single mode...
2023-06-02T17:33:55.673927+00:00 app[web.1]: * Puma version: 5.6.5 (ruby 3.0.6-p216) ("Birdie's Version")
2023-06-02T17:33:55.673928+00:00 app[web.1]: * Min threads: 5
2023-06-02T17:33:55.673928+00:00 app[web.1]: * Max threads: 5
2023-06-02T17:33:55.673928+00:00 app[web.1]: * Environment: production
2023-06-02T17:33:55.673928+00:00 app[web.1]: * PID: 2
2023-06-02T17:33:57.303165+00:00 app[web.1]: * Listening on http://0.0.0.0:4941
2023-06-02T17:33:57.306582+00:00 app[web.1]: Use Ctrl-C to stop
2023-06-02T17:33:57.465545+00:00 heroku[web.1]: State changed from starting to up
2023-06-02T17:34:02.732124+00:00 app[web.1]: I, [2023-06-02T17:34:02.732049 #2] INFO -- : [058559a6-3b16-43e6-9c5f-1aaaedaac7ea] Started GET "/" for 13.110.54.12 at 2023-06-02 17:34:02 +0000
2023-06-02T17:34:02.734756+00:00 app[web.1]: I, [2023-06-02T17:34:02.734712 #2] INFO -- : [058559a6-3b16-43e6-9c5f-1aaaedaac7ea] Processing by WelcomeController#index as HTML
2023-06-02T17:34:02.738573+00:00 app[web.1]: I, [2023-06-02T17:34:02.738531 #2] INFO -- : [058559a6-3b16-43e6-9c5f-1aaaedaac7ea] Rendered welcome/index.html.erb within layouts/application (Duration: 0.3ms | Allocations: 215)
2023-06-02T17:34:02.739557+00:00 app[web.1]: I, [2023-06-02T17:34:02.739524 #2] INFO -- : [058559a6-3b16-43e6-9c5f-1aaaedaac7ea] Rendered layout layouts/application.html.erb (Duration: 1.3ms | Allocations: 1288)
2023-06-02T17:34:02.739736+00:00 app[web.1]: I, [2023-06-02T17:34:02.739713 #2] INFO -- : [058559a6-3b16-43e6-9c5f-1aaaedaac7ea] Completed 200 OK in 5ms (Views: 2.5ms | Allocations: 2932)
2023-06-02T17:34:02.740263+00:00 heroku[router]: at=info method=GET path="/" host=serene-fortress-54097.herokuapp.com request_id=058559a6-3b16-43e6-9c5f-1aaaedaac7ea fwd="13.110.54.12" dyno=web.1 connect=0ms service=13ms status=200 bytes=1848 protocol=https
2023-06-02T17:34:02.879635+00:00 heroku[router]: at=info method=GET path="/assets/application-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css" host=serene-fortress-54097.herokuapp.com request_id=26625d02-97e0-4fcf-b2d8-a7b2c777b8b8 fwd="13.110.54.12" dyno=web.1 connect=0ms service=1ms status=200 bytes=195 protocol=https
2023-06-02T17:34:02.978200+00:00 heroku[router]: at=info method=GET path="/packs/js/application-5f7b4eeb39bbcc778fef.js" host=serene-fortress-54097.herokuapp.com request_id=bb827782-303b-424b-adc4-6975ca73b16f fwd="13.110.54.12" dyno=web.1 connect=0ms service=0ms status=200 bytes=15762 protocol=https
2023-06-02T17:34:03.276759+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=serene-fortress-54097.herokuapp.com request_id=1b75ee4d-6dca-4acc-b6b8-3118322f3434 fwd="13.110.54.12" dyno=web.1 connect=0ms service=0ms status=200 bytes=143 protocol=https
You can also get the full stream of logs by running the logs command with the --tail
flag option like this:
$ heroku logs --tail
By default, Heroku stores 1500 lines of logs from your application, but the full log stream is available as a service. Several add-on providers have logging services that provide things such as log persistence, search, and email and SMS alerts.
Optional Steps
Use The Rails Console
Use the Heroku CLI run
command to trigger one-off dynos to run scripts and applications only when necessary. Use the command to launch a Rails console process attached to the local terminal for experimenting in the app’s environment:
$ heroku run rails console
irb(main):001:0> puts 1+1
2
Run Rake Commands
Run rake
commands, such as db:migrate
, using the run
command exactly like the Rails console:
$ heroku run rake db:migrate
Use a Procfile locally
To use the Procfile
locally, use the heroku local
CLI command.
In addition to running commands in the Procfile
, the heroku local
command can also manage environment variables locally through a .env
file. Set RACK_ENV
to development
for the local environment and the PORT
for Puma.
$ echo "RACK_ENV=development" >>.env
$ echo "PORT=3000" >> .env
Another alternative to using environment variables locally with a .env
file is the dotenv gem.
Add .env
to .gitignore
as these variables are for local environment setup only.
$ echo ".env" >> .gitignore
$ git add .gitignore
$ git commit -m "add .env to .gitignore"
Test the Procfile locally using Foreman. Start the web server with local
:
$ heroku local
[OKAY] Loaded ENV .env File as KEY=VALUE Format
12:34:07 PM web.1 | Puma starting in single mode...
12:34:07 PM web.1 | * Puma version: 5.6.5 (ruby 3.0.6-p216) ("Birdie's Version")
12:34:07 PM web.1 | * Min threads: 5
12:34:07 PM web.1 | * Max threads: 5
12:34:07 PM web.1 | * Environment: development
12:34:07 PM web.1 | * PID: 59960
12:34:09 PM web.1 | * Listening on http://0.0.0.0:3000
12:34:09 PM web.1 | Use Ctrl-C to stop
Press Ctrl+C
or Cmd+C
to exit.
Rails asset pipeline
When deploying to Heroku, there are several options for invoking the Rails asset pipeline. See the Rails 3.1+ Asset Pipeline on Heroku Cedar article for general information on the asset pipeline.
Rails 6 removed the config.assets.initialize_on_precompile
option because it’s no longer needed. Additionally, any failure in asset compilation now causes the push to fail. For Rails 6 asset pipeline support, see the Ruby Support page.
Remove Heroku Gems
Previous versions of Rails required you to add a gem to your project rails_12factor to enable static asset serving and logging on Heroku. If you are deploying a new application, this gem is not needed. If you are upgrading an existing application, you can remove this gem provided you have the appropriate configuration in your config/environments/production.rb
file:
# config/environments/production.rb
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end
Troubleshooting
If an app deployed to Heroku crashes, for example, heroku ps
shows the state crashed
, review the app’s logs. The following section covers common causes of app crashes.
Runtime Dependencies on Development or Test Gems
If a gem is missing during deployment, check the Bundler groups. Heroku builds apps without the development
or test
groups, and if the app depends on a gem from one of these groups to run, move it out of the group.
A common example is using the RSpec tasks in the Rakefile
. The error often looks like this:
$ heroku run rake -T
Running `bundle exec rake -T` attached to terminal... up, ps.3
rake aborted!
no such file to load -- rspec/core/rake_task
First, duplicate the problem locally by running bundle install
without the development or test gem groups:
$ bundle install --without development:test
…
$ bundle exec rake -T
rake aborted!
no such file to load -- rspec/core/rake_task
The --without
option on bundler
is persistent. To remove this option, run bundle config --delete without
.
Fix the error by making these Rake tasks conditional during gem load. For example:
begin
require "rspec/core/rake_task"
desc "Run all examples"
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w[--color]
t.pattern = 'spec/**/*_spec.rb'
end
rescue LoadError
end
Confirm it works locally, then push it to Heroku.
Next Steps
Congratulations! You deployed your first Rails 6 application to Heroku. Review the following articles next:
- Visit the Ruby support category to learn more about using Ruby and Rails on Heroku.
- The Deployment category provides a variety of powerful integrations and features to help streamline and simplify your deployments.
Deleting Your App and Add-on
Remove the app and database from your account. You’re only charged for the resources you used.
This action removes your add-on and any data saved in the database.
$ heroku addons:destroy heroku-postgresql
This action permanently deletes your application
$ heroku apps:destroy
You can confirm that your add-on and app are gone with the commands:
$ heroku addons --all
$ heroku apps -all
You’re now ready to deploy your app.