Ruby on Rails

【Rails】bundle install 時に pg のインストール時にエラーになる

bundle install 時に pg をインストールする際にエラーになったので、備忘録として残しておきたいと思います。

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

    current directory: /Users/sakaishun/Desktop/projects/hotwire-example-template/vendor/bundle/ruby/3.1.0/gems/pg-1.3.1/ext
/Users/sakaishun/.rbenv/versions/3.1.0/bin/ruby -I /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0 -r ./siteconf20240103-60724-l8oj6a.rb extconf.rb
Calling libpq with GVL unlocked
checking for pg_config... yes
Using config values from /Users/sakaishun/anaconda3/bin/pg_config
checking for whether -Wl,-rpath,/Users/sakaishun/anaconda3/lib is accepted as LDFLAGS... yes
Using libpq from /Users/sakaishun/anaconda3/lib
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for pg_config_manual.h... yes
checking for PQconnectdb() in -lpq... no
checking for PQconnectdb() in -llibpq... no
checking for PQconnectdb() in -lms/libpq... no
Can't find the PostgreSQL client library (libpq)
*****************************************************************************

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/sakaishun/.rbenv/versions/3.1.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-pg-dir
	--without-pg-dir
	--with-pg-include
	--without-pg-include=${pg-dir}/include
	--with-pg-lib
	--without-pg-lib=${pg-dir}/lib
	--with-pq-dir
	--without-pq-dir
	--with-pq-include
	--without-pq-include=${pq-dir}/include
	--with-pq-lib
	--without-pq-lib=${pq-dir}/lib
	--with-pqlib
	--without-pqlib
	--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-libpqlib
	--without-libpqlib
	--with-ms/libpq-dir
	--without-ms/libpq-dir
	--with-ms/libpq-include
	--without-ms/libpq-include=${ms/libpq-dir}/include
	--with-ms/libpq-lib
	--without-ms/libpq-lib=${ms/libpq-dir}/lib
	--with-ms/libpqlib
	--without-ms/libpqlib

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

  /Users/sakaishun/Desktop/projects/hotwire-example-template/vendor/bundle/ruby/3.1.0/extensions/arm64-darwin-21/3.1.0/pg-1.3.1/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/sakaishun/Desktop/projects/hotwire-example-template/vendor/bundle/ruby/3.1.0/gems/pg-1.3.1 for inspection.
