[Ruby on Rails] An error occurred while installing pg (1.4.5), and Bundler cannot continue. 解決方法 [macOS 編]

macOS にて gem install pgAn error occurred while installing pg (1.4.5), and Bundler cannot continue. というエラーメッセージが発生したときの解決方法をご紹介します。

エラーメッセージ An error occurred while installing pg (1.4.5), and Bundler cannot continue.

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

    current directory: /Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/pg-1.4.5/ext
/Users/your_username/.asdf/installs/ruby/3.2.0/bin/ruby -I /Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/3.2.0
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.

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/your_username/.asdf/installs/ruby/3.2.0/bin/$(RUBY_BASE_NAME)
	--with-pg
	--without-pg
	--enable-gvl-unlock
	--disable-gvl-unlock
	--enable-windows-cross
	--disable-windows-cross
	--with-pg-config
	--without-pg-config
	--with-pg_config
	--without-pg_config
	--with-libpq-dir
	--without-libpq-dir
	--with-libpq-include
	--without-libpq-include=${libpq-dir}/include
	--with-libpq-lib
	--without-libpq-lib=${libpq-dir}/lib
	--with-libpq-config
	--without-libpq-config
	--with-pkg-config
	--without-pkg-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/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/extensions/arm64-darwin-21/3.2.0/pg-1.4.5/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/pg-1.4.5 for
inspection.
Results logged to
/Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/extensions/arm64-darwin-21/3.2.0/pg-1.4.5/gem_make.out

  /Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/3.2.0/rubygems/ext/builder.rb:102:in `run'
  /Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/3.2.0/rubygems/ext/ext_conf_builder.rb:28:in `build'
  /Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/3.2.0/rubygems/ext/builder.rb:171:in `build_extension'
  /Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/3.2.0/rubygems/ext/builder.rb:205:in `block in build_extensions'
  /Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/3.2.0/rubygems/ext/builder.rb:202:in `each'
  /Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/3.2.0/rubygems/ext/builder.rb:202:in `build_extensions'
  /Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/3.2.0/rubygems/installer.rb:843:in `build_extensions'
/Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.3.26/lib/bundler/rubygems_gem_installer.rb:72:in
`build_extensions'
/Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.3.26/lib/bundler/rubygems_gem_installer.rb:28:in
`install'
/Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.3.26/lib/bundler/source/rubygems.rb:207:in
`install'
/Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.3.26/lib/bundler/installer/gem_installer.rb:54:in
`install'
/Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.3.26/lib/bundler/installer/gem_installer.rb:16:in
`install_from_spec'
/Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.3.26/lib/bundler/installer/parallel_installer.rb:186:in
`do_install'
/Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.3.26/lib/bundler/installer/parallel_installer.rb:177:in
`block in worker_pool'
/Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.3.26/lib/bundler/worker.rb:62:in
`apply_func'
/Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.3.26/lib/bundler/worker.rb:57:in `block
in process_queue'
  /Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.3.26/lib/bundler/worker.rb:54:in `loop'
/Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.3.26/lib/bundler/worker.rb:54:in
`process_queue'
/Users/your_username/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/gems/bundler-2.3.26/lib/bundler/worker.rb:91:in `block
(2 levels) in create_threads'

An error occurred while installing pg (1.4.5), and Bundler cannot continue.

In Gemfile:
  activerecord-tenant-level-security was resolved to 0.0.1, which depends on
    pg

== Command ["bundle install"] failed ==

解決方法 brew install libpq

brew install libpq
echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc

再び bundle install

bin/setup

または、

bundle install

などで gem install pg できたら完了です。

以上、An error occurred while installing pg (1.4.5), and Bundler cannot continue. を解決した、現場からお送りしました。