かもメモ

自分の落ちた落とし穴に何度も落ちる人のメモ帳

Ruby bundle install gb のエラーにハマる

Rails のプロジェクトのローカル環境を作っていて bundle install したら pg の箇所でエラーが発生したのでメモ

環境

エラー内容

$ 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 が無いというエラーが出続けている…

libpqpg_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 でコケて postgresqlpg_config のパスを指定してもうまく行かない場合は libpg を使うと解決するかも知れません。

Ruby まだ慣れてないのでエラーとかがなんもわからん状態です。
おわり


[参考]