Getting Started on Heroku with Rails 8.x
Last updated November 13, 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 8 on Heroku. For information on running previous versions of Rails on Heroku, see the tutorial for Rails 7.x or Rails 6.x.
The tutorial assumes that you have:
- Basic familiarity with Ruby, Ruby on Rails, and Git
- A locally installed version of Ruby 3.2.0+, Rubygems, Bundler, and Rails 8+
- 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 8 installed by running rails -v
before creating an app. If necessary, install Rails 8 with gem install
:
$ gem install rails --no-document
Successfully installed zeitwerk-2.7.1
Successfully installed rack-3.1.8
Successfully installed rackup-2.2.0
Successfully installed reline-0.5.11
Successfully installed irb-1.14.1
Successfully installed uri-1.0.1
Successfully installed concurrent-ruby-1.3.4
Successfully installed tzinfo-2.0.6
Successfully installed securerandom-0.3.2
PSA: I18n will be dropping support for Ruby < 3.2 in the next major release (April 2025), due to Ruby's end of life for 3.1 and below (https://endoflife.date/ruby). Please upgrade to Ruby 3.2 or newer by April 2025 to continue using future versions of this gem.
Successfully installed i18n-1.14.6
Successfully installed connection_pool-2.4.1
Successfully installed benchmark-0.4.0
Successfully installed activesupport-8.0.0
Successfully installed useragent-0.16.10
Successfully installed crass-1.0.6
Successfully installed loofah-2.23.1
Successfully installed rails-html-sanitizer-1.6.0
Successfully installed rails-dom-testing-2.2.0
Successfully installed rack-session-2.0.0
Successfully installed erubi-1.13.0
Successfully installed builder-3.3.0
Successfully installed actionview-8.0.0
Successfully installed actionpack-8.0.0
Successfully installed railties-8.0.0
Successfully installed marcel-1.0.4
Successfully installed timeout-0.4.2
Successfully installed activemodel-8.0.0
Successfully installed activerecord-8.0.0
Successfully installed globalid-1.2.1
Successfully installed activejob-8.0.0
Successfully installed activestorage-8.0.0
Successfully installed actiontext-8.0.0
Successfully installed mail-2.8.1
Successfully installed actionmailer-8.0.0
Successfully installed actionmailbox-8.0.0
Successfully installed websocket-extensions-0.1.5
Building native extensions. This could take a while...
Successfully installed websocket-driver-0.7.6
Building native extensions. This could take a while...
Successfully installed nio4r-2.7.4
Successfully installed actioncable-8.0.0
Successfully installed rails-8.0.0
40 gems installed
Create a Rails app:
$ rails new myapp --database=postgresql
Move into the application directory
$ cd myapp
$ ls -1
Dockerfile
Gemfile
Gemfile.lock
README.md
Rakefile
app
bin
config
config.ru
db
lib
log
public
script
storage
test
tmp
vendor
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:
primary: &primary_production
<<: *default
database: myapp_production
username: myapp
password: <%= ENV["MYAPP_DATABASE_PASSWORD"] %>
cache:
<<: *primary_production
database: myapp_production_cache
migrations_paths: db/cache_migrate
queue:
<<: *primary_production
database: myapp_production_queue
migrations_paths: db/queue_migrate
cable:
<<: *primary_production
database: myapp_production_cable
migrations_paths: db/cable_migrate
Be careful here. If the value of adapter
is postgres
and not postgresql
, the application won’t work.
Create a Welcome Page
Rails 8 no longer has a static index page in production by default. Apps upgraded to Rails 8 keep their existing page configurations, but new Rails 8 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 8 requires Ruby 3.2.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.6"
Update the RUBY VERSION
in the Gemfile.lock
by running:
$ bundle update --ruby
Fetching gem metadata from https://rubygems.org/.........
Resolving dependencies...
Bundle updated!
Verify the change:
$ cat Gemfile.lock | grep RUBY -a1
RUBY VERSION
ruby 3.2.6p234
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 8. When you deploy a Rails 8 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] [--no-lazy-fetch]
[--no-optional-locks] [--no-advice] [--bare] [--git-dir=<path>]
[--work-tree=<path>] [--namespace=<name>] [--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
Dockerfile
Gemfile
Gemfile-e
Gemfile.lock
Procfile
README.md
Rakefile
app
bin
config
config.ru
db
lib
log
public
script
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, sleepy-lake-73349
https://sleepy-lake-73349-75b72c5796f5.herokuapp.com/ | https://git.heroku.com/sleepy-lake-73349.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/sleepy-lake-73349.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.
A mini
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 sleepy-lake-73349... ~$0.007/hour (max $5/month)
Database should be available soon
postgresql-clear-11142 is being created in the background. The app will restart when complete...
Use heroku addons:info postgresql-clear-11142 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 109 paths from 71a5ba5
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Building on the Heroku-24 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.6
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.8
remote: Fetching concurrent-ruby 1.3.4
remote: Fetching benchmark 0.4.0
remote: Installing benchmark 0.4.0
remote: Installing base64 0.2.0
remote: Fetching connection_pool 2.4.1
remote: Fetching drb 2.2.1
remote: Installing bigdecimal 3.1.8 with native extensions
remote: Installing connection_pool 2.4.1
remote: Installing drb 2.2.1
remote: Fetching logger 1.6.1
remote: Installing logger 1.6.1
remote: Installing concurrent-ruby 1.3.4
remote: Fetching minitest 5.25.1
remote: Fetching securerandom 0.3.2
remote: Installing securerandom 0.3.2
remote: Installing minitest 5.25.1
remote: Fetching uri 1.0.1
remote: Installing uri 1.0.1
remote: Fetching builder 3.3.0
remote: Installing builder 3.3.0
remote: Fetching erubi 1.13.0
remote: Installing erubi 1.13.0
remote: Fetching racc 1.8.1
remote: Fetching crass 1.0.6
remote: Installing racc 1.8.1 with native extensions
remote: Installing crass 1.0.6
remote: Fetching rack 3.1.8
remote: Fetching useragent 0.16.10
remote: Installing useragent 0.16.10
remote: Installing rack 3.1.8
remote: Fetching nio4r 2.7.4
remote: Installing nio4r 2.7.4 with native extensions
remote: Fetching websocket-extensions 0.1.5
remote: Installing websocket-extensions 0.1.5
remote: Fetching zeitwerk 2.7.1
remote: Installing zeitwerk 2.7.1
remote: Fetching timeout 0.4.2
remote: Installing timeout 0.4.2
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.4.0
remote: Installing date 3.4.0 with native extensions
remote: Fetching bcrypt_pbkdf 1.1.1
remote: Installing bcrypt_pbkdf 1.1.1 with native extensions
remote: Fetching msgpack 1.7.5
remote: Installing msgpack 1.7.5 with native extensions
remote: Fetching dotenv 3.1.4
remote: Installing dotenv 3.1.4
remote: Fetching ed25519 1.3.0
remote: Installing ed25519 1.3.0 with native extensions
remote: Fetching raabro 1.4.0
remote: Installing raabro 1.4.0
remote: Fetching stringio 3.1.2
remote: Installing stringio 3.1.2 with native extensions
remote: Fetching io-console 0.7.2
remote: Installing io-console 0.7.2 with native extensions
remote: Fetching thor 1.3.2
remote: Installing thor 1.3.2
remote: Fetching net-ssh 7.3.0
remote: Installing net-ssh 7.3.0
remote: Fetching ostruct 0.6.1
remote: Installing ostruct 0.6.1
remote: Fetching pg 1.5.9
remote: Installing pg 1.5.9 with native extensions
remote: Fetching thruster 0.1.8 (x86_64-linux)
remote: Installing thruster 0.1.8 (x86_64-linux)
remote: Fetching i18n 1.14.6
remote: Installing i18n 1.14.6
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 rackup 2.2.0
remote: Installing rackup 2.2.0
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.7 (x86_64-linux)
remote: Installing nokogiri 1.16.7 (x86_64-linux)
remote: Fetching puma 6.4.3
remote: Installing puma 6.4.3 with native extensions
remote: Fetching bootsnap 1.18.4
remote: Installing bootsnap 1.18.4 with native extensions
remote: Fetching psych 5.2.0
remote: Installing psych 5.2.0 with native extensions
remote: Fetching net-scp 4.0.0
remote: Installing net-scp 4.0.0
remote: Fetching net-sftp 4.0.0
remote: Installing net-sftp 4.0.0
remote: Fetching reline 0.5.11
remote: Installing reline 0.5.11
remote: Fetching et-orbi 1.2.11
remote: Installing et-orbi 1.2.11
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.5.1
remote: Installing net-imap 0.5.1
remote: Fetching loofah 2.23.1
remote: Installing loofah 2.23.1
remote: Fetching activesupport 8.0.0
remote: Installing activesupport 8.0.0
remote: Fetching sshkit 1.23.2
remote: Installing sshkit 1.23.2
remote: Fetching fugit 1.11.1
remote: Installing fugit 1.11.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 8.0.0
remote: Installing activemodel 8.0.0
remote: Fetching kamal 2.3.0
remote: Installing kamal 2.3.0
remote: Fetching actionview 8.0.0
remote: Installing actionview 8.0.0
remote: Fetching activejob 8.0.0
remote: Installing activejob 8.0.0
remote: Fetching activerecord 8.0.0
remote: Installing activerecord 8.0.0
remote: Fetching actionpack 8.0.0
remote: Installing actionpack 8.0.0
remote: Fetching jbuilder 2.13.0
remote: Installing jbuilder 2.13.0
remote: Fetching actioncable 8.0.0
remote: Installing actioncable 8.0.0
remote: Fetching activestorage 8.0.0
remote: Installing activestorage 8.0.0
remote: Fetching actionmailer 8.0.0
remote: Installing actionmailer 8.0.0
remote: Fetching actionmailbox 8.0.0
remote: Installing actionmailbox 8.0.0
remote: Fetching actiontext 8.0.0
remote: Installing actiontext 8.0.0
remote: Fetching rdoc 6.7.0
remote: Installing rdoc 6.7.0
remote: Fetching irb 1.14.1
remote: Installing irb 1.14.1
remote: Fetching railties 8.0.0
remote: Installing railties 8.0.0
remote: Fetching importmap-rails 2.0.3
remote: Fetching propshaft 1.1.0
remote: Installing importmap-rails 2.0.3
remote: Installing propshaft 1.1.0
remote: Fetching rails 8.0.0
remote: Installing rails 8.0.0
remote: Fetching solid_cable 3.0.2
remote: Fetching solid_cache 1.0.6
remote: Installing solid_cable 3.0.2
remote: Installing solid_cache 1.0.6
remote: Fetching solid_queue 1.0.1
remote: Fetching stimulus-rails 1.3.4
remote: Installing solid_queue 1.0.1
remote: Installing stimulus-rails 1.3.4
remote: Fetching turbo-rails 2.0.11
remote: Installing turbo-rails 2.0.11
remote: Bundle complete! 21 Gemfile dependencies, 86 gems now installed.
remote: Gems in the groups 'development' and 'test' were not installed.
remote: Bundled gems are installed into `./vendor/bundle`
remote: Post-install message from i18n:
remote: PSA: I18n will be dropping support for Ruby < 3.2 in the next major release (April 2025), due to Ruby's end of life for 3.1 and below (https://endoflife.date/ruby). Please upgrade to Ruby 3.2 or newer by April 2025 to continue using future versions of this gem.
remote: Post-install message from solid_cache:
remote: Upgrading from Solid Cache v0.3 or earlier? There are new database migrations in v0.4.
remote: See https://github.com/rails/solid_cache/blob/main/upgrading_to_version_0.4.x.md for upgrade instructions.
remote: Post-install message from solid_queue:
remote: Upgrading to Solid Queue 0.9.0? There are some breaking changes about how recurring tasks are configured.
remote:
remote: Upgrading to Solid Queue 0.8.0 from < 0.6.0? You need to upgrade to 0.6.0 first.
remote:
remote: Upgrading to Solid Queue 0.4.x, 0.5.x, 0.6.x or 0.7.x? There are some breaking changes about how Solid Queue is started,
remote: configuration and new migrations.
remote:
remote: --> Check https://github.com/rails/solid_queue/blob/main/UPGRADING.md
remote: for upgrade instructions.
remote: Bundle completed (21.75s)
remote: Cleaning up the bundler cache.
remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote: Running: rake assets:precompile
remote: Writing application-8b441ae0.css
remote: Writing stimulus-autoloader-9d447422.js
remote: Writing stimulus-importmap-autoloader-64cc03e1.js
remote: Writing stimulus-loading-1fc53fe7.js
remote: Writing stimulus-d59b3b7f.js
remote: Writing stimulus.min-4b1e420e.js
remote: Writing stimulus.min-2395e199.js.map
remote: Writing turbo-86e38c3c.js
remote: Writing turbo.min-fae85750.js
remote: Writing turbo.min-5dd5a71a.js.map
remote: Writing actiontext.esm-f1c04d34.js
remote: Writing actiontext-a4ee937e.js
remote: Writing trix-4f753c97.js
remote: Writing trix-c4e7be2d.css
remote: Writing action_cable-5212cfee.js
remote: Writing actioncable.esm-e0ec9819.js
remote: Writing actioncable-ac25813f.js
remote: Writing activestorage.esm-f2909226.js
remote: Writing activestorage-32201f68.js
remote: Writing rails-ujs.esm-e925103b.js
remote: Writing rails-ujs-20eaf715.js
remote: Writing controllers/hello_controller-708796bd.js
remote: Writing controllers/index-ee64e1f1.js
remote: Writing controllers/application-3affb389.js
remote: Writing application-bfcdf840.js
remote: Asset precompilation completed (0.84s)
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: 49.3M
remote: -----> Launching...
remote: Released v4
remote: https://sleepy-lake-73349-75b72c5796f5.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/sleepy-lake-73349.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/11/13 09:12:02 -0600 (~ 6s 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-11-13T15:11:59.943576+00:00 heroku[web.1]: Starting process with command `bundle exec puma -C config/puma.rb`
2024-11-13T15:12:00.721350+00:00 app[web.1]: Puma starting in single mode...
2024-11-13T15:12:00.721367+00:00 app[web.1]: * Puma version: 6.4.3 (ruby 3.2.6-p234) ("The Eagle of Durango")
2024-11-13T15:12:00.721367+00:00 app[web.1]: * Min threads: 3
2024-11-13T15:12:00.721368+00:00 app[web.1]: * Max threads: 3
2024-11-13T15:12:00.721368+00:00 app[web.1]: * Environment: production
2024-11-13T15:12:00.721384+00:00 app[web.1]: * PID: 2
2024-11-13T15:12:02.751909+00:00 app[web.1]: * Listening on http://0.0.0.0:51767
2024-11-13T15:12:02.754052+00:00 app[web.1]: Use Ctrl-C to stop
2024-11-13T15:12:02.778173+00:00 heroku[web.1]: State changed from starting to up
2024-11-13T15:12:10.600072+00:00 app[web.1]: [b4650052-7f2a-4ae5-99ee-d42b21e67d0c] Started GET "/" for 204.14.236.210 at 2024-11-13 15:12:10 +0000
2024-11-13T15:12:10.602074+00:00 app[web.1]: [b4650052-7f2a-4ae5-99ee-d42b21e67d0c] Processing by WelcomeController#index as HTML
2024-11-13T15:12:10.618901+00:00 app[web.1]: [b4650052-7f2a-4ae5-99ee-d42b21e67d0c] Rendered layout layouts/application.html.erb (Duration: 6.4ms | GC: 0.6ms)
2024-11-13T15:12:10.619103+00:00 app[web.1]: [b4650052-7f2a-4ae5-99ee-d42b21e67d0c] Completed 200 OK in 17ms (Views: 7.0ms | ActiveRecord: 0.0ms (0 queries, 0 cached) | GC: 1.1ms)
2024-11-13T15:12:10.620721+00:00 heroku[router]: at=info method=GET path="/" host=sleepy-lake-73349-75b72c5796f5.herokuapp.com request_id=b4650052-7f2a-4ae5-99ee-d42b21e67d0c fwd="204.14.236.210" dyno=web.1 connect=0ms service=22ms status=200 bytes=2944 protocol=https
2024-11-13T15:12:10.674598+00:00 heroku[router]: at=info method=GET path="/assets/application-8b441ae0.css" host=sleepy-lake-73349-75b72c5796f5.herokuapp.com request_id=d234470d-9961-4a43-8a38-461a94e875b0 fwd="204.14.236.210" dyno=web.1 connect=0ms service=1ms status=200 bytes=725 protocol=https
2024-11-13T15:12:10.705544+00:00 heroku[router]: at=info method=GET path="/assets/application-bfcdf840.js" host=sleepy-lake-73349-75b72c5796f5.herokuapp.com request_id=aa6666a0-c59a-4200-a9a8-1f4113f3cc3c fwd="204.14.236.210" dyno=web.1 connect=0ms service=1ms status=200 bytes=398 protocol=https
2024-11-13T15:12:10.729602+00:00 heroku[router]: at=info method=GET path="/assets/stimulus.min-4b1e420e.js" host=sleepy-lake-73349-75b72c5796f5.herokuapp.com request_id=02785a18-11ce-4913-ada8-81f4378e1268 fwd="204.14.236.210" dyno=web.1 connect=0ms service=2ms status=200 bytes=45900 protocol=https
2024-11-13T15:12:10.733494+00:00 heroku[router]: at=info method=GET path="/assets/stimulus-loading-1fc53fe7.js" host=sleepy-lake-73349-75b72c5796f5.herokuapp.com request_id=cc2e1287-46cf-46ee-8702-081cf886a64d fwd="204.14.236.210" dyno=web.1 connect=0ms service=1ms status=200 bytes=3557 protocol=https
2024-11-13T15:12:10.735514+00:00 heroku[router]: at=info method=GET path="/assets/turbo.min-fae85750.js" host=sleepy-lake-73349-75b72c5796f5.herokuapp.com request_id=f0339fd0-d962-4e4d-ab7b-ec3050062303 fwd="204.14.236.210" dyno=web.1 connect=0ms service=1ms status=200 bytes=104283 protocol=https
2024-11-13T15:12:10.744800+00:00 heroku[router]: at=info method=GET path="/assets/controllers/hello_controller-708796bd.js" host=sleepy-lake-73349-75b72c5796f5.herokuapp.com request_id=90d62558-8be2-434f-922b-ed001d57a405 fwd="204.14.236.210" dyno=web.1 connect=0ms service=0ms status=200 bytes=398 protocol=https
2024-11-13T15:12:10.745304+00:00 heroku[router]: at=info method=GET path="/assets/controllers/application-3affb389.js" host=sleepy-lake-73349-75b72c5796f5.herokuapp.com request_id=aa5edfd7-bc8a-427f-be6a-32385c5ed7ff fwd="204.14.236.210" dyno=web.1 connect=0ms service=1ms status=200 bytes=459 protocol=https
2024-11-13T15:12:10.749834+00:00 heroku[router]: at=info method=GET path="/assets/controllers/index-ee64e1f1.js" host=sleepy-lake-73349-75b72c5796f5.herokuapp.com request_id=a1c86c2b-9dc8-468e-a7ec-b72234fba972 fwd="204.14.236.210" dyno=web.1 connect=0ms service=0ms status=200 bytes=513 protocol=https
2024-11-13T15:12:10.953738+00:00 heroku[router]: at=info method=GET path="/icon.svg" host=sleepy-lake-73349-75b72c5796f5.herokuapp.com request_id=6c1305e9-e440-4956-adc2-c4b5c562008c fwd="204.14.236.210" dyno=web.1 connect=0ms service=1ms status=200 bytes=361 protocol=https
2024-11-13T15:12:11.037536+00:00 heroku[router]: at=info method=GET path="/icon.png" host=sleepy-lake-73349-75b72c5796f5.herokuapp.com request_id=ec2bc8f8-7361-4b93-97a5-cdf22eb438af fwd="204.14.236.210" dyno=web.1 connect=0ms service=1ms status=200 bytes=4402 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
9:12:15 AM web.1 | Puma starting in single mode...
9:12:15 AM web.1 | * Puma version: 6.4.3 (ruby 3.2.6-p234) ("The Eagle of Durango")
9:12:15 AM web.1 | * Min threads: 3
9:12:15 AM web.1 | * Max threads: 3
9:12:15 AM web.1 | * Environment: development
9:12:15 AM web.1 | * PID: 98056
9:12:15 AM web.1 | * Listening on http://0.0.0.0:3000
9:12:15 AM web.1 | Use Ctrl-C to stop
Press Ctrl+C
or Cmd+C
to exit.
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 8 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.