Skip Navigation
Show nav
Dev Center
  • Get Started
  • ドキュメント
  • Changelog
  • Search
  • Get Started
    • Node.js
    • Ruby on Rails
    • Ruby
    • Python
    • Java
    • PHP
    • Go
    • Scala
    • Clojure
    • .NET
  • ドキュメント
  • Changelog
  • More
    Additional Resources
    • Home
    • Elements
    • Products
    • Pricing
    • Careers
    • Help
    • Status
    • Events
    • Podcasts
    • Compliance Center
    Heroku Blog

    Heroku Blog

    Find out what's new with Heroku on our blog.

    Visit Blog
  • Log inorSign up
Hide categories

Categories

  • Heroku のアーキテクチャ
    • Dyno (アプリコンテナ)
      • Dyno Management
      • Dyno Concepts
      • Dyno Behavior
      • Dyno Reference
      • Dyno Troubleshooting
    • スタック (オペレーティングシステムイメージ)
    • ネットワーキングと DNS
    • プラットフォームポリシー
    • プラットフォームの原則
  • Developer Tools
    • コマンドライン
    • Heroku VS Code Extension
  • デプロイ
    • Git を使用したデプロイ
    • Docker によるデプロイ
    • デプロイ統合
  • 継続的デリバリーとインテグレーション
    • 継続的統合
  • 言語サポート
    • Node.js
      • Working with Node.js
      • Node.js Behavior in Heroku
      • Troubleshooting Node.js Apps
    • Ruby
      • Rails のサポート
      • Bundler の使用
      • Working with Ruby
      • Ruby Behavior in Heroku
      • Troubleshooting Ruby Apps
    • Python
      • Working with Python
      • Python でのバックグランドジョブ
      • Python Behavior in Heroku
      • Django の使用
    • Java
      • Java Behavior in Heroku
      • Working with Java
      • Maven の使用
      • Spring Boot の使用
      • Troubleshooting Java Apps
    • PHP
      • PHP Behavior in Heroku
      • Working with PHP
    • Go
      • Go の依存関係管理
    • Scala
    • Clojure
    • .NET
      • Working with .NET
  • データベースとデータ管理
    • Heroku Postgres
      • Postgres の基礎
      • Postgres スターターガイド
      • Postgres のパフォーマンス
      • Postgres のデータ転送と保持
      • Postgres の可用性
      • Postgres の特別なトピック
      • Migrating to Heroku Postgres
    • Heroku Data For Redis
    • Apache Kafka on Heroku
    • その他のデータストア
  • AI
    • Model Context Protocol
    • Vector Database
    • Heroku Inference
      • Inference Essentials
      • AI Models
      • Inference API
      • Quick Start Guides
    • Working with AI
  • モニタリングとメトリクス
    • ログ記録
  • アプリのパフォーマンス
  • アドオン
    • すべてのアドオン
  • 共同作業
  • セキュリティ
    • アプリのセキュリティ
    • ID と認証
      • シングルサインオン (SSO)
    • Private Space
      • インフラストラクチャネットワーキング
    • コンプライアンス
  • Heroku Enterprise
    • Enterprise Accounts
    • Enterprise Team
    • Heroku Connect (Salesforce 同期)
      • Heroku Connect の管理
      • Heroku Connect のリファレンス
      • Heroku Connect のトラブルシューティング
  • パターンとベストプラクティス
  • Heroku の拡張
    • Platform API
    • アプリの Webhook
    • Heroku Labs
    • アドオンのビルド
      • アドオン開発のタスク
      • アドオン API
      • アドオンのガイドラインと要件
    • CLI プラグインのビルド
    • 開発ビルドパック
    • Dev Center
  • アカウントと請求
  • トラブルシューティングとサポート
  • Salesforce とのインテグレーション
  • 言語サポート
  • Ruby
  • Rails のサポート
  • Heroku スターターガイド (Rails 6.x)

Heroku スターターガイド (Rails 6.x)

日本語 — Switch to English

この記事の英語版に更新があります。ご覧の翻訳には含まれていない変更点があるかもしれません。

最終更新日 2023年06月02日(金)

This article is a work in progress, or documents a feature that is not yet released to all users. This article is unlisted. Only those with the link can access it.

Table of Contents

  • ローカルの設定
  • 新しい Rails アプリの作成または既存の Rails アプリのアップグレード
  • pg gem の追加
  • ウェルカムページの作成
  • Ruby バージョンの指定
  • Procfile の作成
  • Git でのアプリの保存
  • babel のリグレッションの修正
  • Heroku アプリの作成
  • データベースのプロビジョニング
  • Heroku へのアプリのデプロイ
  • データベースの移行
  • アプリケーションのスケーリングとアクセス
  • アプリケーションログの表示
  • オプションのステップ
  • Rails アセットパイプライン
  • Heroku gem の削除
  • トラブルシューティング
  • 次のステップ
  • アプリとアドオンの削除

