HerokuへのPush時にFailed to install gems via Bundler.エラーが出た時の対処法


開発環境

MacOS Big Sur version11.2.3
Ruby 3.0.0
Rails 6.0.4

エラーが出た経緯

アプリ作成中、よし!あとはHerokuへのデプロイだけだ!!!と、
$git push heroku masterと打ったところ、Failed to install gems via Bundlerのエラーに遭遇。

ターミナル
$git push heroku master
# ...(省略)...
remote: -----> Ruby app detected
remote: -----> Installing bundler 2.2.16
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-3.0.0
remote: -----> Installing dependencies using bundler 2.2.16
remote:        Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4
remote:        Your bundle only supports platforms ["x86_64-darwin-20"] but your local platform
remote:        is x86_64-linux. Add the current platform to the lockfile with `bundle lock
remote:        --add-platform x86_64-linux` and try again.
remote:        Bundler Output: Your bundle only supports platforms ["x86_64-darwin-20"] but your local platform
remote:        is x86_64-linux. Add the current platform to the lockfile with `bundle lock
remote:        --add-platform x86_64-linux` and try again.
remote: 
remote:  !
remote:  !     Failed to install gems via Bundler.
remote:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...

解決策

Failed to install gems via Bundlerを大ざっぱに訳してみると、bundlerを経由してgemのインストールに失敗した、とのこと。

エラー文をよく読むと、下記のように書いてある。

ターミナル
Bundler Output: Your bundle only supports platforms ["x86_64-darwin-20"] but your local platform
remote:        is x86_64-linux. Add the current platform to the lockfile with `bundle lock
remote:        --add-platform x86_64-linux` and try again.

現在ローカルで使用しているのはMacOS(Darwin)だけど、HerokuのプラットフォームはLinuxだから異なるよ〜ってことかな。

エラー文に書いてある下記コマンドを実行。

ターミナル
$bundle lock --add-platform x86_64-linux

Fetching gem metadata from https://rubygems.org/............
Resolving dependencies...
Writing lockfile to /Users/macbook/Desktop/BrainTraining/Gemfile.lock
#できたぽい。

#変更したので再度add,commit
$git add -A
$git commit -m "fix Gemfile.lock"

$git push heroku master
# ...(省略)...
remote: Verifying deploy... done.
To https://git.heroku.com/******.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'heroku'.
# Herokuへのデプロイができた!!

答えはエラー文の中に…!!!
いい勉強になりました