【Rails5】Herokuへのデプロイ時にSQLite3でエラーが出た時

解決する候補が2つ程あります。

1. gemfile で group :development を使っているかどうか

HerokuではSQLiteが使用できないので、以下のようにHeroku(本番環境)では gem sqliteをインストールしてしまわないように、以下のように書かないといけません。

group :development do
gem 'sqlite3', '1.3.13'
end

2. ローカルでは、bundle install –without production を指定しているが、.gitignore に .bundle/ を記載していない

Herokuでのエラー文の中に、下記のようなエラー分がありませんか?

###### WARNING:
You have the `.bundle/config` file checked into your repository
It contains local state like the location of the installed bundle
as well as configured git local gems, and other settings that should
not be shared between multiple checkouts of a single repo. Please
remove the `.bundle/` folder from your repo and add it to your `.gitignore` file.

この、’.bundle/config’ファイルには、bundle コマンド実行時に参照されるときのデフォルト設定が保存されています。
もし、ローカル環境で一度でも ‘ bundle install –without production’ とコマンドを実行している場合で、gitignoreにこのファイルを記載していない場合、デプロイ時のHeroku側でもこの設定を採用してしまうため、Heroku側でも本番用のgemがインストールされないことになります。

解決策としては、一旦、ローカルの環境で .bundle を削除します。

rm -r .bundle

その次に、.gitignore ファイルに、 .bundle/ を追加しておきます。

一旦この時点でgit add -A からの git commit してあげます。(.bundle/config が無い状態にするため)

その後、Herokuへデプロイしてあげます。

最後に再び、bundle install –without productionを実行してまたローカルに.bundleファイルを復活させておきましょう。
次回からは gitignore の設定によりこのディレクトリ 以下の変更は反映されませんので大丈夫です。

後書き

とりあえず、エラー部分だけでなく、普段はあまり気にしない警告文まで見てみましょう。

Herokuでのデプロイ時のエラー分は全文見れるはずですので、他にも様々な警告文があるかと思います。

コピペして翻訳するなりして読み解きましょう。

付録 ~今回のエラー文~

-----> Building on the Heroku-20 stack
-----> Determining which buildpack to use for this app
!     Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used.
Detected buildpacks: Ruby,Node.js
See https://devcenter.heroku.com/articles/buildpacks#buildpack-detect-order
-----> Ruby app detected
-----> Installing bundler 2.2.21
-----> Removing BUNDLED WITH version in the Gemfile.lock
-----> Compiling Ruby/Rails
-----> Using Ruby version: ruby-2.6.6
###### WARNING:
You have the `.bundle/config` file checked into your repository
It contains local state like the location of the installed bundle
as well as configured git local gems, and other settings that should
not be shared between multiple checkouts of a single repo. Please
remove the `.bundle/` folder from your repo and add it to your `.gitignore` file.

https://devcenter.heroku.com/articles/bundler-configuration
-----> Installing dependencies using bundler 2.2.21
Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4
Fetching gem metadata from https://rubygems.org/
Fetching gem metadata from https://rubygems.org/............
Fetching rake 13.0.6
...(省略)...
Fetching web-console 3.7.0
Installing web-console 3.7.0
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/tmp/build_1d730781/vendor/bundle/ruby/2.6.0/gems/sqlite3-1.3.13/ext/sqlite3
/tmp/build_1d730781/vendor/ruby-2.6.6/bin/ruby -I
/tmp/build_1d730781/vendor/ruby-2.6.6/lib/ruby/2.6.0 -r
./siteconf20210920-246-yrpsz2.rb extconf.rb
checking for sqlite3.h... no
sqlite3.h is missing. Try 'brew install sqlite3',
'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
and check your shared library search path (the
location where your sqlite3 shared library is located).
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/tmp/build_1d730781/vendor/ruby-2.6.6/bin/$(RUBY_BASE_NAME)
--with-sqlite3-config
--without-sqlite3-config
--with-pkg-config
--without-pkg-config
--with-sqlite3-dir
--without-sqlite3-dir
--with-sqlite3-include
--without-sqlite3-include=${sqlite3-dir}/include
--with-sqlite3-lib
--without-sqlite3-lib=${sqlite3-dir}/lib

To see why this extension failed to compile, please check the mkmf.log which can
be found here:

/tmp/build_1d730781/vendor/bundle/ruby/2.6.0/extensions/x86_64-linux/2.6.0/sqlite3-1.3.13/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/tmp/build_1d730781/vendor/bundle/ruby/2.6.0/gems/sqlite3-1.3.13 for inspection.
Results logged to
/tmp/build_1d730781/vendor/bundle/ruby/2.6.0/extensions/x86_64-linux/2.6.0/sqlite3-1.3.13/gem_make.out

An error occurred while installing sqlite3 (1.3.13), and Bundler cannot
continue.
Make sure that `gem install sqlite3 -v '1.3.13' --source
'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
sqlite3
Bundler Output: Fetching gem metadata from https://rubygems.org/
Fetching gem metadata from https://rubygems.org/............
Fetching rake 13.0.6
Installing rake 13.0.6
...(省略)...
Fetching concurrent-ruby 1.1.9

Installing web-console 3.7.0
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/tmp/build_1d730781/vendor/bundle/ruby/2.6.0/gems/sqlite3-1.3.13/ext/sqlite3
/tmp/build_1d730781/vendor/ruby-2.6.6/bin/ruby -I
/tmp/build_1d730781/vendor/ruby-2.6.6/lib/ruby/2.6.0 -r
./siteconf20210920-246-yrpsz2.rb extconf.rb
checking for sqlite3.h... no
sqlite3.h is missing. Try 'brew install sqlite3',
'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
and check your shared library search path (the
location where your sqlite3 shared library is located).
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/tmp/build_1d730781/vendor/ruby-2.6.6/bin/$(RUBY_BASE_NAME)
--with-sqlite3-config
--without-sqlite3-config
--with-pkg-config
--without-pkg-config
--with-sqlite3-dir
--without-sqlite3-dir
--with-sqlite3-include
--without-sqlite3-include=${sqlite3-dir}/include
--with-sqlite3-lib
--without-sqlite3-lib=${sqlite3-dir}/lib

To see why this extension failed to compile, please check the mkmf.log which can
be found here:

/tmp/build_1d730781/vendor/bundle/ruby/2.6.0/extensions/x86_64-linux/2.6.0/sqlite3-1.3.13/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/tmp/build_1d730781/vendor/bundle/ruby/2.6.0/gems/sqlite3-1.3.13 for inspection.
Results logged to
/tmp/build_1d730781/vendor/bundle/ruby/2.6.0/extensions/x86_64-linux/2.6.0/sqlite3-1.3.13/gem_make.out

An error occurred while installing sqlite3 (1.3.13), and Bundler cannot
continue.
Make sure that `gem install sqlite3 -v '1.3.13' --source
'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
sqlite3
!
!     Failed to install gems via Bundler.
!     Detected sqlite3 gem which is not supported on Heroku:
!     https://devcenter.heroku.com/articles/sqlite3
!
!     Push rejected, failed to compile Ruby app.
!     Push failed