LoginSignup
4
0

More than 1 year has passed since last update.

Rails gem postgresqlをインストールできない時の対処法。

Posted at

はじめに

Railsを使用して開発した、アプリケーションをherokuにデプロイする際、本番環境用のデータベースとして、PostgreSQLを設定する必要があったのですが、その時に少しハマったので、解決策をここに記録しておきます。

PostgreSQLの導入

heroku上のデプロイする際、RailsのデフォルトデータベースのSQliteは使用することができないので、本番環境用のデータベースとして、PostgreSQLを導入します。

まず、開発環境では、sqliteを使用し、本番環境では、PostgreSQLを使用する設定をGemfileに記述します。

Gemfile
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~> 1.4', group: :development
gem 'pg', '~> 1.3', '>= 1.3.3', group: :production

次に、gemfileに記述した内容ターミナルでをインストールします。

terminal
$ bundle install

バンドル時に次のようなエラーが発生しました。

terminal
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory: /Users/**/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/pg-1.3.3/ext
/Users/**/.rbenv/versions/3.0.1/bin/ruby -I /Users/**/.rbenv/versions/3.0.1/lib/ruby/3.0.0 -r ./siteconf20220309-72317-3rqcu0.rb extconf.rb
Calling libpq with GVL unlocked
checking for pg_config... no
checking for libpq per pkg-config... no
Using libpq from
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*****************************************************************************

Unable to find PostgreSQL client library.

Please install libpq or postgresql client package like so:
  brew install libpq

or try again with:
  gem install pg -- --with-pg-config=/path/to/pg_config

or set library paths manually with:
  gem install pg -- --with-pg-include=/path/to/libpq-fe.h/ --with-pg-lib=/path/to/libpq.so/

*** 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.
.
.
.
.
.
To see why this extension failed to compile, please check the mkmf.log which can be found here:

/Users/**/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/extensions/x86_64-darwin-18/3.0.0/pg-1.3.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/**/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/pg-1.3.3 for inspection.
Results logged to /Users/**/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/extensions/x86_64-darwin-18/3.0.0/pg-1.3.3/gem_make.out

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

エラー文の確認

気になるエラー文をピックアップします。

terminal
Can't find the 'libpq-fe.h' header
terminal
To see why this extension failed to compile, please check the mkmf.log which can be found here:/Users/**/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/extensions/x86_64-darwin-18/3.0.0/pg-1.3.3/mkmf.log
terminal
Make sure that `gem install pg -v '1.3.3' --source 'https://rubygems.org/'` succeeds before bundling.

検証

ここからは、実際に検証した順番で行きます。

terminal
Make sure that `gem install pg -v '1.3.3' --source 'https://rubygems.org/'` succeeds before bundling.

この部分については、bundleする前にこれが成功するか確認しろとのことだったので、早速実行しました。

terminal
$ gem install pg -v '1.3.3' --source 'https://rubygems.org/

結果は、失敗。

次に、この部分ですが、コンパイルが失敗した理由が知りたいなら、これを見ろと書かれていたので、これだと飛びつきました。

terminal
To see why this extension failed to compile, please check the mkmf.log which can be found here:/Users/**/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/extensions/x86_64-darwin-18/3.0.0/pg-1.3.3/mkmf.log

まずは、対象のファイルのディレクトリに移動して、対象ファイルをcatします。

terminal
$ cd /Users/**/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/extensions/x86_64-darwin-18/3.0.0/pg-1.3.3/

$ ls
gem_make.out    mkmf.log

$ cat  mkmf.log
terminal
find_executable: checking for pg_config... -------------------- no

--------------------

checking for libpq per pkg-config... -------------------- no

