0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vagrant+Railsでbindle install時にpgインストールできないエラー

Posted at

内容

VagrantでRailsの環境構築中にbundle installしたとき、pgがインストールできないエラーが発生したのでその対応

環境

  • macOS Mojave 10.14.6
  • VirtualBox 6.0.10
  • Vagrant 2.2.5
  • centos/7 (virtualbox, 1905.1)
  • rbenv 1.1.2-4-g577f046
  • ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]
  • Rails 6.0.0
  • psql (PostgreSQL) 12.0
  • Bundler version 2.0.2

エラー内容

bundle install時にpgがインストールできないエラーが発生

エラー
# エラーを発生させたコマンド
$ bundle install --path vendor/bundle


# エラー内容
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/home/vagrant/share/vendor/bundle/ruby/2.6.0/gems/pg-1.1.4/ext
/home/vagrant/.rbenv/versions/2.6.5/bin/ruby -I
/home/vagrant/.rbenv/versions/2.6.5/lib/ruby/site_ruby/2.6.0 -r
./siteconf20191019-11690-11ilaca.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.6.5/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/share/vendor/bundle/ruby/2.6.0/extensions/x86_64-linux/2.6.0-static/pg-1.1.4/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/home/vagrant/share/vendor/bundle/ruby/2.6.0/gems/pg-1.1.4 for inspection.
Results logged to
/home/vagrant/share/vendor/bundle/ruby/2.6.0/extensions/x86_64-linux/2.6.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

やったこと

解決1

エラー文中の次の内容に従って--with-pg-config=...をつけてみる

# エラー
...
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
...
解決1
# つけてみる
$ gem install pg -v 1.1.4 -- --with-pg-config=/usr/pgsql-9.3/bin/pg_config

# bundle install
$ bundle install --path vendor/bundle

一つの環境中ではこちらで解決しました

解決2

もう一つの環境中では上記で解決しませんでした。
エラーログ中に**「致命的なエラー: libpq-fe.h: そのようなファイルやディレクトリはありません」と出力されている場合はpostgresql-devel**をインストールで解決

エラーログの確認方法は次の通り

エラーログの確認
# bundle installした時に発生したエラーの中で次の文で始まる箇所のmkmf.logを確認する
...
To see why this extension failed to compile, please check the mkmf.log which can
be found here:

/home/vagrant/share/vendor/bundle/ruby/.../mkmf.log
...


# mkmf.logの内容確認
$ cat /home/vagrant/share/vendor/bundle/ruby/.../mkmf.log

致命的なエラー: libpq-fe.h: そのようなファイルやディレクトリはありません」と表示がある場合は次のコマンドで解決

解決2
# postgresql-develインストール
$ sudo yum -y install postgresql-devel

# bundle install
$ bundle install --path vendor/bundle
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?