Results logged to /Users/sakaishun/Desktop/projects/hotwire-example-template/vendor/bundle/ruby/3.1.0/extensions/arm64-darwin-21/3.1.0/pg-1.3.1/gem_make.out

  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/rubygems/ext/builder.rb:95:in `run'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/rubygems/ext/ext_conf_builder.rb:47:in `block in build'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/tempfile.rb:317:in `open'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/rubygems/ext/ext_conf_builder.rb:26:in `build'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/rubygems/ext/builder.rb:161:in `build_extension'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/rubygems/ext/builder.rb:195:in `block in build_extensions'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/rubygems/ext/builder.rb:192:in `each'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/rubygems/ext/builder.rb:192:in `build_extensions'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/rubygems/installer.rb:847:in `build_extensions'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/bundler/rubygems_gem_installer.rb:71:in `build_extensions'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/bundler/rubygems_gem_installer.rb:28:in `install'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/bundler/source/rubygems.rb:204:in `install'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/bundler/installer/gem_installer.rb:54:in `install'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/bundler/installer/gem_installer.rb:16:in `install_from_spec'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/bundler/installer/parallel_installer.rb:186:in `do_install'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/bundler/installer/parallel_installer.rb:177:in `block in worker_pool'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/bundler/worker.rb:62:in `apply_func'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/bundler/worker.rb:57:in `block in process_queue'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/bundler/worker.rb:54:in `loop'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/bundler/worker.rb:54:in `process_queue'
  /Users/sakaishun/.rbenv/versions/3.1.0/lib/ruby/3.1.0/bundler/worker.rb:91:in `block (2 levels) in create_threads'

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

In Gemfile:
  pg

試したこと

brew install libpq とあったので、インストールしたり再インストールしましたが、解決しませんでした。

gem install pg -- --with-pg-config=/path/to/pg_config も試しましたがダメでした。

ログに以下の記載があったので、mkmf.log を見ることにしました。

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

  /Users/sakaishun/Desktop/projects/hotwire-example-template/vendor/bundle/ruby/3.1.0/extensions/arm64-darwin-21/3.1.0/pg-1.3.1/mkmf.log

結果は以下のようになっていて、特に有用な情報はありませんでした。

DYLD_FALLBACK_LIBRARY_PATH=.:/Users/sakaishun/.rbenv/versions/3.1.0/lib "clang -o conftest -I/Users/sakaishun/.rbenv/versions/3.1.0/include/ruby-3.1.0/arm64-darwin21 -I/Users/sakaishun/.rbenv/versions/3.1.0/include/ruby-3.1.0/ruby/backward -I/Users/sakaishun/.rbenv/versions/3.1.0/include/ruby-3.1.0 -I. -I/Users/sakaishun/anaconda3/include -I/Users/sakaishun/.rbenv/versions/3.1.0/include -I/opt/homebrew/opt/libffi/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT   -fdeclspec -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wdeprecated-declarations -Wdivision-by-zero -Wimplicit-function-declaration -Wimplicit-int -Wmisleading-indentation -Wpointer-arith -Wshorten-64-to-32 -Wwrite-strings -Wold-style-definition -Wmissing-noreturn -Wno-constant-logical-operand -Wno-long-long -Wno-missing-field-initializers -Wno-overlength-strings -Wno-parentheses-equality -Wno-self-assign -Wno-tautological-compare -Wno-unused-parameter -Wno-unused-value -Wunused-variable -Wextra-tokens -Wundef  -fno-common -pipe conftest.c  -L. -L/Users/sakaishun/.rbenv/versions/3.1.0/lib -L/Users/sakaishun/anaconda3/lib -L. -L/Users/sakaishun/.rbenv/versions/3.1.0/lib -L/opt/homebrew/opt/libffi/lib -fstack-protector-strong -L/usr/local/lib -Wl,-rpath,/Users/sakaishun/anaconda3/lib     -lruby.3.1 -lms/libpq   "
conftest.c:16:13: error: conflicting types for 'PQconnectdb'
extern void PQconnectdb();
            ^
/Users/sakaishun/anaconda3/include/libpq-fe.h:263:16: note: previous declaration is here
extern PGconn *PQconnectdb(const char *conninfo);
               ^
conftest.c:17:27: error: too few arguments to function call, single argument 'conninfo' was not specified
int t(void) { PQconnectdb(); return 0; }
              ~~~~~~~~~~~ ^
/Users/sakaishun/anaconda3/include/libpq-fe.h:263:16: note: 'PQconnectdb' declared here
extern PGconn *PQconnectdb(const char *conninfo);
               ^
2 errors generated.
checked program was:
/* begin */
 1: #include "ruby.h"
 2:
 3: #include <libpq-fe.h>
 4:
 5: /*top*/
 6: extern int t(void);
 7: int main(int argc, char **argv)
 8: {
 9:   if (argc > 1000000) {
10:     int (* volatile tp)(void)=(int (*)(void))&t;
11:     printf("%d", (*tp)());
12:   }
13:
14:   return !!argv[argc];
15: }
16: extern void PQconnectdb();
17: int t(void) { PQconnectdb(); return 0; }
/* end */

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

解決方法

pgconfig のパスが anaconda3 になっていることが原因でした。

$ which pg_config
/Users/sakaishun/anaconda3/bin/pg_config

homebrew で入れた pg より anaconda3 で入れたものが使われていました。anaconda3 を入れた記憶がなく、今後使わないので anaconda3 を削除したら解決しました。

$ which conda
/Users/sakaishun/anaconda3/bin/conda

$ rm -fr /Users/sakaishun/anaconda3

$ bundle install
bundle install                                                                                                                         (base)  2024-01-03 16:13
Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
Using rake 13.0.6
Using racc 1.6.0
Using crass 1.0.6
Using minitest 5.15.0
Using builder 3.2.4
Using digest 3.1.0
Using mini_mime 1.1.2
Using timeout 0.2.0
Using strscan 3.0.1
Using websocket-extensions 0.1.5
Using marcel 1.0.2
Using public_suffix 4.0.6
Using bindex 0.8.1
Using io-wait 0.2.1
Using nio4r 2.5.8
Using erubi 1.10.0
Using matrix 0.4.2
Using ffi 1.15.5
Using msgpack 1.4.4
Using mini_magick 4.11.0
Using childprocess 4.1.0
Using thor 1.2.1
Using rack 2.2.3
Using redis 4.6.0
Using concurrent-ruby 1.1.9
Using rexml 3.2.5
Using method_source 1.0.0
Using regexp_parser 2.2.1
Using nokogiri 1.13.1 (arm64-darwin)
Using zeitwerk 2.5.4
Using websocket-driver 0.7.5
Using net-protocol 0.1.2
Using rubyzip 2.3.2
Using io-console 0.5.11
Using mail 2.7.1
Fetching pg 1.3.1
Using addressable 2.8.0
Using bundler 2.3.3
Using i18n 1.9.1
Using tzinfo 2.0.4
Using puma 5.6.2
Using rack-test 1.1.0
Using net-imap 0.2.3
Using net-pop 0.1.1
Using net-smtp 0.3.1
Using selenium-webdriver 4.1.0
Using loofah 2.14.0
Using sqlite3 1.4.2
Using bootsnap 1.10.3
Using sprockets 4.0.2
Using xpath 3.2.0
Using rails-html-sanitizer 1.4.2
Using capybara 3.36.0
Using activesupport 7.0.2.2
Using tzinfo-data 1.2022.1
Using activemodel 7.0.2.2
Using reline 0.3.1
Using ruby-vips 2.1.4
Using irb 1.4.1
Using rails-dom-testing 2.0.3
Using globalid 1.0.0
Using activerecord 7.0.2.2
Using activejob 7.0.2.2
Using webdrivers 5.0.0
Using debug 1.4.0
Using actionview 7.0.2.2
Using image_processing 1.12.1
Using actionpack 7.0.2.2
Using jbuilder 2.11.5
Using actioncable 7.0.2.2
Using activestorage 7.0.2.2
Using sprockets-rails 3.4.2
Using railties 7.0.2.2
Using actiontext 7.0.2.2
Using actionmailer 7.0.2.2
Using importmap-rails 1.0.2
Using stimulus-rails 1.0.2
Using turbo-rails 1.0.1
Using web-console 4.2.0
Using actionmailbox 7.0.2.2
Using rails 7.0.2.2
Installing pg 1.3.1 with native extensions
Using capybara_accessible_selectors 0.8.2 from https://github.com/citizensadvice/capybara_accessible_selectors.git (at main@075c32c)
Bundle complete! 19 Gemfile dependencies, 82 gems now installed.
Bundled gems are installed into `./vendor/bundle`

さいごに

万人に当てはまる事象ではないとは思いますが、誰かの役に立てば幸いです。

ABOUT ME
sakai
三重出身の28歳。前職はメーカーで働いていて、プログラミングスクールに通って未経験からWeb業界に転職しました。Railsをメインで使っていて、AWSも少しできます。音楽を聞くこととYoutubeを見るのが好きです。最近はへきトラ劇場にハマってます