⛳️

gem install pg でエラーが出た時の対策

2023/03/31に公開

Monterey(macos) で gem insatll pg (bundle insatll) した時に次のようなエラーが発生しました。

Building native extensions. This could take a while...
ERROR:  Error installing pg:
        ERROR: Failed to build gem native extension.

    current directory: /Users/xxxxxxxx/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/pg-0.21.0/ext
/Users/xxxxxxxx/.rbenv/versions/2.7.7/bin/ruby -I /Users/xxxxxxxx/.rbenv/versions/2.7.7/lib/ruby/2.7.0 -r ./siteconf20230331-11435-1st91ym.rb extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** 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=/Users/xxxxxxxx/.rbenv/versions/2.7.7/bin/$(RUBY_BASE_NAME)
        --with-pg
        --without-pg
        --enable-windows-cross
        --disable-windows-cross
        --with-pg-config
        --without-pg-config
        --with-pg_config
        --without-pg_config
        --with-pg-dir
        --without-pg-dir
        --with-pg-include
        --without-pg-include=${pg-dir}/include
        --with-pg-lib
        --without-pg-lib=${pg-dir}/lib

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

  /Users/xxxxxxxx/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/extensions/x86_64-darwin-21/2.7.0/pg-0.21.0/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/xxxxxxxx/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/pg-0.21.0 for inspection.
Results logged to /Users/xxxxxxxx/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/extensions/x86_64-darwin-21/2.7.0/pg-0.21.0/gem_make.out

必要なライブラリが含まれていないからのようです。
libpq というライブラリをインストールして、設定を行う必要があります。

brew install libpq
==> Downloading https://formulae.brew.sh/api/formula.jws.json
######################################################################## 100.0%
==> Downloading https://formulae.brew.sh/api/cask.jws.json
######################################################################## 100.0%
==> Fetching dependencies for libpq: krb5
==> Fetching krb5
==> Downloading https://ghcr.io/v2/homebrew/core/krb5/manifests/1.20.1
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/krb5/blobs/sha256:c529cf22c6e2191065a6c60c35875f83de8247cd4fbad5c84d0c9ed40d806a3f
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:c529cf22c6e2191065a6c60c35875f83de8247cd4fbad5c84d0c9ed40d806a3f?se=2023-
######################################################################## 100.0%
==> Fetching libpq
==> Downloading https://ghcr.io/v2/homebrew/core/libpq/manifests/15.2
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/libpq/blobs/sha256:ca68207e33c0ff6a394a85d2ed7fa0c07aa4fe6f80e21acd321e7ffbe2f214bb
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:ca68207e33c0ff6a394a85d2ed7fa0c07aa4fe6f80e21acd321e7ffbe2f214bb?se=2023-
######################################################################## 100.0%
==> Installing dependencies for libpq: krb5
==> Installing libpq dependency: krb5
==> Pouring krb5--1.20.1.monterey.bottle.tar.gz
🍺  /usr/local/Cellar/krb5/1.20.1: 162 files, 5.1MB
==> Installing libpq
==> Pouring libpq--15.2.monterey.bottle.tar.gz
==> Caveats
libpq is keg-only, which means it was not symlinked into /usr/local,
because conflicts with postgres formula.

If you need to have libpq first in your PATH, run:
  fish_add_path /usr/local/opt/libpq/bin

For compilers to find libpq you may need to set:
  set -gx LDFLAGS "-L/usr/local/opt/libpq/lib"
  set -gx CPPFLAGS "-I/usr/local/opt/libpq/include"

For pkg-config to find libpq you may need to set:
  set -gx PKG_CONFIG_PATH "/usr/local/opt/libpq/lib/pkgconfig"
==> Summary
🍺  /usr/local/Cellar/libpq/15.2: 2,368 files, 28.1MB
==> Running `brew cleanup libpq`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> libpq
libpq is keg-only, which means it was not symlinked into /usr/local,
because conflicts with postgres formula.

If you need to have libpq first in your PATH, run:
  fish_add_path /usr/local/opt/libpq/bin

For compilers to find libpq you may need to set:
  set -gx LDFLAGS "-L/usr/local/opt/libpq/lib"
  set -gx CPPFLAGS "-I/usr/local/opt/libpq/include"

For pkg-config to find libpq you may need to set:
  set -gx PKG_CONFIG_PATH "/usr/local/opt/libpq/lib/pkgconfig"

指示のある通り、環境変数の設定を追加します。私の使っているshellはfishですので~/.config/fish/config.fish へ次の項目を追記します。

# If you need to have libpq first in your PATH, run:
  fish_add_path /usr/local/opt/libpq/bin

# For compilers to find libpq you may need to set:
  set -gx LDFLAGS "-L/usr/local/opt/libpq/lib"
  set -gx CPPFLAGS "-I/usr/local/opt/libpq/include"

# For pkg-config to find libpq you may need to set:
  set -gx PKG_CONFIG_PATH "/usr/local/opt/libpq/lib/pkgconfig"

shellを再起動するか、. ~/.config/fish/config.fish を使ってconfigを読み込み直ししてから、再度 gem install pg します。

gem install pg -v '0.21.0' --source 'https://rubygems.org/'
Building native extensions. This could take a while...
Successfully installed pg-0.21.0
Parsing documentation for pg-0.21.0
Installing ri documentation for pg-0.21.0
Done installing documentation for pg after 0 seconds
1 gem installed

成功🎉

参考情報

macOSでgem install pgしてNo pg_configとなる対処法 - Qiita

Discussion