3
2

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 3 years have passed since last update.

Windows上で pg を bundle install をしてもエラーになる場合

Posted at

現象

WIndows 上で postgres をインストールして、さぁ bundle で pg の gem をインストールしようとしたらエラーになった。

エラー内容

> bundle install
Fetching gem metadata from https://rubygems.org/..............
Resolving dependencies...
(略)
Fetching pg 1.2.3
Installing pg 1.2.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
C:/app/vendor/bundle/ruby/3.0.0/gems/pg-1.2.3/ext
C:/Ruby30-x64/bin/ruby.exe -I C:/Ruby30-x64/lib/ruby/3.0.0 -r
./siteconf20210821-13888-ozsqpz.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=C:/Ruby30-x64/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:

C:/app/vendor/bundle/ruby/3.0.0/extensions/x64-mingw32/3.0.0/pg-1.2.3/mkmf.log

extconf failed, exit code 1
Gem files will remain installed in
C:/app/vendor/bundle/ruby/3.0.0/gems/pg-1.2.3 for inspection.
Results logged to
C:/app/vendor/bundle/ruby/3.0.0/extensions/x64-mingw32/3.0.0/pg-1.2.3/gem_make.out

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

結論

.bundle/configBUNDLE_BUILD__PGで指定したパスが以下のようにシングルクォーテーションで囲まれるようにすればOK

---
BUNDLE_PATH: "vendor/bundle"
BUNDLE_BUILD__PG: "--with-pg-config='C:\\Program Files\\PostgreSQL\\13\\bin\\pg_config.exe'"

経緯

エラーでぐぐってみると以下のようなQiita記事が。

pgがインストールできない - Qiitaより

参考の一番下のサイトを参考にpg_config探したらあった。ので、次のコマンドを使用。
そしたらできました。

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

コマンドオプションの --with-pg-config の内容はLinuxの場合のようなので、
Windowsの場合、ということで以下のように実行してみた。

> bundle config build.pg --with-pg-config=`C:\Program Files\PostgreSQL\13\bin\pg_config.exe'

しかし、bundle install してみてもエラーは取れず。

> bundle install
Fetching gem metadata from https://rubygems.org/..............
Resolving dependencies...
(略)
Fetching pg 1.2.3
Installing pg 1.2.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
C:/app/vendor/bundle/ruby/3.0.0/gems/pg-1.2.3/ext
C:/Ruby30-x64/bin/ruby.exe -I C:/Ruby30-x64/lib/ruby/3.0.0 -r
./siteconf20210821-18424-bvr5j2.rb extconf.rb --with-pg-config\=C:\\Program\\
Files\\PostgreSQL\\13\\bin\\pg_config
Using config values from C:\Program\
*** 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:
        --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=C:/Ruby30-x64/bin/$(RUBY_BASE_NAME)
        --with-pg
        --without-pg
        --enable-windows-cross
        --disable-windows-cross
        --with-pg-config
extconf.rb:33:in ``': No such file or directory - "C:\\Program\\" --includedir
(Errno::ENOENT)
        from extconf.rb:33:in `<main>'

extconf failed, exit code 1

Gem files will remain installed in
C:/app/vendor/bundle/ruby/3.0.0/gems/pg-1.2.3 for inspection.
Results logged to
C:/app/vendor/bundle/ruby/3.0.0/extensions/x64-mingw32/3.0.0/pg-1.2.3/gem_make.out

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

ただ、少しエラー内容が変わって以下のNo such file or directoryエラーに。

extconf.rb:33:in ``': No such file or directory - "C:\\Program\\" --includedir

Windowsあるあるの「Program Files に入れたプログラムを指定する場合にうまく指定できない」が発生しているということで、
bunlde の config が格納されている .bundle\config (グローバルに指定した場合は、C:\Users\(ユーザ名)\.bundle\config)を直接編集して無事インストールできました。

途中で実行した以下のコマンドをうまく指定すればよかったんだろうなと思ってます(解決する気なし)

> bundle config build.pg --with-pg-config=`C:\Program Files\PostgreSQL\13\bin\pg_config.exe'
3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?