Getting Started on Heroku with Rails 8.x (beta)
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 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 --pre --no-document
Successfully installed rails-8.0.0.beta1
1 gem 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.5"
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.5p208
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, limitless-savannah-22282
https://limitless-savannah-22282-b18b7da98ae1.herokuapp.com/ | https://git.heroku.com/limitless-savannah-22282.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/limitless-savannah-22282.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 limitless-savannah-22282... ~$0.007/hour (max $5/month)
Database should be available soon
postgresql-horizontal-06989 is being created in the background. The app will restart when complete...
Use heroku addons:info postgresql-horizontal-06989 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 ae228b1
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.5
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 benchmark 0.3.0
remote: Fetching bigdecimal 3.1.8
remote: Fetching concurrent-ruby 1.3.4
remote: Installing base64 0.2.0
remote: Installing benchmark 0.3.0
remote: Fetching drb 2.2.1
remote: Fetching connection_pool 2.4.1
remote: Installing connection_pool 2.4.1
remote: Installing drb 2.2.1
remote: Fetching logger 1.6.1
remote: Installing bigdecimal 3.1.8 with native extensions
remote: Fetching minitest 5.25.1
remote: Installing logger 1.6.1
remote: Installing minitest 5.25.1
remote: Installing concurrent-ruby 1.3.4
remote: Fetching securerandom 0.3.1
remote: Installing securerandom 0.3.1
remote: Fetching uri 0.13.1
remote: Fetching builder 3.3.0
remote: Installing uri 0.13.1
remote: Installing builder 3.3.0
remote: Fetching erubi 1.13.0
remote: Fetching racc 1.8.1
remote: Installing erubi 1.13.0
remote: Fetching crass 1.0.6
remote: Installing crass 1.0.6
remote: Installing racc 1.8.1 with native extensions
remote: Fetching rack 3.1.7
remote: Fetching useragent 0.16.10
remote: Installing rack 3.1.7
remote: Installing useragent 0.16.10
remote: Fetching nio4r 2.7.3
remote: Installing nio4r 2.7.3 with native extensions
remote: Fetching websocket-extensions 0.1.5
remote: Installing websocket-extensions 0.1.5
remote: Fetching zeitwerk 2.6.18
remote: Installing zeitwerk 2.6.18
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 bcrypt_pbkdf 1.1.1
remote: Installing bcrypt_pbkdf 1.1.1 with native extensions
remote: Fetching msgpack 1.7.2
remote: Installing msgpack 1.7.2 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.1
remote: Installing stringio 3.1.1 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.2
remote: Installing webrick 1.8.2
remote: Fetching thor 1.3.2
remote: Installing thor 1.3.2
remote: Fetching net-ssh 7.2.3
remote: Installing net-ssh 7.2.3
remote: Fetching ostruct 0.6.0
remote: Installing ostruct 0.6.0
remote: Fetching pg 1.5.8
remote: Installing pg 1.5.8 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 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 psych 5.1.2
remote: Installing psych 5.1.2 with native extensions
remote: Fetching bootsnap 1.18.4
remote: Installing bootsnap 1.18.4 with native extensions
remote: Fetching rackup 2.1.0
remote: Installing rackup 2.1.0
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 et-orbi 1.2.11
remote: Installing et-orbi 1.2.11
remote: Fetching net-imap 0.4.16
remote: Fetching net-pop 0.1.2
remote: Installing net-pop 0.1.2
remote: Installing net-imap 0.4.16
remote: Fetching net-smtp 0.5.0
remote: Installing net-smtp 0.5.0
remote: Fetching loofah 2.22.0
remote: Installing loofah 2.22.0
remote: Fetching activesupport 8.0.0.beta1
remote: Fetching reline 0.5.10
remote: Installing reline 0.5.10
remote: Installing activesupport 8.0.0.beta1
remote: Fetching rdoc 6.7.0
remote: Installing rdoc 6.7.0
remote: Fetching sshkit 1.23.1
remote: Installing sshkit 1.23.1
remote: Fetching fugit 1.11.1
remote: Fetching mail 2.8.1
remote: Installing fugit 1.11.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 mail 2.8.1
remote: Installing globalid 1.2.1
remote: Fetching activemodel 8.0.0.beta1
remote: Installing activemodel 8.0.0.beta1
remote: Fetching irb 1.14.1
remote: Fetching kamal 2.0.0
remote: Installing irb 1.14.1
remote: Installing kamal 2.0.0
remote: Fetching actionview 8.0.0.beta1
remote: Fetching activejob 8.0.0.beta1
remote: Installing activejob 8.0.0.beta1
remote: Installing actionview 8.0.0.beta1
remote: Fetching activerecord 8.0.0.beta1
remote: Fetching actionpack 8.0.0.beta1
remote: Installing activerecord 8.0.0.beta1
remote: Installing actionpack 8.0.0.beta1
remote: Fetching jbuilder 2.13.0
remote: Installing jbuilder 2.13.0
remote: Fetching actioncable 8.0.0.beta1
remote: Installing actioncable 8.0.0.beta1
remote: Fetching actionmailer 8.0.0.beta1
remote: Fetching railties 8.0.0.beta1
remote: Installing actionmailer 8.0.0.beta1
remote: Fetching activestorage 8.0.0.beta1
remote: Installing railties 8.0.0.beta1
remote: Installing activestorage 8.0.0.beta1
remote: Fetching actionmailbox 8.0.0.beta1
remote: Installing actionmailbox 8.0.0.beta1
remote: Fetching actiontext 8.0.0.beta1
remote: Installing actiontext 8.0.0.beta1
remote: Fetching importmap-rails 2.0.1
remote: Installing importmap-rails 2.0.1
remote: Fetching propshaft 1.0.1
remote: Installing propshaft 1.0.1
remote: Fetching solid_cable 3.0.2
remote: Installing solid_cable 3.0.2
remote: Fetching solid_queue 1.0.0
remote: Fetching solid_cache 1.0.6
remote: Installing solid_cache 1.0.6
remote: Installing solid_queue 1.0.0
remote: Fetching stimulus-rails 1.3.4
remote: Installing stimulus-rails 1.3.4
remote: Fetching turbo-rails 2.0.10
remote: Fetching rails 8.0.0.beta1
remote: Installing turbo-rails 2.0.10
remote: Installing rails 8.0.0.beta1
remote: Bundle complete! 21 Gemfile dependencies, 87 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 (37.76s)
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-loading-1fc53fe7.js
remote: Writing stimulus-importmap-autoloader-64cc03e1.js
remote: Writing stimulus.min-2395e199.js.map
remote: Writing stimulus-autoloader-9d447422.js
remote: Writing stimulus-d59b3b7f.js
remote: Writing stimulus.min-4b1e420e.js
remote: Writing turbo.min-e867e79e.js
remote: Writing turbo.min-c05b9b29.js.map
remote: Writing turbo-2d374ee7.js
remote: Writing trix-80a3c05d.js
remote: Writing actiontext-a4ee937e.js
remote: Writing actiontext.esm-f1c04d34.js
remote: Writing trix-c4e7be2d.css
remote: Writing action_cable-5212cfee.js
remote: Writing actioncable-ac25813f.js
remote: Writing actioncable.esm-e0ec9819.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/application-3affb389.js
remote: Writing controllers/index-ee64e1f1.js
remote: Writing controllers/hello_controller-708796bd.js
remote: Writing application-bfcdf840.js
remote: Asset precompilation completed (0.70s)
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.1M
remote: -----> Launching...
remote: Released v4
remote: https://limitless-savannah-22282-b18b7da98ae1.herokuapp.com/ deployed to Heroku
remote:
remote: This app is using the Heroku-22 stack, however a newer stack is available.
remote: To upgrade to Heroku-24, see:
remote: https://devcenter.heroku.com/articles/upgrading-to-the-latest-stack
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/limitless-savannah-22282.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/09/29 12:49:13 -0500 (~ 3s 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-09-29T17:48:12.097971+00:00 app[api]: Release v1 created by user developer@example.com2024-09-29T17:48:12.097971+00:00 app[api]: Initial release by user developer@example.com2024-09-29T17:48:12.314521+00:00 app[api]: Enable Logplex by user developer@example.com2024-09-29T17:48:12.314521+00:00 app[api]: Release v2 created by user developer@example.com2024-09-29T17:48:16.000000+00:00 app[api]: Build started by user developer@example.com2024-09-29T17:49:07.706770+00:00 app[api]: Release v3 created by user developer@example.com2024-09-29T17:49:07.706770+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-09-29T17:49:08.049516+00:00 app[api]: Release v4 created by user developer@example.com2024-09-29T17:49:08.049516+00:00 app[api]: Deploy c05b71e8 by user developer@example.com2024-09-29T17:49:08.061459+00:00 app[api]: Scaled to console@0:Basic rake@0:Basic web@1:Basic by user developer@example.com2024-09-29T17:49:10.148036+00:00 heroku[web.1]: Starting process with command `bundle exec puma -C config/puma.rb`
2024-09-29T17:49:10.882648+00:00 app[web.1]: Puma starting in single mode...
2024-09-29T17:49:10.882685+00:00 app[web.1]: * Puma version: 6.4.3 (ruby 3.2.5-p208) ("The Eagle of Durango")
2024-09-29T17:49:10.882685+00:00 app[web.1]: * Min threads: 3
2024-09-29T17:49:10.882686+00:00 app[web.1]: * Max threads: 3
2024-09-29T17:49:10.882686+00:00 app[web.1]: * Environment: production
2024-09-29T17:49:10.882686+00:00 app[web.1]: * PID: 2
2024-09-29T17:49:11.000000+00:00 app[api]: Build succeeded
2024-09-29T17:49:12.889092+00:00 app[web.1]: * Listening on http://0.0.0.0:20150
2024-09-29T17:49:12.891337+00:00 app[web.1]: Use Ctrl-C to stop
2024-09-29T17:49:13.026778+00:00 heroku[web.1]: State changed from starting to up
2024-09-29T17:49:19.355344+00:00 app[web.1]: [10dbca69-088d-4a40-ab8a-27b101b42001] Started GET "/" for 13.110.54.13 at 2024-09-29 17:49:19 +0000
2024-09-29T17:49:19.357369+00:00 app[web.1]: [10dbca69-088d-4a40-ab8a-27b101b42001] Processing by WelcomeController#index as HTML
2024-09-29T17:49:19.374567+00:00 app[web.1]: [10dbca69-088d-4a40-ab8a-27b101b42001] Rendered layout layouts/application.html.erb (Duration: 6.0ms | GC: 0.4ms)
2024-09-29T17:49:19.374784+00:00 app[web.1]: [10dbca69-088d-4a40-ab8a-27b101b42001] Completed 200 OK in 17ms (Views: 6.7ms | ActiveRecord: 0.0ms (0 queries, 0 cached) | GC: 0.7ms)
2024-09-29T17:49:19.376773+00:00 heroku[router]: at=info method=GET path="/" host=limitless-savannah-22282-b18b7da98ae1.herokuapp.com request_id=10dbca69-088d-4a40-ab8a-27b101b42001 fwd="13.110.54.13" dyno=web.1 connect=0ms service=23ms status=200 bytes=2944 protocol=https
2024-09-29T17:49:19.585689+00:00 heroku[router]: at=info method=GET path="/assets/application-8b441ae0.css" host=limitless-savannah-22282-b18b7da98ae1.herokuapp.com request_id=61ecddfc-b632-4e9d-b470-7b32b6637772 fwd="13.110.54.13" dyno=web.1 connect=0ms service=1ms status=200 bytes=725 protocol=https
2024-09-29T17:49:19.686131+00:00 heroku[router]: at=info method=GET path="/assets/application-bfcdf840.js" host=limitless-savannah-22282-b18b7da98ae1.herokuapp.com request_id=2a6b9f25-a40a-4534-a780-c69221a51d61 fwd="13.110.54.13" dyno=web.1 connect=0ms service=1ms status=200 bytes=398 protocol=https
2024-09-29T17:49:19.751679+00:00 heroku[router]: at=info method=GET path="/assets/turbo.min-e867e79e.js" host=limitless-savannah-22282-b18b7da98ae1.herokuapp.com request_id=75eaf8db-41e0-4c45-8b4d-045f558eb08d fwd="13.110.54.13" dyno=web.1 connect=1ms service=1ms status=200 bytes=104283 protocol=https
2024-09-29T17:49:19.760156+00:00 heroku[router]: at=info method=GET path="/assets/stimulus.min-4b1e420e.js" host=limitless-savannah-22282-b18b7da98ae1.herokuapp.com request_id=5683844c-e96a-4e6d-b82f-c8cd96cf29fd fwd="13.110.54.13" dyno=web.1 connect=0ms service=1ms status=200 bytes=45900 protocol=https
2024-09-29T17:49:19.767147+00:00 heroku[router]: at=info method=GET path="/assets/stimulus-loading-1fc53fe7.js" host=limitless-savannah-22282-b18b7da98ae1.herokuapp.com request_id=5fc94a15-673d-4064-baf3-ab2c477ac09e fwd="13.110.54.13" dyno=web.1 connect=0ms service=1ms status=200 bytes=3557 protocol=https
2024-09-29T17:49:19.768581+00:00 heroku[router]: at=info method=GET path="/assets/controllers/application-3affb389.js" host=limitless-savannah-22282-b18b7da98ae1.herokuapp.com request_id=bfaf653b-fa6b-408a-8f3c-ce959184665b fwd="13.110.54.13" dyno=web.1 connect=0ms service=0ms status=200 bytes=459 protocol=https
2024-09-29T17:49:19.776181+00:00 heroku[router]: at=info method=GET path="/assets/controllers/hello_controller-708796bd.js" host=limitless-savannah-22282-b18b7da98ae1.herokuapp.com request_id=c4d52504-6dd5-4104-b7df-4abd311f977d fwd="13.110.54.13" dyno=web.1 connect=0ms service=0ms status=200 bytes=398 protocol=https
2024-09-29T17:49:19.794715+00:00 heroku[router]: at=info method=GET path="/assets/controllers/index-ee64e1f1.js" host=limitless-savannah-22282-b18b7da98ae1.herokuapp.com request_id=6a991477-c3ac-4b8c-97c1-9b9a6a0b933d fwd="13.110.54.13" dyno=web.1 connect=0ms service=0ms status=200 bytes=513 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
12:49:23 PM web.1 | Puma starting in single mode...
12:49:23 PM web.1 | * Puma version: 6.4.3 (ruby 3.2.5-p208) ("The Eagle of Durango")
12:49:23 PM web.1 | * Min threads: 3
12:49:23 PM web.1 | * Max threads: 3
12:49:23 PM web.1 | * Environment: development
12:49:23 PM web.1 | * PID: 40800
12:49:23 PM web.1 | * Listening on http://0.0.0.0:3000
12:49:23 PM 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.