Getting Started on Heroku with Rails 7.x
Last updated February 06, 2023
Table of Contents
- Local setup
- Create a New or Upgrade an Existing Rails App
- Add the pg gem
- Create a Welcome Page
- Heroku Gems
- Specify the Ruby Version
- Store The App in Git
- Deploy the Application to Heroku
- Migrate The Database
- Access the Application
- View Application Logs
- Dyno Sleeping and Scaling
- The Rails Console
- Rake Commands
- Configure The Web Server
- Rails asset pipeline
- Troubleshooting
- Next Steps
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.
Ruby on Rails is a popular web framework written in Ruby. This guide covers using Rails 7 on Heroku. For information on running previous versions of Rails on Heroku, see the tutorial for Rails 6.x or Rails 5.x.
Before continuing, it’s helpful to have:
- Basic familiarity with Ruby, Ruby on Rails, and Git
- A locally installed version of Ruby 2.7.0+, Rubygems, Bundler, and Rails 7+
- A locally installed version of the Heroku CLI
- A verified Heroku Account
- A subscription to the Eco dynos plan (recommended)
Local setup
With the Heroku CLI installed, heroku
is now an available command in the terminal. Log in to Heroku using the CLI:
$ heroku login
heroku: Enter your Heroku credentials
Email: schneems@example.com
Password:
Could not find an existing public key.
Would you like to generate one? [Yn]
Generating new SSH public key.
Uploading ssh public key /Users/adam/.ssh/id_rsa.pub
Press Enter at the prompt to upload an existing ssh
key or create a new one.
info After November 30, 2021, Heroku will no longer support the SSH Git transport. SSH keys will serve no purpose in pushing code to applications on the Heroku platform.
Create a New or Upgrade an Existing Rails App
Ensure Rails 7 is installed with rails -v
before creating an app. If necessary, install Rails 7 with gem install
:
$ gem install rails --no-document
Successfully installed rails-7.0.4
1 gem installed
Create a Rails app:
$ rails new myapp --database=postgresql
Move into the application directly 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
Add the pg gem
For new or existing apps where --database=postgresql
wasn’t defined, confirm the sqlite3
gem doesn’t exist in the Gemfile
. Add the pg
gem in its place.
Within the Gemfile
remove:
gem 'sqlite3'
And replace it with:
gem 'pg'
Heroku highly recommends using PostgreSQL locally during development. Maintaining parity between development and deployment environments prevents subtle bugs from being introduced because of the differences in those environments.
Install Postgres locally now if not present on the system, already. 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
Doing so updates Gemfile.lock
with the changes made previously.
In addition to using the pg
gem, ensure that config/database.yml
defines the postgresql
adapter. The development section of config/database.yml
file will look 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
(note the sql
at the end), the application won’t work.
Create a Welcome Page
Rails 7 no longer has a static index page in production by default. Apps upgraded to Rails 7 keep their existing page configurations, but new Rails 7 apps do not have an automatically generated welcome page. Create a welcome
controller to hold the homepage:
$ rails generate controller welcome
Create app/views/welcome/index.html.erb
and add the following snippet:
In file app/views/welcome/index.html.erb
write:
<h2>Hello World</h2>
<p>
The time is now: <%= Time.now %>
</p>
With a welcome page created, create a route to map 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'
Verify the page is present by starting the Rails web server:
$ rails server
Visit http://localhost:3000 in a browser. If the page doesn’t display, reference the logs Rails outputs within the same terminal where rails server
started to debug the error.
Heroku Gems
Previous versions of Rails (Rails 4 and older) required the rails_12factor gem to enable static asset serving and logging on Heroku. New Rails applications don’t need this gem. The gem can be removed from existing, upgraded applications provided the following code is present in config/environments/production.rb
:
# 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
Specify the Ruby Version
Rails 7 requires Ruby 2.7.0 or above. Heroku installs a recent version of Ruby buy default. Specify an exact version with the ruby
DSL in Gemfile
like the following example of defining Ruby 3.1.2:
ruby "3.1.2"
Always use the same version of Ruby locally, too. Confirm the local version of ruby with ruby -v
. Refer to the Ruby Versions article for more details on defining a specific ruby version.
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>]
Git is not present if the command produces no output or command not found
. Install Git on the system.
After verifying Git is functional, navigate to the root directory of the Rails app. The contents of the Rails app looks something like this when using ls
:
$ ls
Gemfile
Gemfile-e
Gemfile.lock
README.md
Rakefile
app
bin
config
config.ru
db
lib
log
public
storage
test
tmp
vendor
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 was committed correctly with git status
:
$ git status
On branch main
nothing to commit, working tree clean
With the application committed to Git, it is ready to deploy to Heroku.
Deploy the Application to Heroku
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.
Inside the Rails app’s root directory, use the Heroku CLI to create an app on Heroku:
$ heroku apps:create --stack=heroku-22
Creating app... done, calm-citadel-85993, stack is heroku-22
https://calm-citadel-85993.herokuapp.com/ | https://git.heroku.com/calm-citadel-85993.git
The Heroku CLI adds the Git remote automatically. Verify it is set with git config
:
$ git config --list --local | grep heroku
remote.heroku.url=https://git.heroku.com/calm-citadel-85993.git
remote.heroku.fetch=+refs/heads/*:refs/remotes/heroku/*
Git returns fatal: not in a git directory
if the current directory is incorrect or Git is not initialized. If Git returns a list of remotes, it is ready to deploy.
Following changes in the industry, Heroku updated the default branch name to main
. If the project uses master
as its default branch name, use git push heroku master
.
Deploy the code:
$ git push heroku main
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Building on the Heroku-22 stack
remote: -----> Determining which buildpack to use for this app
remote: -----> Ruby app detected
remote: -----> Installing bundler 2.3.10
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-3.1.2
remote: -----> Installing dependencies using bundler 2.3.10
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 minitest 5.16.3
remote: Fetching builder 3.2.4
remote: Fetching concurrent-ruby 1.1.10
remote: Fetching erubi 1.11.0
remote: Installing builder 3.2.4
remote: Installing minitest 5.16.3
remote: Installing concurrent-ruby 1.1.10
remote: Installing erubi 1.11.0
remote: Fetching mini_portile2 2.8.0
remote: Using racc 1.6.0
remote: Fetching crass 1.0.6
remote: Fetching rack 2.2.4
remote: Installing mini_portile2 2.8.0
remote: Fetching nio4r 2.5.8
remote: Installing crass 1.0.6
remote: Fetching websocket-extensions 0.1.5
remote: Installing rack 2.2.4
remote: Installing websocket-extensions 0.1.5
remote: Fetching marcel 1.0.2
remote: Installing nio4r 2.5.8 with native extensions
remote: Fetching mini_mime 1.1.2
remote: Installing marcel 1.0.2
remote: Fetching timeout 0.3.0
remote: Installing mini_mime 1.1.2
remote: Using bundler 2.3.10
remote: Installing timeout 0.3.0
remote: Fetching method_source 1.0.0
remote: Fetching msgpack 1.6.0
remote: Fetching thor 1.2.1
remote: Installing method_source 1.0.0
remote: Installing msgpack 1.6.0 with native extensions
remote: Installing thor 1.2.1
remote: Fetching zeitwerk 2.6.6
remote: Installing zeitwerk 2.6.6
remote: Fetching pg 1.4.4
remote: Fetching nokogiri 1.13.9
remote: Installing pg 1.4.4 with native extensions
remote: Installing nokogiri 1.13.9 with native extensions
remote: Fetching websocket-driver 0.7.5
remote: Installing websocket-driver 0.7.5 with native extensions
remote: Fetching i18n 1.12.0
remote: Installing i18n 1.12.0
remote: Fetching tzinfo 2.0.5
remote: Installing tzinfo 2.0.5
remote: Fetching mail 2.7.1
remote: Installing mail 2.7.1
remote: Fetching rack-test 2.0.2
remote: Installing rack-test 2.0.2
remote: Fetching sprockets 4.1.1
remote: Fetching net-protocol 0.1.3
remote: Installing sprockets 4.1.1
remote: Installing net-protocol 0.1.3
remote: Fetching puma 5.6.5
remote: Fetching activesupport 7.0.4
remote: Installing puma 5.6.5 with native extensions
remote: Installing activesupport 7.0.4
remote: Fetching bootsnap 1.13.0
remote: Installing bootsnap 1.13.0 with native extensions
remote: Fetching net-imap 0.3.1
remote: Installing net-imap 0.3.1
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 globalid 1.0.0
remote: Installing globalid 1.0.0
remote: Fetching activemodel 7.0.4
remote: Installing activemodel 7.0.4
remote: Fetching activejob 7.0.4
remote: Installing activejob 7.0.4
remote: Fetching activerecord 7.0.4
remote: Installing activerecord 7.0.4
remote: Fetching rails-dom-testing 2.0.3
remote: Fetching loofah 2.19.0
remote: Installing loofah 2.19.0
remote: Installing rails-dom-testing 2.0.3
remote: Fetching rails-html-sanitizer 1.4.3
remote: Installing rails-html-sanitizer 1.4.3
remote: Fetching actionview 7.0.4
remote: Installing actionview 7.0.4
remote: Fetching actionpack 7.0.4
remote: Fetching jbuilder 2.11.5
remote: Installing jbuilder 2.11.5
remote: Installing actionpack 7.0.4
remote: Fetching actioncable 7.0.4
remote: Fetching activestorage 7.0.4
remote: Fetching actionmailer 7.0.4
remote: Fetching railties 7.0.4
remote: Installing actioncable 7.0.4
remote: Installing activestorage 7.0.4
remote: Installing actionmailer 7.0.4
remote: Fetching sprockets-rails 3.4.2
remote: Installing railties 7.0.4
remote: Installing sprockets-rails 3.4.2
remote: Fetching actiontext 7.0.4
remote: Fetching actionmailbox 7.0.4
remote: Installing actionmailbox 7.0.4
remote: Installing actiontext 7.0.4
remote: Fetching rails 7.0.4
remote: Fetching importmap-rails 1.1.5
remote: Fetching stimulus-rails 1.1.1
remote: Fetching turbo-rails 1.3.2
remote: Installing stimulus-rails 1.1.1
remote: Installing importmap-rails 1.1.5
remote: Installing rails 7.0.4
remote: Installing turbo-rails 1.3.2
remote: Bundle complete! 15 Gemfile dependencies, 55 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 (35.89s)
remote: Cleaning up the bundler cache.
remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote: Running: rake assets:precompile
remote: I, [2022-11-09T20:50:08.860584 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/manifest-b84bfa46a33d7f0dc4d2e7b8889486c9a957a5e40713d58f54be71b66954a1ff.js
remote: I, [2022-11-09T20:50:08.860763 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/manifest-b84bfa46a33d7f0dc4d2e7b8889486c9a957a5e40713d58f54be71b66954a1ff.js.gz
remote: I, [2022-11-09T20:50:08.860947 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/application-e0cf9d8fcb18bf7f909d8d91a5e78499f82ac29523d475bf3a9ab265d5e2b451.css
remote: I, [2022-11-09T20:50:08.861062 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/application-e0cf9d8fcb18bf7f909d8d91a5e78499f82ac29523d475bf3a9ab265d5e2b451.css.gz
remote: I, [2022-11-09T20:50:08.861188 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/application-37f365cbecf1fa2810a8303f4b6571676fa1f9c56c248528bc14ddb857531b95.js
remote: I, [2022-11-09T20:50:08.861278 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/application-37f365cbecf1fa2810a8303f4b6571676fa1f9c56c248528bc14ddb857531b95.js.gz
remote: I, [2022-11-09T20:50:08.861393 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/controllers/application-368d98631bccbf2349e0d4f8269afb3fe9625118341966de054759d96ea86c7e.js
remote: I, [2022-11-09T20:50:08.861503 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/controllers/application-368d98631bccbf2349e0d4f8269afb3fe9625118341966de054759d96ea86c7e.js.gz
remote: I, [2022-11-09T20:50:08.861924 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/controllers/hello_controller-549135e8e7c683a538c3d6d517339ba470fcfb79d62f738a0a089ba41851a554.js
remote: I, [2022-11-09T20:50:08.862037 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/controllers/hello_controller-549135e8e7c683a538c3d6d517339ba470fcfb79d62f738a0a089ba41851a554.js.gz
remote: I, [2022-11-09T20:50:08.862166 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/controllers/index-2db729dddcc5b979110e98de4b6720f83f91a123172e87281d5a58410fc43806.js
remote: I, [2022-11-09T20:50:08.862309 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/controllers/index-2db729dddcc5b979110e98de4b6720f83f91a123172e87281d5a58410fc43806.js.gz
remote: I, [2022-11-09T20:50:08.862437 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/turbo-8794998d8fb36a2ae3fcb23679cd330faef003414be4662cf01cde0756461abd.js
remote: I, [2022-11-09T20:50:08.862584 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/turbo-8794998d8fb36a2ae3fcb23679cd330faef003414be4662cf01cde0756461abd.js.gz
remote: I, [2022-11-09T20:50:08.862740 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/turbo.min-7ab2ea9f35bae4a4d65b552f9b93524099f267a8ba3a2e07002aaa7bff8ae4cf.js
remote: I, [2022-11-09T20:50:08.862817 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/turbo.min-7ab2ea9f35bae4a4d65b552f9b93524099f267a8ba3a2e07002aaa7bff8ae4cf.js.gz
remote: I, [2022-11-09T20:50:08.862961 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/turbo.min.js-cdf476cab7616917e39a89c91399dcb01097d08a2d4787b0ee3b52d576cb06ec.map
remote: I, [2022-11-09T20:50:08.863058 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/turbo.min.js-cdf476cab7616917e39a89c91399dcb01097d08a2d4787b0ee3b52d576cb06ec.map.gz
remote: I, [2022-11-09T20:50:08.863219 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/actiontext-28c61f5197c204db043317a8f8826a87ab31495b741f854d307ca36122deefce.js
remote: I, [2022-11-09T20:50:08.863325 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/actiontext-28c61f5197c204db043317a8f8826a87ab31495b741f854d307ca36122deefce.js.gz
remote: I, [2022-11-09T20:50:08.863439 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/trix-1563ff9c10f74e143b3ded40a8458497eaf2f87a648a5cbbfebdb7dec3447a5e.js
remote: I, [2022-11-09T20:50:08.863527 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/trix-1563ff9c10f74e143b3ded40a8458497eaf2f87a648a5cbbfebdb7dec3447a5e.js.gz
remote: I, [2022-11-09T20:50:08.863670 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/trix-ac629f94e04ee467ab73298a3496a4dfa33ca26a132f624dd5475381bc27bdc8.css
remote: I, [2022-11-09T20:50:08.863760 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/trix-ac629f94e04ee467ab73298a3496a4dfa33ca26a132f624dd5475381bc27bdc8.css.gz
remote: I, [2022-11-09T20:50:08.863915 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/es-module-shims-16719834c9bbcdd75f1f99da713bd0c89de488be94d4c5df594511f39cffe7c1.js
remote: I, [2022-11-09T20:50:08.864010 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/es-module-shims-16719834c9bbcdd75f1f99da713bd0c89de488be94d4c5df594511f39cffe7c1.js.gz
remote: I, [2022-11-09T20:50:08.864160 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/es-module-shims.min-d89e73202ec09dede55fb74115af9c5f9f2bb965433de1c2446e1faa6dac2470.js
remote: I, [2022-11-09T20:50:08.864266 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/es-module-shims.min-d89e73202ec09dede55fb74115af9c5f9f2bb965433de1c2446e1faa6dac2470.js.gz
remote: I, [2022-11-09T20:50:08.864415 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/es-module-shims.js-32db422c5db541b7129a2ce936aed905edc2cd481748f8d67ffe84e28313158a.map
remote: I, [2022-11-09T20:50:08.864526 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/es-module-shims.js-32db422c5db541b7129a2ce936aed905edc2cd481748f8d67ffe84e28313158a.map.gz
remote: I, [2022-11-09T20:50:08.864677 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-d094f4a1919726d8e6080201a149255e6b1561342bff3d739bc227018b6bbc80.js
remote: I, [2022-11-09T20:50:08.864780 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-d094f4a1919726d8e6080201a149255e6b1561342bff3d739bc227018b6bbc80.js.gz
remote: I, [2022-11-09T20:50:08.865025 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-autoloader-c584942b568ba74879da31c7c3d51366737bacaf6fbae659383c0a5653685693.js
remote: I, [2022-11-09T20:50:08.865151 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-autoloader-c584942b568ba74879da31c7c3d51366737bacaf6fbae659383c0a5653685693.js.gz
remote: I, [2022-11-09T20:50:08.865416 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-importmap-autoloader-db2076c783bf2dbee1226e2add52fef290b5d31b5bcd1edd999ac8a6dd31c44a.js
remote: I, [2022-11-09T20:50:08.865919 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-importmap-autoloader-db2076c783bf2dbee1226e2add52fef290b5d31b5bcd1edd999ac8a6dd31c44a.js.gz
remote: I, [2022-11-09T20:50:08.866089 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js
remote: I, [2022-11-09T20:50:08.866199 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js.gz
remote: I, [2022-11-09T20:50:08.866356 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus.min-2dae3fdcdb1a5ee8172d3dc02a2a10cd6d5f022cc7782b3888cedc06bab7388a.js
remote: I, [2022-11-09T20:50:08.866492 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus.min-2dae3fdcdb1a5ee8172d3dc02a2a10cd6d5f022cc7782b3888cedc06bab7388a.js.gz
remote: I, [2022-11-09T20:50:08.866634 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-autoloader-c584942b568ba74879da31c7c3d51366737bacaf6fbae659383c0a5653685693.js
remote: I, [2022-11-09T20:50:08.866722 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-autoloader-c584942b568ba74879da31c7c3d51366737bacaf6fbae659383c0a5653685693.js.gz
remote: I, [2022-11-09T20:50:08.867067 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-importmap-autoloader-db2076c783bf2dbee1226e2add52fef290b5d31b5bcd1edd999ac8a6dd31c44a.js
remote: I, [2022-11-09T20:50:08.867566 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-importmap-autoloader-db2076c783bf2dbee1226e2add52fef290b5d31b5bcd1edd999ac8a6dd31c44a.js.gz
remote: I, [2022-11-09T20:50:08.868434 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js
remote: I, [2022-11-09T20:50:08.868667 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js.gz
remote: I, [2022-11-09T20:50:08.880863 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus.min.js-b59104bd3d943049392c2908fc25a7dcbc5f918a632174f80f6bb34e86e49a8a.map
remote: I, [2022-11-09T20:50:08.881219 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus.min.js-b59104bd3d943049392c2908fc25a7dcbc5f918a632174f80f6bb34e86e49a8a.map.gz
remote: I, [2022-11-09T20:50:08.881573 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/activestorage-3ab61e47dd4ee2d79db525ade1dca2ede0ea2b7371fe587e408ee37b7ade265d.js
remote: I, [2022-11-09T20:50:08.881936 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/activestorage-3ab61e47dd4ee2d79db525ade1dca2ede0ea2b7371fe587e408ee37b7ade265d.js.gz
remote: I, [2022-11-09T20:50:08.882240 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/activestorage.esm-01f58a45d77495cdfbdfcc872902a430426c4391634ec9c3da5f69fbf8418492.js
remote: I, [2022-11-09T20:50:08.882497 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/activestorage.esm-01f58a45d77495cdfbdfcc872902a430426c4391634ec9c3da5f69fbf8418492.js.gz
remote: I, [2022-11-09T20:50:08.882722 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/actioncable-5433453f9b6619a9de91aaab2d7fc7ff183e5260c0107cbc9a1aa0c838d9a74e.js
remote: I, [2022-11-09T20:50:08.882879 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/actioncable-5433453f9b6619a9de91aaab2d7fc7ff183e5260c0107cbc9a1aa0c838d9a74e.js.gz
remote: I, [2022-11-09T20:50:08.883064 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/actioncable.esm-e01089c3ec4fe7817fa9abcad06cab6bdc387f95f0ca6aab4bf7ba7537f70690.js
remote: I, [2022-11-09T20:50:08.883209 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/actioncable.esm-e01089c3ec4fe7817fa9abcad06cab6bdc387f95f0ca6aab4bf7ba7537f70690.js.gz
remote: Asset precompilation completed (1.16s)
remote: Cleaning assets
remote: Running: rake assets:clean
remote: -----> Detecting rails configuration
remote:
remote: ###### WARNING:
remote:
remote: No Procfile detected, using the default web server.
remote: We recommend explicitly declaring how to boot your server process via a Procfile.
remote: https://devcenter.heroku.com/articles/ruby-default-web-server
remote:
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> (none)
remote: Default types for buildpack -> console, rake, web
remote:
remote: -----> Compressing...
remote: Done: 37.6M
remote: -----> Launching...
remote: ! The following add-ons were automatically provisioned: heroku-postgresql. These add-ons may incur additional cost, which is prorated to the second. Run `heroku addons` for more info.
remote: Released v6
remote: https://calm-citadel-85993.herokuapp.com/ deployed to Heroku
remote:
remote: Starting November 28th, 2022, free Heroku Dynos, free Heroku Postgres, and free Heroku Data for Redis® will no longer be available.
remote:
remote: If you have apps using any of these resources, you must upgrade to paid plans by this date to ensure your apps continue to run and to retain your data. For students, we will announce a new program by the end of September. Learn more at https://blog.heroku.com/next-chapter
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/calm-citadel-85993.git
* [new branch] main -> main
The output may display warnings or error messages. Check the output for these and make adjustments as necessary.
If the deployment is successful, the application may need a few additional adjustments:
- Migration of the database
- Ensure proper dyno scaling
- Reference the app’s logs if any issues arise
Migrate The Database
If the application uses a database, trigger a migration by using the Heroku CLI to start a one-off dyno, which is a lightweight container that is the basic unit of composition on Heroku, and run db:migrate
:
$ heroku run rake db:migrate
Obtain an interactive shell session, instead, with heroku run bash
.
Access the Application
The application is successfully deployed to Heroku. Heroku runs application code using defined processes and process types. New applications will not have a process type active by default. Scale the web
process type using the Heroku CLI’s ps:scale
command:
$ heroku ps:scale web=1
Use the Heroku CLI’s ps
command to display the state of all of an app’s dynos in the terminal:
$ heroku ps
Eco dyno hours quota remaining this month: 1000h 0m (100%)
Eco dyno usage for this app: 0h 0m (0%)
For more information on dyno sleeping and how to upgrade, see:
https://devcenter.heroku.com/articles/eco-dyno-hours#dyno-sleeping
=== web (Eco): bin/rails server -p ${PORT:-5000} -e $RAILS_ENV (1)
web.1: up 2022/11/09 12:50:25 -0800 (~ 1s ago)
In the previous example, a single web
process is running.
Use heroku open
to launch the app in the browser.
$ heroku open
The browser should display the “Hello World” text defined previously. If it does not, or an error is present, review and confirm the welcome page contents.
Heroku provides a default web URL for every application during development. When the application is ready to scale up 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 is example output:
$ heroku logs
2022-11-09T20:49:22.967927+00:00 app[api]: Initial release by user developer@example.com2022-11-09T20:49:22.967927+00:00 app[api]: Release v1 created by user developer@example.com2022-11-09T20:49:23.123785+00:00 app[api]: Enable Logplex by user developer@example.com2022-11-09T20:49:23.123785+00:00 app[api]: Release v2 created by user developer@example.com2022-11-09T20:49:25.000000+00:00 app[api]: Build started by user developer@example.com2022-11-09T20:50:15.759414+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.com2022-11-09T20:50:15.759414+00:00 app[api]: Release v3 created by user developer@example.com2022-11-09T20:50:17.118438+00:00 app[api]: Running release v4 commands by user developer@example.com2022-11-09T20:50:17.118438+00:00 app[api]: Attach DATABASE (@ref:postgresql-contoured-75359) by user developer@example.com2022-11-09T20:50:17.136705+00:00 app[api]: Release v5 created by user developer@example.com2022-11-09T20:50:17.136705+00:00 app[api]: @ref:postgresql-contoured-75359 completed provisioning, setting DATABASE_URL. by user developer@example.com2022-11-09T20:50:17.517963+00:00 app[api]: Deploy 3e67ada9 by user developer@example.com2022-11-09T20:50:17.517963+00:00 app[api]: Release v6 created by user developer@example.com2022-11-09T20:50:17.534131+00:00 app[api]: Scaled to console@0:Eco rake@0:Eco web@1:Eco by user developer@example.com2022-11-09T20:50:19.949404+00:00 heroku[web.1]: Starting process with command `bin/rails server -p ${PORT:-5000} -e production`
2022-11-09T20:50:23.218318+00:00 app[web.1]: => Booting Puma
2022-11-09T20:50:23.218355+00:00 app[web.1]: => Rails 7.0.4 application starting in production
2022-11-09T20:50:23.218355+00:00 app[web.1]: => Run `bin/rails server --help` for more startup options
2022-11-09T20:50:24.603033+00:00 app[web.1]: Puma starting in single mode...
2022-11-09T20:50:24.603101+00:00 app[web.1]: * Puma version: 5.6.5 (ruby 3.1.2-p20) ("Birdie's Version")
2022-11-09T20:50:24.603129+00:00 app[web.1]: * Min threads: 5
2022-11-09T20:50:24.603146+00:00 app[web.1]: * Max threads: 5
2022-11-09T20:50:24.603162+00:00 app[web.1]: * Environment: production
2022-11-09T20:50:24.603183+00:00 app[web.1]: * PID: 4
2022-11-09T20:50:24.603486+00:00 app[web.1]: * Listening on http://0.0.0.0:11193
2022-11-09T20:50:24.609496+00:00 app[web.1]: Use Ctrl-C to stop
2022-11-09T20:50:25.010279+00:00 heroku[web.1]: State changed from starting to up
2022-11-09T20:50:30.072797+00:00 app[web.1]: I, [2022-11-09T20:50:30.072682 #4] INFO -- : [1f916fca-22c1-45b2-913b-2c931e999b05] Started GET "/" for 13.110.54.11 at 2022-11-09 20:50:30 +0000
2022-11-09T20:50:30.077315+00:00 app[web.1]: I, [2022-11-09T20:50:30.077223 #4] INFO -- : [1f916fca-22c1-45b2-913b-2c931e999b05] Processing by WelcomeController#index as HTML
2022-11-09T20:50:30.080505+00:00 app[web.1]: I, [2022-11-09T20:50:30.080420 #4] INFO -- : [1f916fca-22c1-45b2-913b-2c931e999b05] Rendered welcome/index.html.erb within layouts/application (Duration: 0.7ms | Allocations: 222)
2022-11-09T20:50:30.091341+00:00 app[web.1]: I, [2022-11-09T20:50:30.091242 #4] INFO -- : [1f916fca-22c1-45b2-913b-2c931e999b05] Rendered layout layouts/application.html.erb (Duration: 11.6ms | Allocations: 2733)
2022-11-09T20:50:30.091812+00:00 app[web.1]: I, [2022-11-09T20:50:30.091750 #4] INFO -- : [1f916fca-22c1-45b2-913b-2c931e999b05] Completed 200 OK in 14ms (Views: 13.6ms | Allocations: 3716)
2022-11-09T20:50:30.095252+00:00 heroku[router]: at=info method=GET path="/" host=calm-citadel-85993.herokuapp.com request_id=1f916fca-22c1-45b2-913b-2c931e999b05 fwd="13.110.54.11" dyno=web.1 connect=0ms service=29ms status=200 bytes=3408 protocol=https
2022-11-09T20:50:30.262089+00:00 heroku[router]: at=info method=GET path="/assets/application-e0cf9d8fcb18bf7f909d8d91a5e78499f82ac29523d475bf3a9ab265d5e2b451.css" host=calm-citadel-85993.herokuapp.com request_id=f37745b2-876e-4c29-8450-22950fa90937 fwd="13.110.54.11" dyno=web.1 connect=0ms service=2ms status=200 bytes=575 protocol=https
2022-11-09T20:50:30.262831+00:00 heroku[router]: at=info method=GET path="/assets/es-module-shims.min-d89e73202ec09dede55fb74115af9c5f9f2bb965433de1c2446e1faa6dac2470.js" host=calm-citadel-85993.herokuapp.com request_id=92b09871-4957-4616-98fc-90d1fdda1758 fwd="13.110.54.11" dyno=web.1 connect=0ms service=3ms status=200 bytes=11261 protocol=https
2022-11-09T20:50:30.395242+00:00 heroku[router]: at=info method=GET path="/assets/application-37f365cbecf1fa2810a8303f4b6571676fa1f9c56c248528bc14ddb857531b95.js" host=calm-citadel-85993.herokuapp.com request_id=7ba4308c-aed4-47f6-a822-b888e58cd22b fwd="13.110.54.11" dyno=web.1 connect=0ms service=1ms status=200 bytes=323 protocol=https
2022-11-09T20:50:30.460956+00:00 heroku[router]: at=info method=GET path="/assets/turbo.min-7ab2ea9f35bae4a4d65b552f9b93524099f267a8ba3a2e07002aaa7bff8ae4cf.js" host=calm-citadel-85993.herokuapp.com request_id=0a5d8de3-a146-4eb4-b418-e03a203f407e fwd="13.110.54.11" dyno=web.1 connect=0ms service=2ms status=200 bytes=22015 protocol=https
2022-11-09T20:50:30.513920+00:00 heroku[router]: at=info method=GET path="/assets/stimulus.min-2dae3fdcdb1a5ee8172d3dc02a2a10cd6d5f022cc7782b3888cedc06bab7388a.js" host=calm-citadel-85993.herokuapp.com request_id=a31a5c2b-23c0-4253-a8f7-7a6c5ed76703 fwd="13.110.54.11" dyno=web.1 connect=0ms service=1ms status=200 bytes=9020 protocol=https
2022-11-09T20:50:30.517850+00:00 heroku[router]: at=info method=GET path="/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js" host=calm-citadel-85993.herokuapp.com request_id=a6518616-8e2e-4e4d-b0b9-13fd4de52a7c fwd="13.110.54.11" dyno=web.1 connect=0ms service=1ms status=200 bytes=1202 protocol=https
2022-11-09T20:50:30.551655+00:00 heroku[router]: at=info method=GET path="/assets/controllers/index-2db729dddcc5b979110e98de4b6720f83f91a123172e87281d5a58410fc43806.js" host=calm-citadel-85993.herokuapp.com request_id=1f3c8f5b-ed5a-4fb1-8762-fe947c2847e5 fwd="13.110.54.11" dyno=web.1 connect=0ms service=1ms status=200 bytes=444 protocol=https
2022-11-09T20:50:30.713009+00:00 heroku[router]: at=info method=GET path="/assets/controllers/application-368d98631bccbf2349e0d4f8269afb3fe9625118341966de054759d96ea86c7e.js" host=calm-citadel-85993.herokuapp.com request_id=a5994257-aaa5-41e4-b6c5-b6522fae4bf7 fwd="13.110.54.11" dyno=web.1 connect=0ms service=1ms status=200 bytes=349 protocol=https
Append -t
/--tail
to the command to see a full, live stream of the app’s logs:
$ heroku logs --tail
Dyno Sleeping and Scaling
New applications are deployed to an eco dyno by default. After a period of inactivity, eco apps will “sleep” to conserve resources. For more on Heroku’s eco dyno behavior, see Eco Dyno Hours.
Upgrade to a Basic or Professional dyno type as described in the Dyno Types article to avoid dyno sleeping. For example, migrating an app to a progressional dyno allows for easy scaling by using the Heroku CLI ps:scale
command to instruct the Heroku platform to start or stop additional dynos that run the same web
process type.
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
The run bash
Heroku CLI command is also helpful for debugging. The command starts a new one-off dyno with an interactive bash session.
Rake Commands
Run rake
commands (db:migrate
, for example) using the run
command exactly like the Rails console:
$ heroku run rake db:migrate
Configure The Web Server
By default, a Rails app’s web process runs rails server
, which uses Puma in Rails 7. Apps upgraded to Rails 7 need the puma
gem added to the app’s Gemfile
:
gem 'puma'
After adding the puma
gem, install it:
$ bundle install
Rails 7 uses config/puma.rb
to define Puma’s configuration and functionality with Puma installed. Heroku recommends reviewing additional Puma configuration options to maximize the app’s performance.
If config/puma.rb
doesn’t exist, create one using Heroku’s Puma documentation for maximum performance.
With Puma installed, use the Procfile
to instruct Heroku on how to launch the Rails app on a dyno.
Create a Procfile
Change the command used to launch your web process by creating a file called Procfile inside the app’s root directory. Add the following line:
In file Procfile
write:
web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}
This file must be named Procfile
exactly with a capital P
, lowercase rocfile
, and no file extension.
To use the Procfile locally, use the local
Heroku CLI command.
In addition to running commands in the Procfile
, heroku local
can also manage environment variables locally through a .env
file. Set RACK_ENV
to development
for the local environment and the PORT
for Puma. Test with the RACK_ENV
set to production
before pushing to Heroku; production
is the environment in which the Heroku app will run.
$ 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
since this is 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:50:35 PM web.1 | Puma starting in single mode...
12:50:35 PM web.1 | * Puma version: 5.6.5 (ruby 3.1.2-p20) ("Birdie's Version")
12:50:35 PM web.1 | * Min threads: 5
12:50:35 PM web.1 | * Max threads: 5
12:50:35 PM web.1 | * Environment: development
12:50:35 PM web.1 | * PID: 20843
12:50:36 PM web.1 | * Listening on http://0.0.0.0:3000
12:50:36 PM web.1 | Use Ctrl-C to stop
A successful test will look similar to the previous example. Press Ctrl+C
or CMD+C
to exit and deploy the changes to Heroku:
$ git add .
$ git commit -m "use puma via procfile"
$ git push heroku main || git push heroku master
Check ps
. The web
process is now using the new command specifying Puma as the web server:
$ heroku ps
Eco dyno hours quota remaining this month: 1000h 0m (100%)
Eco dyno usage for this app: 0h 0m (0%)
For more information on dyno sleeping and how to upgrade, see:
https://devcenter.heroku.com/articles/dyno-sleeping
=== web (Eco): bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development} (1)
web.1: starting 2022/11/09 12:50:53 -0800 (~ 5s ago)
The logs also reflect that Puma is in use.
$ heroku logs
Rails asset pipeline
When deploying to Heroku, there are several options for invoking the Rails asset pipeline. Please review the Rails 3.1+ Asset Pipeline on Heroku Cedar article for general information on the asset pipeline.
Rails 7 removed the config.assets.initialize_on_precompile
option because it is no longer needed. Additionally, any failure in asset compilation will now cause the push to fail. For Rails 7 asset pipeline support, see the Ruby Support page.
Troubleshooting
If an app deployed to Heroku crashes (heroku ps
shows state crashed
), review the app’s logs to determine what went wrong. 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 sticky. To get rid of 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 to Heroku.
Next Steps
Congratulations! You deployed your first Rails 7 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.