Getting Started on Heroku with Rails 6.x
Last updated May 22, 2023
Table of Contents
- Local setup
- Create a new Rails app (or upgrade an existing one)
- Add the pg gem
- Create a welcome page
- Heroku gems
- Specify your Ruby version
- Store your app in Git
- Create a Heroku app
- Provision a Database
- Deploy your application to Heroku
- Migrate your database
- Visit your application
- View logs
- Dyno sleeping and scaling
- Run the Rails console
- Run Rake commands
- Configure your webserver
- Rails asset pipeline
- Troubleshooting
- Next Steps
- Deleting your app and Add-on
As of November 28th, 2022, free Heroku dynos, free Heroku Postgres and free Heroku Data for Redis plans are no longer available.
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.
The latest version of Rails available is Rails 7. If you are starting a new application we recommend you use the most recently released version.
Ruby on Rails is a popular web framework written in Ruby. This guide covers using Rails 6 on Heroku. For information on running previous versions of Rails on Heroku, see the tutorial for Rails 5.x or Rails 4.x.
For this guide you will need:
- Basic familiarity with Ruby/Rails and Git
- A locally installed version of Ruby 2.5.0+, Rubygems, Bundler, and Rails 6+
- A verified Heroku Account
- A subscription to the Eco dynos plan (recommended)
Local setup
Install the Heroku CLI on your development machine.
Once installed, the heroku
command is available from your terminal. Log in using your Heroku account’s email address and password:
$ heroku login
heroku: Enter your Heroku credentials
Email: developer@example.com
Password:
Could not find an existing public key.
Would you like to generate one? [Yn]
Generating new SSH public key.
Uploading ssh public key /Users/adam/.ssh/id_rsa.pub
Press Enter at the prompt to upload your existing ssh
key or create a new one, used for pushing code later on.
Create a new Rails app (or upgrade an existing one)
To create a new app, first make sure that you’re using Rails 6.x by running rails -v
. If necessary, you can get the new version of rails by running the following:
$ gem install rails -v 6.1.7.3 --no-document
Successfully installed rails-6.1.7.3
1 gem installed
Then create a new app and move into its root directory:
$ rails new myapp --database=postgresql
Then move into your application directory.
$ 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 database locally:
$ bin/rails db:create
Database 'myapp_development' already exists
Database 'myapp_test' already exists
Add the pg gem
If you’re using an existing app that was created without specifying --database=postgresql
, you need to add the pg
gem to your Rails project. Edit your Gemfile
and change this line:
gem 'sqlite3'
To this:
gem 'pg'
We highly recommend using PostgreSQL during development. Maintaining parity between your development and deployment environments prevents subtle bugs from being introduced because of differences between your environments. Install Postgres locally now if it is not already on your system.
Now re-install your dependencies (to generate a new Gemfile.lock
):
$ bundle install
For more information on why Postgres is recommended instead of Sqlite3, see why you cannot use Sqlite3 on Heroku.
In addition to using the pg
gem, ensure that your config/database.yml
file is using the postgresql
adapter. The development section of your config/database.yml
file should look something like this:
$ cat config/database.yml
# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
# gem install pg
# On macOS with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS with MacPorts:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: myapp_development
# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user running Rails.
#username: myapp
# The password associated with the postgres role (username).
#password:
# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost
# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432
# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public
# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# Defaults to warning.
#min_messages: notice
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: myapp_test
# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
# production:
# url: <%= ENV['MY_APP_DATABASE_URL'] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
Be careful here. If you omit the sql
at the end of postgresql
in the adapter
section, your application will not work.
Create a welcome page
Rails 6 no longer has a static index page in production by default. When you’re using a new app, there will not be a root page in production, so we need to create one. We will first create a controller called welcome
for our home page to live:
$ rails generate controller welcome
Next we’ll add an index page:
In file app/views/welcome/index.html.erb
write:
<h2>Hello World</h2>
<p>
The time is now: <%= Time.now %>
</p>
Now we need to make Rails route to this action. We’ll edit config/routes.rb
to set the index page to our new method:
In file config/routes.rb
, on line 2 add:
root 'welcome#index'
You can verify that the page is there by running your server:
$ rails server
And visiting http://localhost:3000 in your browser. If you do not see the page, use the logs that are output to your server to debug.
Heroku gems
Previous versions of Rails required you to add a gem to your project rails_12factor to enable static asset serving and logging on Heroku. If you are deploying a new application, this gem is not needed. If you are upgrading an existing application, you can remove this gem provided you have the appropriate configuration in your config/environments/production.rb
file:
# config/environments/production.rb
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end
Specify your Ruby version
Rails 6 requires Ruby 2.5.0 or above. Heroku has a recent version of Ruby installed by default, however you can specify an exact version by using the ruby
DSL in your Gemfile
. Depending on your version of Ruby that you are currently running it might look like this:
ruby "3.0.6"
You should also be running the same version of Ruby locally. You can check this by running $ ruby -v
. You can get more information on specifying your Ruby version on Heroku here.
Store your app in Git
Heroku relies on Git, a distributed source control management tool, for deploying your project. If your project is not already in Git, first verify that git
is on your system:
$ git --help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
[--super-prefix=<path>] [--config-env=<name>=<envvar>]
If you don’t see any output or get command not found
you need to install Git on your system.
Once you’ve verified that Git works, first make sure you are in your Rails app directory by running $ ls
:
The output should look like this:
$ ls
Gemfile
Gemfile-e
Gemfile.lock
README.md
Rakefile
app
babel.config.js
bin
config
config.ru
db
lib
log
node_modules
package.json
postcss.config.js
public
storage
test
tmp
vendor
yarn.lock
Now run these commands in your Rails app directory to initialize and commit your code to Git:
$ git init
$ git add .
$ git commit -m "init"
You can verify everything was committed correctly by running:
$ git status
On branch main
nothing to commit, working tree clean
Now that your application is committed to Git you can 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.
Make sure you are in the directory that contains your Rails app, then create an app on Heroku:
$ heroku create --stack heroku-20
Creating app... done, blooming-mountain-08184, stack is heroku-20
https://blooming-mountain-08184.herokuapp.com/ | https://git.heroku.com/blooming-mountain-08184.git
You can verify that the remote was added to your project by running:
$ git config --list --local | grep heroku
remote.heroku.url=https://git.heroku.com/blooming-mountain-08184.git
remote.heroku.fetch=+refs/heads/*:refs/remotes/heroku/*
If you see fatal: not in a git directory
then you are likely not in the correct directory. Otherwise, you can deploy your code. After you deploy your code, you need to migrate your database, make sure it is properly scaled, and use logs to debug any issues that come up.
Following changes in the industry, Heroku has updated our default git branch name to main
. If the project you’re deploying uses master
as its default branch name, use git push heroku master
.
Provision a Database
Provision a Postgresql database using Add-ons.
A mini
Postgres size costs $5 a month, prorated to the minute. At the end of this tutorial, you will be prompted to delete your database to minimize costs.
$ heroku addons:create heroku-postgresql:mini
Creating heroku-postgresql:mini on ⬢ shrouded-anchorage-34700... $5/month
Database has been created and is available
! This database is empty. If upgrading, you can transfer
! data from another database with pg:copy
Created postgresql-encircled-75487 as DATABASE_URL
Use heroku addons:docs heroku-postgresql to view documentation
Your Heroku app now has access to a Postgresql database. The credentials are stored in the DATABASE_URL
environment variable, which Rails will connect to by convention.
Deploy your application to Heroku
Deploy your code:
$ git push heroku main
remote: Updated 99 paths from f4b193a
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Building on the Heroku-20 stack
remote: -----> Determining which buildpack to use for this app
remote: ! Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used.
remote: Detected buildpacks: Ruby,Node.js
remote: See https://devcenter.heroku.com/articles/buildpacks#buildpack-detect-order
remote: -----> Ruby app detected
remote: -----> Installing bundler 2.3.25
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-3.0.6
remote: -----> Installing dependencies using bundler 2.3.25
remote: Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4
remote: Fetching gem metadata from https://rubygems.org/..........
remote: Fetching rake 13.0.6
remote: Installing rake 13.0.6
remote: Fetching minitest 5.18.0
remote: Fetching zeitwerk 2.6.8
remote: Fetching concurrent-ruby 1.2.2
remote: Fetching builder 3.2.4
remote: Installing minitest 5.18.0
remote: Installing zeitwerk 2.6.8
remote: Fetching erubi 1.12.0
remote: Installing builder 3.2.4
remote: Fetching mini_portile2 2.8.2
remote: Fetching racc 1.6.2
remote: Installing erubi 1.12.0
remote: Fetching crass 1.0.6
remote: Installing mini_portile2 2.8.2
remote: Installing concurrent-ruby 1.2.2
remote: Fetching rack 2.2.7
remote: Installing crass 1.0.6
remote: Fetching nio4r 2.5.9
remote: Installing racc 1.6.2 with native extensions
remote: Installing nio4r 2.5.9 with native extensions
remote: Installing rack 2.2.7
remote: Fetching websocket-extensions 0.1.5
remote: Fetching marcel 1.0.2
remote: Installing marcel 1.0.2
remote: Installing websocket-extensions 0.1.5
remote: Fetching mini_mime 1.1.2
remote: Fetching date 3.3.3
remote: Installing mini_mime 1.1.2
remote: Fetching timeout 0.3.2
remote: Installing date 3.3.3 with native extensions
remote: Installing timeout 0.3.2
remote: Fetching msgpack 1.7.1
remote: Installing msgpack 1.7.1 with native extensions
remote: Using bundler 2.3.25
remote: Fetching ffi 1.15.5
remote: Installing ffi 1.15.5 with native extensions
remote: Fetching method_source 1.0.0
remote: Installing method_source 1.0.0
remote: Fetching pg 1.5.3
remote: Installing pg 1.5.3 with native extensions
remote: Fetching thor 1.2.2
remote: Installing thor 1.2.2
remote: Fetching tilt 2.1.0
remote: Installing tilt 2.1.0
remote: Fetching semantic_range 3.0.0
remote: Installing semantic_range 3.0.0
remote: Fetching turbolinks-source 5.2.0
remote: Installing turbolinks-source 5.2.0
remote: Fetching i18n 1.13.0
remote: Installing i18n 1.13.0
remote: Fetching tzinfo 2.0.6
remote: Installing tzinfo 2.0.6
remote: Fetching rack-test 2.1.0
remote: Installing rack-test 2.1.0
remote: Fetching rack-proxy 0.7.6
remote: Installing rack-proxy 0.7.6
remote: Fetching sprockets 4.2.0
remote: Installing sprockets 4.2.0
remote: Fetching websocket-driver 0.7.5
remote: Installing websocket-driver 0.7.5 with native extensions
remote: Fetching net-protocol 0.2.1
remote: Installing net-protocol 0.2.1
remote: Fetching nokogiri 1.15.1
remote: Installing nokogiri 1.15.1 with native extensions
remote: Fetching puma 5.6.5
remote: Installing puma 5.6.5 with native extensions
remote: Fetching bootsnap 1.16.0
remote: Installing bootsnap 1.16.0 with native extensions
remote: Fetching turbolinks 5.2.1
remote: Installing turbolinks 5.2.1
remote: Fetching activesupport 6.1.7.3
remote: Installing activesupport 6.1.7.3
remote: Fetching net-pop 0.1.2
remote: Installing net-pop 0.1.2
remote: Fetching net-smtp 0.3.3
remote: Installing net-smtp 0.3.3
remote: Fetching net-imap 0.3.4
remote: Installing net-imap 0.3.4
remote: Fetching sassc 2.4.0
remote: Installing sassc 2.4.0 with native extensions
remote: Fetching globalid 1.1.0
remote: Installing globalid 1.1.0
remote: Fetching activemodel 6.1.7.3
remote: Installing activemodel 6.1.7.3
remote: Fetching mail 2.8.1
remote: Installing mail 2.8.1
remote: Fetching activejob 6.1.7.3
remote: Installing activejob 6.1.7.3
remote: Fetching activerecord 6.1.7.3
remote: Installing activerecord 6.1.7.3
remote: Fetching rails-dom-testing 2.0.3
remote: Fetching loofah 2.21.3
remote: Installing loofah 2.21.3
remote: Installing rails-dom-testing 2.0.3
remote: Fetching rails-html-sanitizer 1.5.0
remote: Installing rails-html-sanitizer 1.5.0
remote: Fetching actionview 6.1.7.3
remote: Installing actionview 6.1.7.3
remote: Fetching jbuilder 2.11.5
remote: Fetching actionpack 6.1.7.3
remote: Installing jbuilder 2.11.5
remote: Installing actionpack 6.1.7.3
remote: Fetching actioncable 6.1.7.3
remote: Fetching activestorage 6.1.7.3
remote: Fetching actionmailer 6.1.7.3
remote: Installing actioncable 6.1.7.3
remote: Installing activestorage 6.1.7.3
remote: Fetching railties 6.1.7.3
remote: Installing actionmailer 6.1.7.3
remote: Fetching sprockets-rails 3.4.2
remote: Fetching actionmailbox 6.1.7.3
remote: Installing railties 6.1.7.3
remote: Installing sprockets-rails 3.4.2
remote: Fetching actiontext 6.1.7.3
remote: Installing actionmailbox 6.1.7.3
remote: Installing actiontext 6.1.7.3
remote: Fetching rails 6.1.7.3
remote: Fetching webpacker 5.4.4
remote: Installing rails 6.1.7.3
remote: Installing webpacker 5.4.4
remote: Fetching sassc-rails 2.1.2
remote: Installing sassc-rails 2.1.2
remote: Fetching sass-rails 6.0.0
remote: Installing sass-rails 6.0.0
remote: Bundle complete! 17 Gemfile dependencies, 63 gems now installed.
remote: Gems in the groups 'development' and 'test' were not installed.
remote: Bundled gems are installed into `./vendor/bundle`
remote: Bundle completed (89.98s)
remote: Cleaning up the bundler cache.
remote: -----> Installing node-v16.18.1-linux-x64
remote: -----> Installing yarn-v1.22.19
remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote: Running: rake assets:precompile
remote: yarn install v1.22.19
remote: [1/4] Resolving packages...
remote: [2/4] Fetching packages...
remote: [3/4] Linking dependencies...
remote: [4/4] Building fresh packages...
remote: Done in 13.31s.
remote: I, [2023-05-22T21:07:10.680874 #1760] INFO -- : Writing /tmp/build_c3511f61/public/assets/manifest-b4bf6e57a53c2bdb55b8998cc94cd00883793c1c37c5e5aea3ef6749b4f6d92b.js
remote: I, [2023-05-22T21:07:10.681155 #1760] INFO -- : Writing /tmp/build_c3511f61/public/assets/manifest-b4bf6e57a53c2bdb55b8998cc94cd00883793c1c37c5e5aea3ef6749b4f6d92b.js.gz
remote: I, [2023-05-22T21:07:10.681305 #1760] INFO -- : Writing /tmp/build_c3511f61/public/assets/application-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css
remote: I, [2023-05-22T21:07:10.681388 #1760] INFO -- : Writing /tmp/build_c3511f61/public/assets/application-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css.gz
remote: I, [2023-05-22T21:07:10.681633 #1760] INFO -- : Writing /tmp/build_c3511f61/public/assets/welcome-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css
remote: I, [2023-05-22T21:07:10.682211 #1760] INFO -- : Writing /tmp/build_c3511f61/public/assets/welcome-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css.gz
remote: Compiling...
remote: Compiled all packs in /tmp/build_c3511f61/public/packs
remote: Hash: 9696eea663587e1a9695
remote: Version: webpack 4.46.0
remote: Time: 3219ms
remote: Built at: 05/22/2023 9:07:15 PM
remote: Asset Size Chunks Chunk Names
remote: js/application-449c89a2494baee3fa6f.js 68.2 KiB 0 [emitted] [immutable] application
remote: js/application-449c89a2494baee3fa6f.js.br 15.2 KiB [emitted]
remote: js/application-449c89a2494baee3fa6f.js.gz 17.5 KiB [emitted]
remote: js/application-449c89a2494baee3fa6f.js.map 203 KiB 0 [emitted] [dev] application
remote: js/application-449c89a2494baee3fa6f.js.map.br 43.3 KiB [emitted]
remote: js/application-449c89a2494baee3fa6f.js.map.gz 49.9 KiB [emitted]
remote: manifest.json 364 bytes [emitted]
remote: manifest.json.br 128 bytes [emitted]
remote: manifest.json.gz 142 bytes [emitted]
remote: Entrypoint application = js/application-449c89a2494baee3fa6f.js js/application-449c89a2494baee3fa6f.js.map
remote: [3] ./app/javascript/packs/application.js 480 bytes {0} [built]
remote: [4] ./app/javascript/channels/index.js 205 bytes {0} [built]
remote: [5] ./app/javascript/channels sync _channel\.js$ 160 bytes {0} [built]
remote: + 3 hidden modules
remote:
remote: Asset precompilation completed (19.88s)
remote: Cleaning assets
remote: Running: rake assets:clean
remote: -----> Detecting rails configuration
remote:
remote: ###### WARNING:
remote:
remote: No Procfile detected, using the default web server.
remote: We recommend explicitly declaring how to boot your server process via a Procfile.
remote: https://devcenter.heroku.com/articles/ruby-default-web-server
remote:
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> (none)
remote: Default types for buildpack -> console, rake, web
remote:
remote: -----> Compressing...
remote: Done: 95.8M
remote: -----> Launching...
remote: ! The following add-ons were automatically provisioned: heroku-postgresql. These add-ons may incur additional cost, which is prorated to the second. Run `heroku addons` for more info.
remote: Released v6
remote: https://blooming-mountain-08184.herokuapp.com/ deployed to Heroku
remote:
remote: This app is using the Heroku-20 stack, however a newer stack is available.
remote: To upgrade to Heroku-22, see:
remote: https://devcenter.heroku.com/articles/upgrading-to-the-latest-stack
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/blooming-mountain-08184.git
* [new branch] main -> main
It is always a good idea to check to see if there are any warnings or errors in the output. If everything went well you can migrate your database.
Migrate your database
If you are using the database in your application, trigger a migration by using the Heroku CLI to start a one-off dyno, which is a lightweight container that is the basic unit of composition on Heroku, and run db:migrate
:
$ heroku run rake db:migrate
Any commands after the heroku run
are executed on a Heroku dyno. You can obtain an interactive shell session by running $ heroku run bash
.
Visit your application
You’ve deployed your code to Heroku. You can now instruct Heroku to execute a process type. Heroku does this by running the associated command in a dyno, which is a lightweight container that is the basic unit of composition on Heroku.
Let’s ensure we have one dyno running the web
process type:
$ heroku ps:scale web=1
You can check the state of the app’s dynos. The heroku ps
command lists the running dynos of your application:
$ heroku ps
=== web (Basic): bin/rails server -p ${PORT:-5000} -e $RAILS_ENV (1)
web.1: up 2023/05/22 16:07:36 -0500 (~ 2s ago)
Here, one dyno is running.
We can now visit the app in our browser with heroku open
.
$ heroku open
You should now see the “Hello World” text we inserted above.
Heroku gives you a default web URL for simplicity while you are developing. When you are ready to scale up and use Heroku for production you can add your own custom domain.
View logs
If you run into any problems getting your app to perform properly, you will need to check the logs.
You can view information about your running app using one of the logging commands, heroku logs
:
$ heroku logs
2023-05-22T21:05:18.189449+00:00 app[api]: Initial release by user developer@example.com2023-05-22T21:05:18.189449+00:00 app[api]: Release v1 created by user developer@example.com2023-05-22T21:05:18.360650+00:00 app[api]: Release v2 created by user developer@example.com2023-05-22T21:05:18.360650+00:00 app[api]: Enable Logplex by user developer@example.com2023-05-22T21:05:20.000000+00:00 app[api]: Build started by user developer@example.com2023-05-22T21:07:26.868117+00:00 app[api]: Set LANG, RACK_ENV, RAILS_ENV, RAILS_LOG_TO_STDOUT, RAILS_SERVE_STATIC_FILES, SECRET_KEY_BASE config vars by user developer@example.com2023-05-22T21:07:26.868117+00:00 app[api]: Release v3 created by user developer@example.com2023-05-22T21:07:27.547058+00:00 app[api]: Attach DATABASE (@ref:postgresql-solid-63908) by user developer@example.com2023-05-22T21:07:27.547058+00:00 app[api]: Running release v4 commands by user developer@example.com2023-05-22T21:07:27.561621+00:00 app[api]: Release v5 created by user developer@example.com2023-05-22T21:07:27.561621+00:00 app[api]: @ref:postgresql-solid-63908 completed provisioning, setting DATABASE_URL. by user developer@example.com2023-05-22T21:07:27.922785+00:00 app[api]: Deploy 8b5ba9b4 by user developer@example.com2023-05-22T21:07:27.922785+00:00 app[api]: Release v6 created by user developer@example.com2023-05-22T21:07:27.939630+00:00 app[api]: Scaled to console@0:Basic rake@0:Basic web@1:Basic by user developer@example.com2023-05-22T21:07:32.000000+00:00 app[api]: Build succeeded
2023-05-22T21:07:33.670748+00:00 heroku[web.1]: Starting process with command `bin/rails server -p ${PORT:-5000} -e production`
2023-05-22T21:07:35.470076+00:00 app[web.1]: => Booting Puma
2023-05-22T21:07:35.470109+00:00 app[web.1]: => Rails 6.1.7.3 application starting in production
2023-05-22T21:07:35.470109+00:00 app[web.1]: => Run `bin/rails server --help` for more startup options
2023-05-22T21:07:36.240236+00:00 app[web.1]: Puma starting in single mode...
2023-05-22T21:07:36.240262+00:00 app[web.1]: * Puma version: 5.6.5 (ruby 3.0.6-p216) ("Birdie's Version")
2023-05-22T21:07:36.240263+00:00 app[web.1]: * Min threads: 5
2023-05-22T21:07:36.240263+00:00 app[web.1]: * Max threads: 5
2023-05-22T21:07:36.240263+00:00 app[web.1]: * Environment: production
2023-05-22T21:07:36.240263+00:00 app[web.1]: * PID: 2
2023-05-22T21:07:36.240428+00:00 app[web.1]: * Listening on http://0.0.0.0:17104
2023-05-22T21:07:36.244505+00:00 app[web.1]: Use Ctrl-C to stop
2023-05-22T21:07:36.480005+00:00 heroku[web.1]: State changed from starting to up
2023-05-22T21:07:41.772055+00:00 app[web.1]: I, [2023-05-22T21:07:41.771965 #2] INFO -- : [74c582c5-e0b6-4a85-baf9-04fef89a20d2] Started GET "/" for 165.225.32.83 at 2023-05-22 21:07:41 +0000
2023-05-22T21:07:41.774362+00:00 app[web.1]: I, [2023-05-22T21:07:41.774316 #2] INFO -- : [74c582c5-e0b6-4a85-baf9-04fef89a20d2] Processing by WelcomeController#index as HTML
2023-05-22T21:07:41.778347+00:00 app[web.1]: I, [2023-05-22T21:07:41.778310 #2] INFO -- : [74c582c5-e0b6-4a85-baf9-04fef89a20d2] Rendered welcome/index.html.erb within layouts/application (Duration: 0.3ms | Allocations: 214)
2023-05-22T21:07:41.779201+00:00 app[web.1]: I, [2023-05-22T21:07:41.779170 #2] INFO -- : [74c582c5-e0b6-4a85-baf9-04fef89a20d2] Rendered layout layouts/application.html.erb (Duration: 1.2ms | Allocations: 1076)
2023-05-22T21:07:41.779408+00:00 app[web.1]: I, [2023-05-22T21:07:41.779382 #2] INFO -- : [74c582c5-e0b6-4a85-baf9-04fef89a20d2] Completed 200 OK in 5ms (Views: 2.6ms | Allocations: 2721)
2023-05-22T21:07:41.782894+00:00 heroku[router]: at=info method=GET path="/" host=blooming-mountain-08184.herokuapp.com request_id=74c582c5-e0b6-4a85-baf9-04fef89a20d2 fwd="165.225.32.83" dyno=web.1 connect=0ms service=20ms status=200 bytes=1836 protocol=https
2023-05-22T21:07:42.071211+00:00 heroku[router]: at=info method=GET path="/assets/application-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css" host=blooming-mountain-08184.herokuapp.com request_id=2c88fcc7-451a-4f3f-a5e6-f34946366931 fwd="165.225.32.83" dyno=web.1 connect=0ms service=1ms status=200 bytes=195 protocol=https
2023-05-22T21:07:42.104854+00:00 heroku[router]: at=info method=GET path="/packs/js/application-449c89a2494baee3fa6f.js" host=blooming-mountain-08184.herokuapp.com request_id=8af3253e-5200-46b1-adce-a119e618fba8 fwd="165.225.32.83" dyno=web.1 connect=0ms service=1ms status=200 bytes=15758 protocol=https
2023-05-22T21:07:42.302410+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=blooming-mountain-08184.herokuapp.com request_id=f201c75f-4b8c-43fd-9d30-8fc41e19c91d fwd="165.225.32.83" dyno=web.1 connect=0ms service=1ms status=200 bytes=143 protocol=https
You can also get the full stream of logs by running the logs command with the --tail
flag option like this:
$ heroku logs --tail
Dyno sleeping and scaling
By default, new applications are deployed to an eco dyno. Eco apps will “sleep” to conserve resources. You can find more information about this behavior by reading about eco dyno behavior.
To avoid dyno sleeping, you can upgrade to a Basic or Professional dyno type as described in the Dyno Types article. For example, if you migrate your app to a professional dyno, you can easily scale it by running a command telling Heroku to execute a specific number of dynos, each running your web process type.
Run the Rails console
Heroku allows you to run commands in a one-off dyno - scripts and applications that only need to be executed when needed - using the heroku run
command. Use this to launch a Rails console process attached to your local terminal for experimenting in your app’s environment:
$ heroku run rails console
irb(main):001:0> puts 1+1
2
Another useful command for debugging is $ heroku run bash
which will spin up a new dyno and give you access to a bash session.
Run Rake commands
Rake can be run as an attached process exactly like the console:
$ heroku run rake db:migrate
Configure your webserver
By default, your app’s web process runs rails server
, which uses Puma in Rails 6. If you are upgrading an app you’ll need to add puma
to your application Gemfile
:
gem 'puma'
Then run
$ bundle install
Now you are ready to configure your app to use Puma. For this tutorial we will use the default config/puma.rb
of that ships with Rails 6, but we recommend reading more about configuring your application for maximum performance by reading the Puma documentation.
Finally you will need to tell Heroku how to run your Rails app by creating a Procfile
in the root of your application directory.
Create a Procfile
Change the command used to launch your web process by creating a file called Procfile and entering this:
In file Procfile
write:
web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}
Note: This file must be named
Procfile
exactly.
We recommend generating a Puma config file based on our Puma documentation for maximum performance.
To use the Procfile locally, you can use heroku local
.
In addition to running commands in your Procfile
heroku local
can also help you manage environment variables locally through a .env
file. Set the local RACK_ENV
to development in your environment and a PORT
to connect to. Before pushing to Heroku you’ll want to test with the RACK_ENV
set to production since this is the environment your Heroku app will run in.
$ echo "RACK_ENV=development" >>.env
$ echo "PORT=3000" >> .env
Note: Another alternative to using environment variables locally with a
.env
file is the dotenv gem.
You’ll also want to add .env
to your .gitignore
since this is for local environment setup.
$ echo ".env" >> .gitignore
$ git add .gitignore
$ git commit -m "add .env to .gitignore"
If you’re running Rails 6.0.0 you’ll need to create a tmp/pids
directory:
$ mkdir -p tmp/pids
$ touch tmp/pids/.gitkeep
Test your Procfile locally using Foreman. You can now start your web server by running:
$ heroku local
[OKAY] Loaded ENV .env File as KEY=VALUE Format
4:07:48 PM web.1 | Puma starting in single mode...
4:07:48 PM web.1 | * Puma version: 5.6.5 (ruby 3.0.6-p216) ("Birdie's Version")
4:07:48 PM web.1 | * Min threads: 5
4:07:48 PM web.1 | * Max threads: 5
4:07:48 PM web.1 | * Environment: development
4:07:48 PM web.1 | * PID: 31133
4:07:50 PM web.1 | * Listening on http://0.0.0.0:3000
4:07:50 PM web.1 | Use Ctrl-C to stop
Looks good, so press Ctrl+C
to exit and you can deploy your changes to Heroku:
$ git add .
$ git commit -m "use puma via procfile"
$ git push heroku main || git push heroku master
Check ps
. You’ll see that the web process uses your new command specifying Puma as the web server.
$ heroku ps
=== web (Basic): bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development} (1)
web.1: starting 2023/05/22 16:08:32 -0500 (~ 7s ago)
The logs also reflect that we are now using Puma.
$ heroku logs
Rails asset pipeline
There are several options for invoking the Rails asset pipeline when deploying to Heroku. For general information on the asset pipeline please see the Rails 3.1+ Asset Pipeline on Heroku Cedar article.
The config.assets.initialize_on_precompile
option has been removed is and not needed for Rails 6. Also, any failure in asset compilation will now cause the push to fail. For Rails 6 asset pipeline support see the Ruby Support page.
Troubleshooting
If you push up your app and it crashes (heroku ps
shows state crashed
), check your logs to find out what went wrong. Here are some common problems.
Runtime dependencies on development/test gems
If you’re missing a gem when you deploy, check your Bundler groups. Heroku builds your app without the development
or test
groups, and if your app depends on a gem from one of these groups to run, you should move it out of the group.
One common example is using the RSpec tasks in your Rakefile
. If you see this in your Heroku deploy:
$ 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
Then you’ve hit this problem. First, duplicate the problem locally:
$ bundle install --without development:test
…
$ bundle exec rake -T
rake aborted!
no such file to load -- rspec/core/rake_task
Note: The
--without
option on bundler is sticky. You can get rid of this option by runningbundle config --delete without
.
Now you can fix it by making these Rake tasks conditional on the gem load. For example:
begin
require "rspec/core/rake_task"
desc "Run all examples"
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w[--color]
t.pattern = 'spec/**/*_spec.rb'
end
rescue LoadError
end
Confirm it works locally, then push to Heroku.
Next Steps
Congratulations on deploying a Rails 6 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.
Deleting your app and Add-on
If you don’t need this application and database, you can now remove them from your account. You’ll only be charged for the resources you used.
This will remove your add-on you’ll lose any data saved in the database.
$ heroku addons:destroy heroku-postgresql
This will delete your application
$ heroku apps:destroy
You can confirm that your add-on and app are gone with the commands:
$ heroku addons --all
$ heroku apps -all
You’re now ready to deploy your app.