"pkg-config --exists libpq"
| pkg-config --libs libpq
=> "-lpq\n"
"clang -fdeclspec -o conftest -I/Users/**/.rbenv/versions/3.0.1/include/ruby-3.0.0/x86_64-darwin18 -I/Users/**/.rbenv/versions/3.0.1/include/ruby-3.0.0/ruby/backward -I/Users/**/.rbenv/versions/3.0.1/include/ruby-3.0.0 -I. -I/Users/**/.rbenv/versions/3.0.1/include  -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT    -O3 -Wno-error=shorten-64-to-32  -fno-common -pipe conftest.c  -L. -L/Users/**/.rbenv/versions/3.0.1/lib -L. -L/Users/**/.rbenv/versions/3.0.1/lib  -fstack-protector-strong -L/usr/local/lib     -lruby.3.0   "
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: int main(int argc, char **argv)
4: {
5:   return !!argv[argc];
6: }
/* end */

| pkg-config --cflags-only-I libpq
=> "-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.Internal.sdk/usr/local/include\n"
| pkg-config --cflags-only-other libpq
=> "\n"
| pkg-config --libs-only-l libpq
=> "-lpq\n"
package configuration for libpq
incflags: -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.Internal.sdk/usr/local/include
cflags:
ldflags:
libs: -lpq

--------------------
find_header: checking for libpq-fe.h... -------------------- no

"clang -E -I/Users/**/.rbenv/versions/3.0.1/include/ruby-3.0.0/x86_64-darwin18 -I/Users/**/.rbenv/versions/3.0.1/include/ruby-3.0.0/ruby/backward -I/Users/**/.rbenv/versions/3.0.1/include/ruby-3.0.0 -I. -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.Internal.sdk/usr/local/include -I/Users/**/.rbenv/versions/3.0.1/include  -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT    -O3 -Wno-error=shorten-64-to-32  -fno-common -pipe   conftest.c -o conftest.i"
conftest.c:3:10: fatal error: 'libpq-fe.h' file not found
#include <libpq-fe.h>
         ^~~~~~~~~~~~
1 error generated.
checked program was:
/* begin */
1: #include "ruby.h"
2:
3: #include <libpq-fe.h>
/* end */

--------------------

bundle時にチェックした項目とその結果についてのログが残っているようです。最後の部分でめぼしいエラー文を発見しました。

terminal
fatal error: 'libpq-fe.h' file not found
#include <libpq-fe.h>
         ^~~~~~~~~~~~

fatal error(致命的なエラー)。。エラー原因はこれみたいですね。
どうやら、'libpq-fe.h'というファイルが必要みたいですが、現状存在していないようです。
また、解決策として、#include を提示してくれてます。
ここで最後のエラー文と結びつきます。冒頭で、必要なファイルが足りてないことを教えてくれていたのですね。

terminal
Can't find the 'libpq-fe.h' header

'libpq-fe.h'とは

libpqのラージオブジェクトインタフェースを使用するクライアントアプリケーションは、libpq/libpq-fs.hヘッダファイルをインクルードし、libpqライブラリとリンクしなければなりません。

引用:32.3. クライアントインタフェース

アプリケーションサイドから、データベースのオブジェクトを操作するために必要なライブラリ(ファイル群)のようです。
ラージ・オブジェクトについては、以下を参照してください。
参考:ラージ・オブジェクトの概要

brew install postgresql

'libpq-fe.h'ファイルが必要ということがわかったので、用意します。
方法として、Homebrewを使用してposgresqlのパケージをインストールします。
パッケージマネージャーを使用することで、必要なファイル群をそれらの依存関係に基づいて適切にインストールすることができます。

terminal
$ brew install postgresql

これで'libpq-fe.h'を含めた、必要なファイル群がインストールされたので、再度bundle installを実行します。

terminal
$ bundle install

これで、無事gemをインストールすることができました!

参考:Can't find the 'libpq-fe.h header when trying to install pg gem

参考

32.3. クライアントインタフェース
ラージ・オブジェクトの概要
Homebrew Formulae/postgresql
Can't find the 'libpq-fe.h header when trying to install pg gem

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0