LoginSignup
21
17

More than 5 years have passed since last update.

pgがインストールできない

Posted at

pgのインストールでエラーが出る

やろうとしたこと

Rails new したらbundle installでエラーが発生した。
解決に時間がかかってしまいました。

エラー内容

Fetching pg 1.1.4
Installing pg 1.1.4 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/home/vagrant/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/pg-1.1.4/ext
/home/vagrant/.rbenv/versions/2.5.1/bin/ruby -I
/home/vagrant/.rbenv/versions/2.5.1/lib/ruby/site_ruby/2.5.0 -r
./siteconf20190420-18674-1mi6rsi.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=/home/vagrant/.rbenv/versions/2.5.1/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:

/home/vagrant/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-linux/             2.5.0-static/pg-1.1.4/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/home/vagrant/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/pg-1.1.4 for
inspection.
Results logged to
/home/vagrant/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/extensions/x86_64-linux/             2.5.0-static/pg-1.1.4/gem_make.out

An error occurred while installing pg (1.1.4), and Bundler cannot
continue.
Make sure that `gem install pg -v '1.1.4' --source 'https://rubygems.org/'`
succeeds before bundling.

In Gemfile:
  pg

やったこと

ここ見るとgem install pg ができていないらしい。

An error occurred while installing pg (1.1.4), and Bundler cannot
continue.
Make sure that `gem install pg -v '1.1.4' --source 'https://rubygems.org/'`
succeeds before bundling.

とりあえずこれやってみたけどできなかった。

$ gem install pg -v '1.1.4'

「pgインストールできない」とかエラーメッセージで検索しつつエラー文をしっかり読む。

いくつか記事を見つけて試してみる。
が、この辺のはだめでした。

$ yum install postgresql
$ yum install postgresql-devel

このようなエラーが出たという記事を発見。自分のをよく見ると同じエラーが。

Can't find the 'libpq-fe.h header

なのでこのファイルがないのかと思ってインストールしようとかしてみたけどだめでした。

エラー文よくみるとさっきのメッセージの上にこんなエラーが。

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

pg_configが見つからないと。
それで --with-pg-config=/path/to/pg_config をつけてみてと。
参考の一番下のサイトを参考にpg_config探したらあった。ので、次のコマンドを使用。
そしたらできました。

$bundle config build.pg --with-pg-config=/usr/pgsql-9.3/bin/pg_config
$bundle install

まとめ

今回はpg_configの参照が上手くいってなかったようでした。なのでbundleするときの参照場所を変更してからbundle installでできました。

環境

centos6
vagrant
rails5
postgresql9.5

参考

https://qiita.com/kiyodori/items/aa13cea7fe96e417d133
http://jyoko.hatenablog.com/entry/2016/10/08/gem_%27pg%27_%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%E6%99%82%E3%81%AE%E3%82%A8%E3%83%A9%E3%83%BC%E8%A7%A3%E6%B1%BA%E6%96%B9%E6%B3%95
https://qiita.com/youcune/items/5b783f7fde45d0fd4b35

21
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
17