Rails のプロジェクトのローカル環境を作っていて bundle install したら pg の箇所でエラーが発生したのでメモ
環境
- Mac OS Catalina (10.15.6)
- ruby 2.7.1 (rbenv)
- postgresql を
brew install
済み
エラー内容
$ bundle install # … 略 Installing pg 1.2.3 with native extension 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 \*\*\* # … 略 An error occurred while installing pg (1.2.3), and Bundler cannot continue. Make sure that `gem install pg -v '1.2.3' --source 'https://rubygems.org/'` succeeds before bundling. In Gemfile: pg
pg_config のパスを指定する
pg_config
が見つからないので、 --with-pg-config=/path/to/pg_config
オプションで pg_config
のパスを指定しろトノコト
homebrew でインストールした postgresql の bin 内に pg_config
があるのでこのパスを指定する
/usr/local/Cellar/postgresql/<VWESION>/bin/pg_config
こんな感じ。
$ bundle config build.pg --with-pg-config=/usr/local/Cellar/postgresql/<VWESION>/bin/pg_config $ bundle install // …略 Installing pg 1.2.3 with native extension checking for libpq-fe.h... no Can't find the 'libpq-fe.h header \*\*\* extconf.rb failed \*\*\* // …略
libpq-fe.h
が無いというエラーが出続けている…
libpq の pg_config
を指定する
$ brew install libpq
libpq
You can install libpq (dev files are included) alone using homebrew.
cf. postgresql - Install libpq-dev on Mac OS? - Super User
Postgres 無しに devファイルを含む psql などのユーティリティを提供しているパッケージらしい
libpq
にも pg_config
があるのでこちらのファイルを指定してみる
/usr/local/Cellar/libpq/<VWESION>/bin/pg_config
パスは postgresql と同じ感じ
$ bundle config build.pg --with-pg-config=/usr/local/Cellar/libpq/VWESION/bin/pg_config $ bundle install // …略 Installing pg 1.2.3 with native extensions // ...略
問題なく bundle install が完了しました。
このまま問題なくローカル環境は動作しているのですが、postgresql
もインストールしている状態だったので libpq
だけで十分なのかどうかは解っていません。
bundle install gb
でコケて postgresql
の pg_config
のパスを指定してもうまく行かない場合は libpg
を使うと解決するかも知れません。
Ruby まだ慣れてないのでエラーとかがなんもわからん状態です。
おわり
[参考]
- ruby on rails - How can I pass a parameter for gem installation when I run bundle install? - Stack Overflow
- pgがインストールできない - Qiita
- gem install pgしてNo pg_configとなる対処法 - Qiita
- rubyプロジェクトのビルド中にpg gemをインストールしようとすると失敗します
- OSX Can’t find the ‘libpq-fe.h header | by George Opritescu | Medium
- Mac OSにlibpq-devをインストールしますか?
- macos — macOSに完全なPostgresなしでpsqlをインストールする正しい方法は?