Rails の利用可能な最新バージョンは Rails 7​ です。新しいアプリケーションを開始する場合は、最新リリースバージョンを使用することをお勧めします。

Ruby on Rails は、Ruby​ で記述され、広く使われている Web フレームワークです。このガイドでは、Heroku での Rails 6 の使用について説明します。Heroku での Rails 旧バージョンの実行についての詳細は、Rails 5.x​ または Rails 4.x​ に関するチュートリアルを参照してください。

このチュートリアルでは、以下が用意されていることを前提としています。

  • Ruby/Rails および Git の基本的な知識
  • Ruby 2.5.0+、Rubygems、Bundler、Rails 6 のローカルにインストールされたバージョン+
  • Heroku CLI​ のローカルにインストールされたバージョン
  • 確認済みの Heroku アカウント
  • Eco dyno プラン​のサブスクリプション (推奨)

dyno とデータベースを使用してこのチュートリアルを完了した場合、使用量のカウントに入ります。Heroku dyno、Heroku Postgres、および Heroku Data for Redis の無料プランは 2022 年 11 月 28 日で提供を終了します。資格のある学生の皆様は、新しい Heroku for GitHub Students プログラム​を通じてプラットフォームクレジットを申請できます。

ローカルの設定

Heroku CLI​ のインストール後、ターミナルからログインします。

$ 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 me@example.com

このコマンドにより、Web ブラウザで Heroku ログインページが開きます。ブラウザですでに Heroku にログインしている場合は、ページの Log in​ (ログイン) ボタンをクリックします。

この認証は、heroku​ と git​ コマンドが正常に動作するために必要な操作です。

新しい Rails アプリの作成または既存の Rails アプリのアップグレード

rails -v​ を実行して、Rails 6.x を使用していることを確認します。必要に応じて、次のコマンドでインストールします。

$ gem install rails -v 6.1.7.3 --no-document
Successfully installed rails-6.1.7.3
1 gem installed

Rails アプリを作成します。

$ rails _6.1.7.3_ new myapp --database=postgresql

アプリケーションディレクトリに移動し、Gemfile.lock​ に x86_64-linux​ および ruby​ プラットフォームを追加します。

$ 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

ローカルデータベースを作成します。

$ bin/rails db:create
Database 'myapp_development' already exists
Database 'myapp_test' already exists

pg gem の追加

--database=postgresql​ が定義されていない新規または既存のアプリの場合は、Gemfile​ 内に sqlite3​ gem が存在しないことを確認します。pg​ gem を所定の位置に追加します。

gem 'sqlite3'

変更後は次のようになります。

gem 'pg'

開発中は PostgreSQL をローカルで使用することを強くお勧めします。開発環境とデプロイ環境の間の同等性​を維持することにより、環境の違いによる微妙なバグが取り込まれることを防ぎます。

Postgres をローカルにインストールします​。Sqlite3 の代わりに Postgres が推奨される理由についての詳細は、Sqlite3 が Heroku と互換性がない理由​を参照してください。

Gemfile​ が更新されたら、依存関係を再インストールします。

$ bundle install

インストールによって、Gemfile.lock​ も変更を含めて更新されます。

pg​ gem に加えて、config/database.yml​ で postgresql​ アダプタが定義されていることを確認します。config/database.yml​ ファイルの開発セクションは次のようになります。

$ 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'] %>

ここで注意点があります。adapter​ の値が postgres​ で、postgresql​ でない場合、アプリケーションは機能しません。

ウェルカムページの作成

Rails 6 の本番環境には、デフォルトで静的なインデックスページがありません。新しいアプリを使用するとき、本番環境にはルートページがないため、ルートページを作成する必要があります。ホームページ用の welcome​ というコントローラーを作成します。

$ rails generate controller welcome

ファイル app/views/welcome/index.html.erb​ で、以下のように記述します。

<h2>Hello World</h2>
<p>
  The time is now: <%= Time.now %>
</p>

このアクションへの Rails ルートを作成します。config/routes.rb​ を編集して、インデックスページを新しいメソッドに設定します。

ファイル config/routes.rb​ の 2 行目に以下を追加します。

  root 'welcome#index'

サーバーを実行して、このページが存在することを検証できます。

$ rails server

ブラウザで http://localhost:3000​ にアクセスします。ページが表示されない場合は、ログを使用​してデバッグします。 Rails によって、rails server​ が起動した同じターミナルにログが出力されます。

