我使用的是Ruby on Rails 3.1预版本。我喜欢使用PostgreSQL,但问题是安装pg gem。它给了我以下错误:

$ gem install pg
Building native extensions.  This could take a while...
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

        /home/u/.rvm/rubies/ruby-1.9.2-p0/bin/ruby 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/u/.rvm/rubies/ruby-1.9.2-p0/bin/ruby
    --with-pg
    --without-pg
    --with-pg-dir
    --without-pg-dir
    --with-pg-include
    --without-pg-include=${pg-dir}/include
    --with-pg-lib
    --without-pg-lib=${pg-dir}/lib
    --with-pg-config
    --without-pg-config
    --with-pg_config
    --without-pg_config
    Gem files will remain installed in /home/u/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.11.0 for inspection.
    Results logged to /home/u/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.11.0/ext/gem_make.out

我怎么解决这个问题?


当前回答

我通过rvm重新安装Ruby修复了同样的错误:

rvm reinstall 1.9.3

其他回答

在Fedora 32上,我通过专门安装libpq5-devel解决了同样的问题。

如果你从官方存储库安装postgresql,你会有几个版本都不能解决这个问题,所以这真的是一个尝试它需要哪个版本的问题。

我对Fedora 30的解决方案:

sudo dnf install /usr/include/libpq-fe.h

在CentOS上,我使用下面的命令安装libpq-dev包

yum install postgresql-devel

执行gem install pg返回与“No pg_config…”不管怎样。如果构建失败,请使用——with-pg-config=/path/to/pg_config"再试一次。

安装宝石如下解决了我的问题

gem install pg -- --with-pg-config=/usr/pgsql-x.x/bin/pg_config

在Debian 7.0, 64位(Wheezy)上,只需运行:

sudo apt-get install libpq-dev

成功安装libpq-dev后,运行:

bundle install

我刚刚在OSX上运行了brew和postgres@9.4。

我的解决方案是这样的:

CONFIGURE_ARGS="with-pg-include=/usr/local/opt/postgresql@9.4/include/" bundle install