Getting Started on Heroku with Rails 7.x
Last updated September 29, 2024
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
- 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
- Troubleshooting
- Next Steps
- Delete Your App and Add-on
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.
The tutorial assumes that you have:
- Basic familiarity with Ruby, Ruby on Rails, and Git
- A locally installed version of Ruby 3.0.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)
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 developer@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.
If you’re behind a firewall that uses a proxy to connect with external HTTP/HTTPS services, set the HTTP_PROXY
or HTTPS_PROXY
environment variables in your local development environment before running the heroku
command.
Create a New or Upgrade an Existing Rails App
Ensure you have Rails 7 installed by running rails -v
before creating an app. If necessary, install Rails 7 with gem install
:
$ gem install rails --no-document
Successfully installed rails-7.1.3.2
1 gem installed
Create a Rails app:
$ rails 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.
Within the Gemfile
remove:
gem 'sqlite3'
Replace it with:
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 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 PostgreSQL.
# To create additional roles in PostgreSQL see `$ createuser --help`.
# When left blank, PostgreSQL will use the default role. This is
# the same name as the operating system user running Rails.
#username: myapp
# The password associated with the PostgreSQL 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 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 don’t automatically generate a 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 code:
<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.
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 to debug the error. Rails outputs logs in the same terminal where rails server
was started.
Specify the Ruby Version
Rails 7 requires Ruby 2.7.0 or above. Heroku installs a recent version of Ruby by default. Specify an exact version with the ruby
DSL in Gemfile
. For example:
ruby "3.2.4"
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
. For example:
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 [-v | --version] [-h | --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>]
[--config-env=<name>=<envvar>] <command> [<args>]
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
Dockerfile
Gemfile
Gemfile-e
Gemfile.lock
Procfile
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 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.
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 apps:create
Creating app... done, murmuring-taiga-66903
https://murmuring-taiga-66903-369309d7b6b1.herokuapp.com/ | https://git.heroku.com/murmuring-taiga-66903.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/murmuring-taiga-66903.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 updated the default branch name to main
. If the project 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 ⬢ murmuring-taiga-66903... ~$0.007/hour (max $5/month)
Database should be available soon
postgresql-corrugated-66300 is being created in the background. The app will restart when complete...
Use heroku addons:info postgresql-corrugated-66300 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 87 paths from 5068e81
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.5.6
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-3.2.4
remote: -----> Installing dependencies using bundler 2.5.6
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.2.1
remote: Installing rake 13.2.1
remote: Fetching base64 0.2.0
remote: Fetching bigdecimal 3.1.7
remote: Fetching concurrent-ruby 1.2.3
remote: Fetching connection_pool 2.4.1
remote: Installing base64 0.2.0
remote: Fetching drb 2.2.1
remote: Installing bigdecimal 3.1.7 with native extensions
remote: Installing connection_pool 2.4.1
remote: Fetching minitest 5.22.3
remote: Installing concurrent-ruby 1.2.3
remote: Installing drb 2.2.1
remote: Fetching mutex_m 0.2.0
remote: Installing mutex_m 0.2.0
remote: Fetching builder 3.2.4
remote: Installing minitest 5.22.3
remote: Installing builder 3.2.4
remote: Fetching erubi 1.12.0
remote: Installing erubi 1.12.0
remote: Fetching racc 1.7.3
remote: Fetching crass 1.0.6
remote: Fetching rack 3.0.10
remote: Installing racc 1.7.3 with native extensions
remote: Installing crass 1.0.6
remote: Fetching nio4r 2.7.1
remote: Installing rack 3.0.10
remote: Installing nio4r 2.7.1 with native extensions
remote: Fetching websocket-extensions 0.1.5
remote: Installing websocket-extensions 0.1.5
remote: Fetching zeitwerk 2.6.13
remote: Installing zeitwerk 2.6.13
remote: Fetching timeout 0.4.1
remote: Installing timeout 0.4.1
remote: Fetching marcel 1.0.4
remote: Installing marcel 1.0.4
remote: Fetching mini_mime 1.1.5
remote: Installing mini_mime 1.1.5
remote: Fetching date 3.3.4
remote: Installing date 3.3.4 with native extensions
remote: Fetching msgpack 1.7.2
remote: Installing msgpack 1.7.2 with native extensions
remote: Fetching stringio 3.1.0
remote: Installing stringio 3.1.0 with native extensions
remote: Fetching io-console 0.7.2
remote: Installing io-console 0.7.2 with native extensions
remote: Fetching webrick 1.8.1
remote: Installing webrick 1.8.1
remote: Fetching thor 1.3.1
remote: Installing thor 1.3.1
remote: Fetching pg 1.5.6
remote: Installing pg 1.5.6 with native extensions
remote: Fetching redis-client 0.22.1
remote: Installing redis-client 0.22.1
remote: Fetching i18n 1.14.4
remote: Installing i18n 1.14.4
remote: Fetching tzinfo 2.0.6
remote: Installing tzinfo 2.0.6
remote: Fetching rack-session 2.0.0
remote: Installing rack-session 2.0.0
remote: Fetching rack-test 2.1.0
remote: Installing rack-test 2.1.0
remote: Fetching sprockets 4.2.1
remote: Installing sprockets 4.2.1
remote: Fetching websocket-driver 0.7.6
remote: Installing websocket-driver 0.7.6 with native extensions
remote: Fetching net-protocol 0.2.2
remote: Installing net-protocol 0.2.2
remote: Fetching nokogiri 1.16.4 (x86_64-linux)
remote: Installing nokogiri 1.16.4 (x86_64-linux)
remote: Fetching puma 6.4.2
remote: Installing puma 6.4.2 with native extensions
remote: Fetching psych 5.1.2
remote: Installing psych 5.1.2 with native extensions
remote: Fetching bootsnap 1.18.3
remote: Installing bootsnap 1.18.3 with native extensions
remote: Fetching rackup 2.1.0
remote: Installing rackup 2.1.0
remote: Fetching reline 0.5.5
remote: Installing reline 0.5.5
remote: Fetching redis 5.2.0
remote: Installing redis 5.2.0
remote: Fetching net-pop 0.1.2
remote: Installing net-pop 0.1.2
remote: Fetching net-smtp 0.5.0
remote: Installing net-smtp 0.5.0
remote: Fetching net-imap 0.4.10
remote: Installing net-imap 0.4.10
remote: Fetching loofah 2.22.0
remote: Installing loofah 2.22.0
remote: Fetching activesupport 7.1.3.2
remote: Installing activesupport 7.1.3.2
remote: Fetching rdoc 6.6.3.1
remote: Installing rdoc 6.6.3.1
remote: Fetching mail 2.8.1
remote: Installing mail 2.8.1
remote: Fetching rails-html-sanitizer 1.6.0
remote: Installing rails-html-sanitizer 1.6.0
remote: Fetching rails-dom-testing 2.2.0
remote: Installing rails-dom-testing 2.2.0
remote: Fetching globalid 1.2.1
remote: Installing globalid 1.2.1
remote: Fetching activemodel 7.1.3.2
remote: Installing activemodel 7.1.3.2
remote: Fetching irb 1.13.0
remote: Installing irb 1.13.0
remote: Fetching actionview 7.1.3.2
remote: Installing actionview 7.1.3.2
remote: Fetching activejob 7.1.3.2
remote: Installing activejob 7.1.3.2
remote: Fetching activerecord 7.1.3.2
remote: Installing activerecord 7.1.3.2
remote: Fetching actionpack 7.1.3.2
remote: Installing actionpack 7.1.3.2
remote: Fetching jbuilder 2.12.0
remote: Installing jbuilder 2.12.0
remote: Fetching actioncable 7.1.3.2
remote: Installing actioncable 7.1.3.2
remote: Fetching activestorage 7.1.3.2
remote: Installing activestorage 7.1.3.2
remote: Fetching actionmailer 7.1.3.2
remote: Installing actionmailer 7.1.3.2
remote: Fetching railties 7.1.3.2
remote: Installing railties 7.1.3.2
remote: Fetching sprockets-rails 3.4.2
remote: Installing sprockets-rails 3.4.2
remote: Fetching actionmailbox 7.1.3.2
remote: Installing actionmailbox 7.1.3.2
remote: Fetching actiontext 7.1.3.2
remote: Installing actiontext 7.1.3.2
remote: Fetching importmap-rails 2.0.1
remote: Installing importmap-rails 2.0.1
remote: Fetching stimulus-rails 1.3.3
remote: Installing stimulus-rails 1.3.3
remote: Fetching turbo-rails 2.0.5
remote: Installing turbo-rails 2.0.5
remote: Fetching rails 7.1.3.2
remote: Installing rails 7.1.3.2
remote: Bundle complete! 15 Gemfile dependencies, 70 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.78s)
remote: Cleaning up the bundler cache.
remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote: Running: rake assets:precompile
remote: I, [2024-05-03T15:57:41.661688 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/manifest-b84bfa46a33d7f0dc4d2e7b8889486c9a957a5e40713d58f54be71b66954a1ff.js
remote: I, [2024-05-03T15:57:41.661889 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/manifest-b84bfa46a33d7f0dc4d2e7b8889486c9a957a5e40713d58f54be71b66954a1ff.js.gz
remote: I, [2024-05-03T15:57:41.662182 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/application-e0cf9d8fcb18bf7f909d8d91a5e78499f82ac29523d475bf3a9ab265d5e2b451.css
remote: I, [2024-05-03T15:57:41.662283 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/application-e0cf9d8fcb18bf7f909d8d91a5e78499f82ac29523d475bf3a9ab265d5e2b451.css.gz
remote: I, [2024-05-03T15:57:41.662400 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/application-37f365cbecf1fa2810a8303f4b6571676fa1f9c56c248528bc14ddb857531b95.js
remote: I, [2024-05-03T15:57:41.662479 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/application-37f365cbecf1fa2810a8303f4b6571676fa1f9c56c248528bc14ddb857531b95.js.gz
remote: I, [2024-05-03T15:57:41.662644 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/controllers/application-368d98631bccbf2349e0d4f8269afb3fe9625118341966de054759d96ea86c7e.js
remote: I, [2024-05-03T15:57:41.662719 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/controllers/application-368d98631bccbf2349e0d4f8269afb3fe9625118341966de054759d96ea86c7e.js.gz
remote: I, [2024-05-03T15:57:41.662812 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/controllers/hello_controller-549135e8e7c683a538c3d6d517339ba470fcfb79d62f738a0a089ba41851a554.js
remote: I, [2024-05-03T15:57:41.662883 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/controllers/hello_controller-549135e8e7c683a538c3d6d517339ba470fcfb79d62f738a0a089ba41851a554.js.gz
remote: I, [2024-05-03T15:57:41.663073 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/controllers/index-2db729dddcc5b979110e98de4b6720f83f91a123172e87281d5a58410fc43806.js
remote: I, [2024-05-03T15:57:41.663168 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/controllers/index-2db729dddcc5b979110e98de4b6720f83f91a123172e87281d5a58410fc43806.js.gz
remote: I, [2024-05-03T15:57:41.663294 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/turbo-6f8f1796078d2d3f7cb9b6badcd2d5e76287a3c58d97baffaa59dd12bd4135f5.js
remote: I, [2024-05-03T15:57:41.663391 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/turbo-6f8f1796078d2d3f7cb9b6badcd2d5e76287a3c58d97baffaa59dd12bd4135f5.js.gz
remote: I, [2024-05-03T15:57:41.663508 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/turbo.min-918a6cf2f2be8ed9555c9a11eee69c7dc2f01770802815576efc931876b327fb.js
remote: I, [2024-05-03T15:57:41.663599 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/turbo.min-918a6cf2f2be8ed9555c9a11eee69c7dc2f01770802815576efc931876b327fb.js.gz
remote: I, [2024-05-03T15:57:41.663755 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/turbo.min.js-16b5fa18e8e95a86fc9f56ced2c8a9c83a99b73834da35ba1b2ff5dab550d7fa.map
remote: I, [2024-05-03T15:57:41.663834 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/turbo.min.js-16b5fa18e8e95a86fc9f56ced2c8a9c83a99b73834da35ba1b2ff5dab550d7fa.map.gz
remote: I, [2024-05-03T15:57:41.663938 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/actiontext-78de0ebeae470799f9ec25fd0e20ae2d931df88c2ff9315918d1054a2fca2596.js
remote: I, [2024-05-03T15:57:41.664285 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/actiontext-78de0ebeae470799f9ec25fd0e20ae2d931df88c2ff9315918d1054a2fca2596.js.gz
remote: I, [2024-05-03T15:57:41.664591 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/actiontext.esm-328ef022563f73c1b9b45ace742bd21330da0f6bd6c1c96d352d52fc8b8857e5.js
remote: I, [2024-05-03T15:57:41.664895 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/actiontext.esm-328ef022563f73c1b9b45ace742bd21330da0f6bd6c1c96d352d52fc8b8857e5.js.gz
remote: I, [2024-05-03T15:57:41.665193 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/trix-e17a480fcb4e30c8571f0fed42dc81de5faeef93755ca30fe9623eb3f5c709e5.js
remote: I, [2024-05-03T15:57:41.665449 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/trix-e17a480fcb4e30c8571f0fed42dc81de5faeef93755ca30fe9623eb3f5c709e5.js.gz
remote: I, [2024-05-03T15:57:41.665759 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/trix-91ad87f30d9c53c7b05f534dc3c9994e4e90d50dff03a7222c1326f342f9c223.css
remote: I, [2024-05-03T15:57:41.666178 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/trix-91ad87f30d9c53c7b05f534dc3c9994e4e90d50dff03a7222c1326f342f9c223.css.gz
remote: I, [2024-05-03T15:57:41.666487 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/stimulus-f75215805563870a61ee9dc5a207ce46d4675c7e667558a54344fd1e7baa697f.js
remote: I, [2024-05-03T15:57:41.666736 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/stimulus-f75215805563870a61ee9dc5a207ce46d4675c7e667558a54344fd1e7baa697f.js.gz
remote: I, [2024-05-03T15:57:41.667021 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/stimulus-autoloader-c584942b568ba74879da31c7c3d51366737bacaf6fbae659383c0a5653685693.js
remote: I, [2024-05-03T15:57:41.667289 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/stimulus-autoloader-c584942b568ba74879da31c7c3d51366737bacaf6fbae659383c0a5653685693.js.gz
remote: I, [2024-05-03T15:57:41.667598 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/stimulus-importmap-autoloader-db2076c783bf2dbee1226e2add52fef290b5d31b5bcd1edd999ac8a6dd31c44a.js
remote: I, [2024-05-03T15:57:41.667847 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/stimulus-importmap-autoloader-db2076c783bf2dbee1226e2add52fef290b5d31b5bcd1edd999ac8a6dd31c44a.js.gz
remote: I, [2024-05-03T15:57:41.668146 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/stimulus-loading-3576ce92b149ad5d6959438c6f291e2426c86df3b874c525b30faad51b0d96b3.js
remote: I, [2024-05-03T15:57:41.668411 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/stimulus-loading-3576ce92b149ad5d6959438c6f291e2426c86df3b874c525b30faad51b0d96b3.js.gz
remote: I, [2024-05-03T15:57:41.668696 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/stimulus.min-dd364f16ec9504dfb72672295637a1c8838773b01c0b441bd41008124c407894.js
remote: I, [2024-05-03T15:57:41.668958 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/stimulus.min-dd364f16ec9504dfb72672295637a1c8838773b01c0b441bd41008124c407894.js.gz
remote: I, [2024-05-03T15:57:41.669307 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/stimulus-autoloader-c584942b568ba74879da31c7c3d51366737bacaf6fbae659383c0a5653685693.js
remote: I, [2024-05-03T15:57:41.669566 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/stimulus-autoloader-c584942b568ba74879da31c7c3d51366737bacaf6fbae659383c0a5653685693.js.gz
remote: I, [2024-05-03T15:57:41.669871 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/stimulus-importmap-autoloader-db2076c783bf2dbee1226e2add52fef290b5d31b5bcd1edd999ac8a6dd31c44a.js
remote: I, [2024-05-03T15:57:41.670159 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/stimulus-importmap-autoloader-db2076c783bf2dbee1226e2add52fef290b5d31b5bcd1edd999ac8a6dd31c44a.js.gz
remote: I, [2024-05-03T15:57:41.670453 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/stimulus-loading-3576ce92b149ad5d6959438c6f291e2426c86df3b874c525b30faad51b0d96b3.js
remote: I, [2024-05-03T15:57:41.670711 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/stimulus-loading-3576ce92b149ad5d6959438c6f291e2426c86df3b874c525b30faad51b0d96b3.js.gz
remote: I, [2024-05-03T15:57:41.671027 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/stimulus.min.js-2cc63625fa177963b45da974806e7aee846cbf1d4930815733d0fdf3fb232325.map
remote: I, [2024-05-03T15:57:41.671563 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/stimulus.min.js-2cc63625fa177963b45da974806e7aee846cbf1d4930815733d0fdf3fb232325.map.gz
remote: I, [2024-05-03T15:57:41.671980 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/activestorage-503a4fe23aabfbcb752dad255f01835904e6961d5f20d1de13987a691c27d9cd.js
remote: I, [2024-05-03T15:57:41.674247 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/activestorage-503a4fe23aabfbcb752dad255f01835904e6961d5f20d1de13987a691c27d9cd.js.gz
remote: I, [2024-05-03T15:57:41.682044 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/activestorage.esm-b3f7f0a5ef90530b509c5e681c4b3ef5d5046851e5b70d57fdb45e32b039c883.js
remote: I, [2024-05-03T15:57:41.682485 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/activestorage.esm-b3f7f0a5ef90530b509c5e681c4b3ef5d5046851e5b70d57fdb45e32b039c883.js.gz
remote: I, [2024-05-03T15:57:41.682654 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/actioncable-1c7f008c6deb7b55c6878be38700ff6bf56b75444a086fa1f46e3b781365a3ea.js
remote: I, [2024-05-03T15:57:41.682751 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/actioncable-1c7f008c6deb7b55c6878be38700ff6bf56b75444a086fa1f46e3b781365a3ea.js.gz
remote: I, [2024-05-03T15:57:41.682849 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/actioncable.esm-06609b0ecaffe2ab952021b9c8df8b6c68f65fc23bee728fc678a2605e1ce132.js
remote: I, [2024-05-03T15:57:41.682911 #1307] INFO -- : Writing /tmp/build_95a1e009/public/assets/actioncable.esm-06609b0ecaffe2ab952021b9c8df8b6c68f65fc23bee728fc678a2605e1ce132.js.gz
remote: Asset precompilation completed (1.62s)
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: 42.8M
remote: -----> Launching...
remote: Released v6
remote: https://murmuring-taiga-66903-369309d7b6b1.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/murmuring-taiga-66903.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 have 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 2024/05/03 10:57:54 -0500 (~ 4s ago)
In this example, a single web
process is running.
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
2024-05-03T15:56:53.688568+00:00 app[api]: Release v1 created by user developer@example.com2024-05-03T15:56:53.688568+00:00 app[api]: Initial release by user developer@example.com2024-05-03T15:56:53.994609+00:00 app[api]: Enable Logplex by user developer@example.com2024-05-03T15:56:53.994609+00:00 app[api]: Release v2 created by user developer@example.com2024-05-03T15:56:56.210643+00:00 app[api]: Running release v3 commands by user developer@example.com2024-05-03T15:56:56.210643+00:00 app[api]: Attach DATABASE (@ref:postgresql-corrugated-66300) by user developer@example.com2024-05-03T15:56:56.222146+00:00 app[api]: @ref:postgresql-corrugated-66300 completed provisioning, setting DATABASE_URL. by user developer@example.com2024-05-03T15:56:56.222146+00:00 app[api]: Release v4 created by user developer@example.com2024-05-03T15:56:58.000000+00:00 app[api]: Build started by user developer@example.com2024-05-03T15:57:50.020078+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.com2024-05-03T15:57:50.020078+00:00 app[api]: Release v5 created by user developer@example.com2024-05-03T15:57:50.320771+00:00 app[api]: Release v6 created by user developer@example.com2024-05-03T15:57:50.320771+00:00 app[api]: Deploy a0b1f706 by user developer@example.com2024-05-03T15:57:50.332914+00:00 app[api]: Scaled to console@0:Basic rake@0:Basic web@1:Basic by user developer@example.com2024-05-03T15:57:53.000000+00:00 app[api]: Build succeeded
2024-05-03T15:57:53.170135+00:00 heroku[web.1]: Starting process with command `bundle exec puma -C config/puma.rb`
2024-05-03T15:57:54.081636+00:00 app[web.1]: [2] Puma starting in cluster mode...
2024-05-03T15:57:54.081709+00:00 app[web.1]: [2] * Puma version: 6.4.2 (ruby 3.2.4-p170) ("The Eagle of Durango")
2024-05-03T15:57:54.081710+00:00 app[web.1]: [2] * Min threads: 5
2024-05-03T15:57:54.081710+00:00 app[web.1]: [2] * Max threads: 5
2024-05-03T15:57:54.081710+00:00 app[web.1]: [2] * Environment: production
2024-05-03T15:57:54.081711+00:00 app[web.1]: [2] * Master PID: 2
2024-05-03T15:57:54.081711+00:00 app[web.1]: [2] * Workers: 4
2024-05-03T15:57:54.081711+00:00 app[web.1]: [2] * Restarts: (✔) hot (✔) phased
2024-05-03T15:57:54.081846+00:00 app[web.1]: [2] * Listening on http://0.0.0.0:25875
2024-05-03T15:57:54.081912+00:00 app[web.1]: [2] Use Ctrl-C to stop
2024-05-03T15:57:54.440430+00:00 heroku[web.1]: State changed from starting to up
2024-05-03T15:57:55.942470+00:00 app[web.1]: [2] - Worker 3 (PID: 11) booted in 1.86s, phase: 0
2024-05-03T15:57:55.948153+00:00 app[web.1]: [2] - Worker 1 (PID: 7) booted in 1.86s, phase: 0
2024-05-03T15:57:55.974855+00:00 app[web.1]: [2] - Worker 0 (PID: 5) booted in 1.89s, phase: 0
2024-05-03T15:57:55.978895+00:00 app[web.1]: [2] - Worker 2 (PID: 9) booted in 1.89s, phase: 0
2024-05-03T15:58:01.558246+00:00 app[web.1]: I, [2024-05-03T15:58:01.558112 #11] INFO -- : [bdac5d62-b659-4c68-a2cb-f74bccac26ae] Started GET "/" for 13.110.54.14 at 2024-05-03 15:58:01 +0000
2024-05-03T15:58:01.560244+00:00 app[web.1]: I, [2024-05-03T15:58:01.560189 #11] INFO -- : [bdac5d62-b659-4c68-a2cb-f74bccac26ae] Processing by WelcomeController#index as HTML
2024-05-03T15:58:01.565205+00:00 app[web.1]: I, [2024-05-03T15:58:01.565163 #11] INFO -- : [bdac5d62-b659-4c68-a2cb-f74bccac26ae] Rendered layout layouts/application.html.erb (Duration: 4.0ms | Allocations: 2555)
2024-05-03T15:58:01.565377+00:00 app[web.1]: I, [2024-05-03T15:58:01.565353 #11] INFO -- : [bdac5d62-b659-4c68-a2cb-f74bccac26ae] Completed 200 OK in 5ms (Views: 4.7ms | ActiveRecord: 0.0ms | Allocations: 3396)
2024-05-03T15:58:01.566615+00:00 heroku[router]: at=info method=GET path="/" host=murmuring-taiga-66903-369309d7b6b1.herokuapp.com request_id=bdac5d62-b659-4c68-a2cb-f74bccac26ae fwd="13.110.54.14" dyno=web.1 connect=0ms service=10ms status=200 bytes=3553 protocol=https
2024-05-03T15:58:01.686259+00:00 heroku[router]: at=info method=GET path="/assets/application-e0cf9d8fcb18bf7f909d8d91a5e78499f82ac29523d475bf3a9ab265d5e2b451.css" host=murmuring-taiga-66903-369309d7b6b1.herokuapp.com request_id=a65e92f7-edd3-4eec-8280-f82c2d91ae1d fwd="13.110.54.14" dyno=web.1 connect=0ms service=1ms status=200 bytes=639 protocol=https
2024-05-03T15:58:01.800415+00:00 heroku[router]: at=info method=GET path="/assets/application-37f365cbecf1fa2810a8303f4b6571676fa1f9c56c248528bc14ddb857531b95.js" host=murmuring-taiga-66903-369309d7b6b1.herokuapp.com request_id=a5315144-6eb5-4b5e-96f2-7b6a151f1313 fwd="13.110.54.14" dyno=web.1 connect=0ms service=1ms status=200 bytes=387 protocol=https
2024-05-03T15:58:01.904570+00:00 heroku[router]: at=info method=GET path="/assets/turbo.min-918a6cf2f2be8ed9555c9a11eee69c7dc2f01770802815576efc931876b327fb.js" host=murmuring-taiga-66903-369309d7b6b1.herokuapp.com request_id=e34b2d4f-f578-4ff4-a1ee-4a471e5a9c5c fwd="13.110.54.14" dyno=web.1 connect=0ms service=1ms status=200 bytes=27538 protocol=https
2024-05-03T15:58:01.906297+00:00 heroku[router]: at=info method=GET path="/assets/stimulus-loading-3576ce92b149ad5d6959438c6f291e2426c86df3b874c525b30faad51b0d96b3.js" host=murmuring-taiga-66903-369309d7b6b1.herokuapp.com request_id=3e10eae2-c334-47b2-a6c5-1e16ae77feb9 fwd="13.110.54.14" dyno=web.1 connect=1ms service=1ms status=200 bytes=1279 protocol=https
2024-05-03T15:58:01.907023+00:00 heroku[router]: at=info method=GET path="/assets/stimulus.min-dd364f16ec9504dfb72672295637a1c8838773b01c0b441bd41008124c407894.js" host=murmuring-taiga-66903-369309d7b6b1.herokuapp.com request_id=9469b478-adb0-41e9-ba03-6fa166223d7d fwd="13.110.54.14" dyno=web.1 connect=0ms service=1ms status=200 bytes=11321 protocol=https
2024-05-03T15:58:01.912815+00:00 heroku[router]: at=info method=GET path="/assets/controllers/index-2db729dddcc5b979110e98de4b6720f83f91a123172e87281d5a58410fc43806.js" host=murmuring-taiga-66903-369309d7b6b1.herokuapp.com request_id=af6a90f3-9621-4632-ac1f-ab5dffd97fd0 fwd="13.110.54.14" dyno=web.1 connect=0ms service=1ms status=200 bytes=508 protocol=https
2024-05-03T15:58:01.913743+00:00 heroku[router]: at=info method=GET path="/assets/controllers/hello_controller-549135e8e7c683a538c3d6d517339ba470fcfb79d62f738a0a089ba41851a554.js" host=murmuring-taiga-66903-369309d7b6b1.herokuapp.com request_id=f3caf4a6-60b3-4bce-a2d9-132e4f437808 fwd="13.110.54.14" dyno=web.1 connect=1ms service=0ms status=200 bytes=397 protocol=https
2024-05-03T15:58:01.914329+00:00 heroku[router]: at=info method=GET path="/assets/controllers/application-368d98631bccbf2349e0d4f8269afb3fe9625118341966de054759d96ea86c7e.js" host=murmuring-taiga-66903-369309d7b6b1.herokuapp.com request_id=797c9084-6983-4032-a9eb-42d64ee5c258 fwd="13.110.54.14" dyno=web.1 connect=1ms service=1ms status=200 bytes=413 protocol=https
2024-05-03T15:58:02.201060+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=murmuring-taiga-66903-369309d7b6b1.herokuapp.com request_id=7e05b330-ce00-4274-9fef-c43af38dc2f9 fwd="13.110.54.14" dyno=web.1 connect=0ms service=4ms status=200 bytes=207 protocol=https
Append -t
/--tail
to the command to see a full, live stream of the app’s logs:
$ 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
The run bash
Heroku CLI command is also helpful for debugging. The command starts a new one-off dyno with an interactive bash session.
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
10:58:05 AM web.1 | Puma starting in single mode...
10:58:05 AM web.1 | * Puma version: 6.4.2 (ruby 3.2.4-p170) ("The Eagle of Durango")
10:58:05 AM web.1 | * Min threads: 5
10:58:05 AM web.1 | * Max threads: 5
10:58:05 AM web.1 | * Environment: development
10:58:05 AM web.1 | * PID: 86703
10:58:05 AM web.1 | * Listening on http://0.0.0.0:3000
10:58:05 AM 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 article for general information on the asset pipeline.
Rails 7 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 7 asset pipeline support, see the Ruby Support page.
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 on deploying a Rails 7 application! To continue exploring, 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.
Remember to delete your example app, and database as soon as you’re done with the tutorial, to control costs.
Delete 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 these commands:
$ heroku addons --all
$ heroku apps --all
You’re now ready to deploy your app.