Ruby バージョンの指定

Rails 6 では、Ruby 2.5.0 以上が必要です。Heroku は、デフォルトで最新バージョンの Ruby がインストールされます。Gemfile​ で ruby​ DSL を使用して、特定のバージョンを指定します。たとえば、次のようになります。

ruby "3.0.6"

常にローカルでも同じバージョンの Ruby を使用します。ruby -v​ を使用して ruby のローカルバージョンを確認します。特定の Ruby バージョンの定義についての詳細は、「Ruby のバージョン​」の記事を参照してください。

Procfile の作成

Procfile​ は、アプリケーションのルートディレクトリにあるテキストファイルです。このファイルを使って、アプリの起動時に実行するコマンドを明示的に宣言します。

この Procfile では、単一のプロセスタイプの web​ と、その実行に必要なコマンドを宣言しています。 ここでは、web​ という名前が重要です。 これは、このプロセスタイプを Heroku の HTTP ルーティング​スタックにアタッチし、デプロイ後に Web トラフィックを受信することを宣言しています。

デフォルトでは、Rails アプリの Web プロセスは、Rails 7 で Puma を使用する rails server​ を実行します。Procfile を使用せずに Rails 7 アプリケーションをデプロイすると、このコマンドが実行されます。ただし、Procfile を介してサーバープロセスを起動する方法を明示的に宣言することをお勧めします。

ファイル Procfile​ で、以下のように記述します。

web: bundle exec puma -C config/puma.rb

Procfile​ ファイル名は大文字と小文字が区別されます。ファイル拡張子はありません。

config/puma.rb​ が存在しない場合は、最大のパフォーマンスを得るために、Heroku の Puma ドキュメント​を使用して作成してください。

Procfile には追加のプロセスタイプを含めることができます。 たとえば、キューからアイテムを取り出して処理するバックグラウンドワーカープロセス​を宣言できます。

Git でのアプリの保存

Heroku では、アプリケーションのデプロイを分散型ソース管理ツールである Git​ に依存しています。アプリケーションがまだ Git に存在しない場合は、まず git --help​ を使用して git​ がシステムに存在することを確認してください。

$ 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>]

コマンドで出力が生成されないか、または command not found​ の場合、Git をインストール​します。

Rails アプリのルートディレクトリに移動します。ls​ コマンドを使用して、その内容を確認します。

$ ls
Gemfile
Gemfile-e
Gemfile.lock
Procfile
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

Rails アプリ内で直接、ローカルの空の Git リポジトリを初期化し、そのアプリのコードをコミットします。

$ git init
$ git add .
$ git commit -m "init"

git status​ を使用して、すべてが正しくコミットされたことを確認します。

$ git status
On branch main
nothing to commit, working tree clean

アプリケーションが Git にコミットされたら、Heroku にデプロイする準備ができています。

babel のリグレッションの修正

Rails 6 は、依存関係として babel を使用する webpacker gem​ に依存しています。マイナーバージョンのバージョン 7.22.0 の babel では破壊的変更がリリースされ​、新しい Rails 6.1.x アプリが動作しなくなりました。Rails 6 と webpacker はバグ修正プログラムを受け取らなくなったため、babel の変更が戻されない限り、新しいアプリでこのバグを回避する必要があります。

次のコマンドを実行して、アプリケーションがこのバグの影響を受けるかどうかをテストして確認してください。

$ rake webpacker:clobber webpacker:compile  2>&1 | grep "Cannot find package '@babel/plugin-proposal-private-methods'"
Error: Cannot find package '@babel/plugin-proposal-private-methods' imported from ./myapp/babel-virtual-resolve-base.js

この問題を修正するには、babel.config.js​ を変更し、plugin-proposal-private-methods​ を plugin-transform-private-methods​ で置き換えます。

-        '@babel/plugin-plugin-private-methods',
+        '@babel/plugin-transform-private-methods',

正しく機能することを確かめます。

$ rake webpacker:clobber webpacker:compile

修正を Git にコミットします。

$ git add .
$ git commit -m "fix babel error"

Heroku アプリの作成

dyno とデータベースを使用してこのチュートリアルを完了した場合、使用量のカウントに入ります。コストを抑制するために、完了したらすぐにアプリを削除​し、データベースも削除​してください。

Heroku でアプリを作成するには、Rails アプリのルートディレクトリ内で Heroku CLI を使用します。

$ heroku create --stack heroku-20
Creating app... done, serene-fortress-54097, stack is heroku-20
https://serene-fortress-54097.herokuapp.com/ | https://git.heroku.com/serene-fortress-54097.git

アプリを作成すると、heroku​ という名前の Git リモートリポジトリも作成され、ローカルの Git リポジトリと関連付けられます。Git リモートは、他のサーバー上で稼働するリポジトリのバージョンです。アプリに関連付けられた、Heroku でホストされる特別なリモートにコードをプッシュすることにより、アプリをデプロイします。git config​ でリモートが 設定されていることを確認します。

$ git config --list --local | grep heroku
remote.heroku.url=https://git.heroku.com/serene-fortress-54097.git
remote.heroku.fetch=+refs/heads/*:refs/remotes/heroku/*

現在のディレクトリが正しくないか、または Git が初期化​されていない場合、Git は fatal: not in a git directory​ を返します。Git がリモートの一覧を返した場合は、デプロイする準備ができています。

業界での変更に従い、Heroku ではデフォルトの Git ブランチ名を main​ に変更しました。デプロイ中のプロジェクトでデフォルトのブランチ名として master​ を使用している場合は、git push heroku master​ を使用してください。

データベースのプロビジョニング

Elements Marketplace​ から入手可能なアドオンの 1 つである Heroku Postgres​ データベースをプロビジョニングします。アドオンは、ログ記録、モニタリング、データベースなど、アプリケーションですぐに使える追加サービスを提供するクラウドサービスです。

mini​ Postgres のサイズのコストは月額 5 ドルで、分単位で課金されます​。このチュートリアルの最後で、データベースを削除​して、コストを最小限に抑えるように求められます。

$ heroku addons:create heroku-postgresql:mini
Creating heroku-postgresql:mini on serene-fortress-54097... $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-acute-36546 as DATABASE_URL
Use heroku addons:docs heroku-postgresql to view documentation

Heroku アプリでこの Postgres データベースにアクセスできるようになりました。DATABASE_URL​ 環境変数は、規約によって Rails が接続する資格情報を格納します。

Heroku へのアプリのデプロイ

dyno を使用してこのチュートリアルを完了した場合、使用量のカウントに入ります。コストを抑制するために、完了したらすぐにアプリを削除​してください。

コードをデプロイします。このコマンドは、サンプルリポジトリの main​ ブランチを heroku​ リモートにプッシュし、次に Heroku にデプロイします。

$ git push heroku main
remote: Updated 101 paths from 59c4854
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 zeitwerk 2.6.8
remote:        Fetching builder 3.2.4
remote:        Fetching concurrent-ruby 1.2.2
remote:        Fetching minitest 5.18.0
remote:        Installing concurrent-ruby 1.2.2
remote:        Installing minitest 5.18.0
remote:        Installing zeitwerk 2.6.8
remote:        Fetching erubi 1.12.0
remote:        Fetching mini_portile2 2.8.2
remote:        Installing erubi 1.12.0
remote:        Fetching racc 1.6.2
remote:        Installing mini_portile2 2.8.2
remote:        Fetching crass 1.0.6
remote:        Fetching rack 2.2.7
remote:        Installing racc 1.6.2 with native extensions
remote:        Installing builder 3.2.4
remote:        Fetching nio4r 2.5.9
remote:        Installing rack 2.2.7
remote:        Installing crass 1.0.6
remote:        Fetching websocket-extensions 0.1.5
remote:        Installing nio4r 2.5.9 with native extensions
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 date 3.3.3 with native extensions
remote:        Installing mini_mime 1.1.2
remote:        Fetching timeout 0.3.2
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.14.0
remote:        Installing i18n 1.14.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.2
remote:        Installing nokogiri 1.15.2 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 rails-dom-testing 2.0.3
remote:        Installing loofah 2.21.3
remote:        Fetching rails-html-sanitizer 1.6.0
remote:        Installing rails-html-sanitizer 1.6.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 actionpack 6.1.7.3
remote:        Installing jbuilder 2.11.5
remote:        Fetching actioncable 6.1.7.3
remote:        Fetching activestorage 6.1.7.3
remote:        Fetching actionmailer 6.1.7.3
remote:        Installing activestorage 6.1.7.3
remote:        Installing actionmailer 6.1.7.3
remote:        Installing actioncable 6.1.7.3
remote:        Fetching sprockets-rails 3.4.2
remote:        Fetching railties 6.1.7.3
remote:        Fetching actionmailbox 6.1.7.3
remote:        Installing sprockets-rails 3.4.2
remote:        Fetching actiontext 6.1.7.3
remote:        Installing railties 6.1.7.3
remote:        Installing actiontext 6.1.7.3
remote:        Installing actionmailbox 6.1.7.3
remote:        Fetching webpacker 5.4.4
remote:        Fetching rails 6.1.7.3
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 (91.68s)
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.41s.
remote:        I, [2023-06-02T17:33:30.634554 #1761]  INFO -- : Writing /tmp/build_3c4f2a0f/public/assets/manifest-b4bf6e57a53c2bdb55b8998cc94cd00883793c1c37c5e5aea3ef6749b4f6d92b.js
remote:        I, [2023-06-02T17:33:30.635400 #1761]  INFO -- : Writing /tmp/build_3c4f2a0f/public/assets/manifest-b4bf6e57a53c2bdb55b8998cc94cd00883793c1c37c5e5aea3ef6749b4f6d92b.js.gz
remote:        I, [2023-06-02T17:33:30.635816 #1761]  INFO -- : Writing /tmp/build_3c4f2a0f/public/assets/application-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css
remote:        I, [2023-06-02T17:33:30.635966 #1761]  INFO -- : Writing /tmp/build_3c4f2a0f/public/assets/application-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css.gz
remote:        I, [2023-06-02T17:33:30.636085 #1761]  INFO -- : Writing /tmp/build_3c4f2a0f/public/assets/welcome-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css
remote:        I, [2023-06-02T17:33:30.636165 #1761]  INFO -- : Writing /tmp/build_3c4f2a0f/public/assets/welcome-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css.gz
remote:        Compiling...
remote:        Compiled all packs in /tmp/build_3c4f2a0f/public/packs
remote:        Hash: 13bc5769bbba2660d04a
remote:        Version: webpack 4.46.0
remote:        Time: 3198ms
remote:        Built at: 06/02/2023 5:33:35 PM
remote:                                                Asset       Size  Chunks                         Chunk Names
remote:               js/application-5f7b4eeb39bbcc778fef.js   68.2 KiB       0  [emitted] [immutable]  application
remote:            js/application-5f7b4eeb39bbcc778fef.js.br   15.2 KiB          [emitted]
remote:            js/application-5f7b4eeb39bbcc778fef.js.gz   17.5 KiB          [emitted]
remote:           js/application-5f7b4eeb39bbcc778fef.js.map    203 KiB       0  [emitted] [dev]        application
remote:        js/application-5f7b4eeb39bbcc778fef.js.map.br   43.3 KiB          [emitted]
remote:        js/application-5f7b4eeb39bbcc778fef.js.map.gz   49.9 KiB          [emitted]
remote:                                        manifest.json  364 bytes          [emitted]
remote:                                     manifest.json.br  129 bytes          [emitted]
remote:                                     manifest.json.gz  142 bytes          [emitted]
remote:        Entrypoint application = js/application-5f7b4eeb39bbcc778fef.js js/application-5f7b4eeb39bbcc778fef.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.96s)
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: 95.9M
remote: -----> Launching...
remote:  !     The following add-ons were automatically provisioned: . These add-ons may incur additional cost, which is prorated to the second. Run `heroku addons` for more info.
remote:        Released v6
remote:        https://serene-fortress-54097.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/serene-fortress-54097.git
 * [new branch]      main -> main

出力に警告やエラーメッセージが表示された場合は、出力をチェックして、調整します。

デプロイが成功したら、必要に応じて以下のタスクを実行します。

  • データベースの移行
  • dyno のスケーリング
  • 問題が発生した場合はアプリのログのチェック

データベースの移行

アプリケーションでデータベースを使用している場合は、Heroku CLI を使用して、One-off dyno​ を起動し、移行をトリガーします。One-off dyno で、heroku run​ コマンドを使ってコマンド (通常はアプリの一部を構成するスクリプトやアプリケーション) を実行できます。次のコマンドでデータベース移行をトリガーできます。

$ heroku run rake db:migrate

代わりに対話型シェルを使用するには、heroku run bash​ を実行できます。

アプリケーションのスケーリングとアクセス

Heroku は、定義されたプロセスとプロセスタイプ​を使用してアプリケーションコードを実行します。新しいアプリケーションでは、デフォルトでプロセスタイプがアクティブになりません。次のコマンドはアプリを最大 1 つの dyno にスケーリングし、web​ プロセスを実行します。

$ heroku ps:scale web=1

Heroku CLI の ps​ コマンドを使用して、アプリのすべての dyno の状態をターミナルに表示します。

$ heroku ps
=== web (Basic): bundle exec puma -C config/puma.rb (1)
web.1: up 2023/06/02 12:33:57 -0500 (~ 1s ago)

Eco​ にサブスクライブしている場合、アプリではデフォルトで Eco dyno が使用されます。それ以外の場合は、デフォルトで Basic dyno が使用されます。Eco dyno プランは、アカウントのすべての Eco dyno 間で共有され、多数の小さなアプリを Heroku にデプロイする場合にお勧めします。Eco dyno は 30 分間トラフィックを何も受信しないとスリープします。 このスリープ動作により、スリープ解除した最初のリクエストで数秒の遅延が発生します。Eco dyno は、月ごとに割り当てられるアカウント別の Eco dyno 時間​を消費します。割り当て時間が残っている限り、アプリは稼働し続けます。

dyno がスリープしないようにするには、「dyno タイプ​」の記事で紹介されている Basic 以上の dyno タイプにアップグレードします。少なくとも Standard dyno にアップグレードすることでも、プロセスタイプあたり複数の dyno にスケールアップできます。

アプリをブラウザで開くには、heroku open​ を実行します。

$ heroku open

ブラウザに、「Hello World」というテキストが表示されます。表示されない場合、またはエラーが発生した場合は、ウェルカムページのコンテンツをレビューして確認​してください。

Heroku では、開発中のすべてのアプリケーションにデフォルトの Web URL​ が提供されます。アプリケーションを本番環境用に準備できたら、カスタムドメイン​を追加します。

アプリケーションログの表示

アプリが正しく実行されないか、またはエラーを生成する場合、アプリログは貴重なツールです。

実行中のアプリに関する情報は、Heroku CLI のログコマンド​ heroku logs​ を使用して表示します。出力例を次に示します。

$ heroku logs
2023-06-02T17:31:33.299436+00:00 app[api]: Initial release by user developer@example.com2023-06-02T17:31:33.299436+00:00 app[api]: Release v1 created by user developer@example.com2023-06-02T17:31:33.496522+00:00 app[api]: Enable Logplex by user developer@example.com2023-06-02T17:31:33.496522+00:00 app[api]: Release v2 created by user developer@example.com2023-06-02T17:31:36.007624+00:00 app[api]: Attach DATABASE (@ref:postgresql-acute-36546) by user developer@example.com2023-06-02T17:31:36.007624+00:00 app[api]: Running release v3 commands by user developer@example.com2023-06-02T17:31:36.020015+00:00 app[api]: Release v4 created by user developer@example.com2023-06-02T17:31:36.020015+00:00 app[api]: @ref:postgresql-acute-36546 completed provisioning, setting DATABASE_URL. by user developer@example.com2023-06-02T17:31:38.000000+00:00 app[api]: Build started by user developer@example.com2023-06-02T17:33:47.861945+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-06-02T17:33:47.861945+00:00 app[api]: Release v5 created by user developer@example.com2023-06-02T17:33:48.497066+00:00 app[api]: Deploy e187b9b6 by user developer@example.com2023-06-02T17:33:48.497066+00:00 app[api]: Release v6 created by user developer@example.com2023-06-02T17:33:48.512927+00:00 app[api]: Scaled to console@0:Basic rake@0:Basic web@1:Basic by user developer@example.com2023-06-02T17:33:52.000000+00:00 app[api]: Build succeeded
2023-06-02T17:33:54.830828+00:00 heroku[web.1]: Starting process with command `bundle exec puma -C config/puma.rb`
2023-06-02T17:33:55.673903+00:00 app[web.1]: Puma starting in single mode...
2023-06-02T17:33:55.673927+00:00 app[web.1]: * Puma version: 5.6.5 (ruby 3.0.6-p216) ("Birdie's Version")
2023-06-02T17:33:55.673928+00:00 app[web.1]: *  Min threads: 5
2023-06-02T17:33:55.673928+00:00 app[web.1]: *  Max threads: 5
2023-06-02T17:33:55.673928+00:00 app[web.1]: *  Environment: production
2023-06-02T17:33:55.673928+00:00 app[web.1]: *          PID: 2
2023-06-02T17:33:57.303165+00:00 app[web.1]: * Listening on http://0.0.0.0:4941
2023-06-02T17:33:57.306582+00:00 app[web.1]: Use Ctrl-C to stop
2023-06-02T17:33:57.465545+00:00 heroku[web.1]: State changed from starting to up
2023-06-02T17:34:02.732124+00:00 app[web.1]: I, [2023-06-02T17:34:02.732049 #2]  INFO -- : [058559a6-3b16-43e6-9c5f-1aaaedaac7ea] Started GET "/" for 13.110.54.12 at 2023-06-02 17:34:02 +0000
2023-06-02T17:34:02.734756+00:00 app[web.1]: I, [2023-06-02T17:34:02.734712 #2]  INFO -- : [058559a6-3b16-43e6-9c5f-1aaaedaac7ea] Processing by WelcomeController#index as HTML
2023-06-02T17:34:02.738573+00:00 app[web.1]: I, [2023-06-02T17:34:02.738531 #2]  INFO -- : [058559a6-3b16-43e6-9c5f-1aaaedaac7ea]   Rendered welcome/index.html.erb within layouts/application (Duration: 0.3ms | Allocations: 215)
2023-06-02T17:34:02.739557+00:00 app[web.1]: I, [2023-06-02T17:34:02.739524 #2]  INFO -- : [058559a6-3b16-43e6-9c5f-1aaaedaac7ea]   Rendered layout layouts/application.html.erb (Duration: 1.3ms | Allocations: 1288)
2023-06-02T17:34:02.739736+00:00 app[web.1]: I, [2023-06-02T17:34:02.739713 #2]  INFO -- : [058559a6-3b16-43e6-9c5f-1aaaedaac7ea] Completed 200 OK in 5ms (Views: 2.5ms | Allocations: 2932)
2023-06-02T17:34:02.740263+00:00 heroku[router]: at=info method=GET path="/" host=serene-fortress-54097.herokuapp.com request_id=058559a6-3b16-43e6-9c5f-1aaaedaac7ea fwd="13.110.54.12" dyno=web.1 connect=0ms service=13ms status=200 bytes=1848 protocol=https
2023-06-02T17:34:02.879635+00:00 heroku[router]: at=info method=GET path="/assets/application-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css" host=serene-fortress-54097.herokuapp.com request_id=26625d02-97e0-4fcf-b2d8-a7b2c777b8b8 fwd="13.110.54.12" dyno=web.1 connect=0ms service=1ms status=200 bytes=195 protocol=https
2023-06-02T17:34:02.978200+00:00 heroku[router]: at=info method=GET path="/packs/js/application-5f7b4eeb39bbcc778fef.js" host=serene-fortress-54097.herokuapp.com request_id=bb827782-303b-424b-adc4-6975ca73b16f fwd="13.110.54.12" dyno=web.1 connect=0ms service=0ms status=200 bytes=15762 protocol=https
2023-06-02T17:34:03.276759+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=serene-fortress-54097.herokuapp.com request_id=1b75ee4d-6dca-4acc-b6b8-3118322f3434 fwd="13.110.54.12" dyno=web.1 connect=0ms service=0ms status=200 bytes=143 protocol=https

以下のように --tail​ フラグオプションを使用してログコマンドを実行することで、完全なログストリームを入手することもできます。

$ heroku logs --tail

Heroku では、デフォルトで 1500 行のアプリケーションログが記録されますが、完全なログストリームはサービスとして提供されています。複数のアドオンプロバイダ​が、ログの永続化、検索、メールや SMS 通知などの機能を提供するログサービスを用意しています。

オプションのステップ

Rails コンソールの使用

One-off dyno​ をトリガーして、スクリプトやアプリケーションを必要な場合にのみ実行するには、Heroku CLI の run​ コマンドを使用します。このコマンドを使用して、アプリの環境で試行するためにローカルターミナルにアタッチされた Rails コンソールプロセスを起動します。

$ heroku run rails console
irb(main):001:0> puts 1+1
2

Rake コマンドの実行

Rails コンソールと同様に、run​ コマンドを使用して、db:migrate​ などの rake​ コマンドを実行します。

$ heroku run rake db:migrate

Procfile をローカルで使用する

Procfile​ をローカルで使用するには、heroku local​ CLI コマンドを使用します。

Procfile​ でのコマンドの実行に加えて、heroku local​ コマンドでは、環境変数を .env​ ファイル経由でローカルで管理することもできます。RACK_ENV​ を、ローカル環境の場合は development​ に、Puma の場合は PORT​ に設定します。

$ echo "RACK_ENV=development" >>.env
$ echo "PORT=3000" >> .env

.env​ ファイルを使用して環境変数をローカルで使用する代わりの方法として dotenv​ gem があります。

これらの変数はローカル環境の設定専用であるため、.env​ を .gitignore​ に追加します。

$ echo ".env" >> .gitignore
$ git add .gitignore
$ git commit -m "add .env to .gitignore"

Foreman​ を使用して、Procfile をローカルでテストします。local​ を使用して Web サーバーを起動します。

$ heroku local
[OKAY] Loaded ENV .env File as KEY=VALUE Format
12:34:07 PM web.1 |  Puma starting in single mode...
12:34:07 PM web.1 |  * Puma version: 5.6.5 (ruby 3.0.6-p216) ("Birdie's Version")
12:34:07 PM web.1 |  *  Min threads: 5
12:34:07 PM web.1 |  *  Max threads: 5
12:34:07 PM web.1 |  *  Environment: development
12:34:07 PM web.1 |  *          PID: 59960
12:34:09 PM web.1 |  * Listening on http://0.0.0.0:3000
12:34:09 PM web.1 |  Use Ctrl-C to stop

Ctrl+C​ または Cmd+C​ を押して終了します。

Rails アセットパイプライン

Heroku にデプロイするときに、Rails アセットパイプライン​を起動するためのオプションはいくつかあります。アセットパイプラインに関する一般的な情報については、「Heroku での Rails アセットパイプライン​」の記事を参照してください。

Rails 6 では、必要がなくなった config.assets.initialize_on_precompile​ オプションが削除されました。さらに、アセットコンパイルで何らかのエラーが発生するとプッシュが失敗するようになりました。Rails 6 アセットパイプラインのサポートについては、Ruby サポート​のページを参照してください。

Heroku gem の削除

以前のバージョンの Rails では、Heroku で静的アセットの提供やログ記録を有効にするには、プロジェクト rails_12factor​ に gem を追加する必要がありました。新しいアプリケーションをデプロイしている場合、この gem は必要ありません。既存のアプリケーションをアップグレードしているときは、config/environments/production.rb​ ファイルに適切な設定がある場合、この gem を削除できます。

# 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

トラブルシューティング

たとえば heroku ps​ に crashed​ 状態が表示されるなど、Heroku にデプロイされたアプリがクラッシュした場合は、アプリのログを確認します。次のセクションでは、アプリのクラッシュの一般的な原因について説明します。

開発またはテスト gem でのランタイムの依存関係

デプロイ中に gem が存在しない場合は、Bundler グループをチェックしてください。Heroku では、アプリが development​ または test​ グループなしでビルドされ、アプリの実行をこれらのいずれかのグループの gem に依存している場合は、それをグループから除外します。

一般的な例として、Rakefile​ での RSpec タスクの使用があります。このエラーは多くの場合、次のようになります。

$ 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

まず、開発またはテスト gem グループなしで bundle install​ を実行して、問題をローカルに複製します。

$ bundle install --without development:test
…
$ bundle exec rake -T
rake aborted!
no such file to load -- rspec/core/rake_task

bundler​ の --without​ オプションは永続的です。このオプションを削除するには、bundle config --delete without​ を実行します。

これらの Rake タスクを gem のロード中に条件付きにすることによってエラーを解決します。次に例を示します。

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

これがローカルで動作することを確認してから、Heroku にプッシュします。

次のステップ

これで完了です。最初の Rails 6 アプリケーションが Heroku にデプロイされました。次に、次の記事を確認してください。

  • 「Ruby サポートカテゴリ​」では、Heroku での Ruby および Rails の使用に関する詳細を確認できます。
  • 「デプロイカテゴリ​」では、デプロイを効率的かつ簡単に行うことができるようにする、多くの強力な統合や機能を紹介しています。

アプリとアドオンの削除

アカウントからアプリとデータベースを削除します。使用したリソースに対してのみ課金されます。

この操作により、アドオンとデータベースに保存されたすべてのデータが削除されます。

$ heroku addons:destroy heroku-postgresql

この操作により、アプリケーションが完全に削除されます。

$ heroku apps:destroy

以下のコマンドによって、アドオンとアプリが消去されたことを確認できます。

$ heroku addons --all
$ heroku apps -all

これで、​アプリをデプロイする​準備ができました。

関連カテゴリー

  • Rails のサポート
Unicorn を使用した Rails アプリケーションのデプロイ Heroku スターターガイド (Rails 7.x)

Information & Support

  • Getting Started
  • Documentation
  • Changelog
  • Compliance Center
  • Training & Education
  • Blog
  • Support Channels
  • Status

Language Reference

  • Node.js
  • Ruby
  • Java
  • PHP
  • Python
  • Go
  • Scala
  • Clojure
  • .NET

Other Resources

  • Careers
  • Elements
  • Products
  • Pricing
  • RSS
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku Blog
    • Heroku News Blog
    • Heroku Engineering Blog
  • Twitter
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku
    • Heroku Status
  • Github
  • LinkedIn
  • © 2025 Salesforce, Inc. All rights reserved. Various trademarks held by their respective owners. Salesforce Tower, 415 Mission Street, 3rd Floor, San Francisco, CA 94105, United States
  • heroku.com
  • Legal
  • Terms of Service
  • Privacy Information
  • Responsible Disclosure
  • Trust
  • Contact
  • Cookie Preferences
  • Your Privacy Choices