我使用的是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

我怎么解决这个问题?


当前回答

我可以用另一种方法解决这个问题。我在系统里找不到图书馆。因此,我使用PostgreSQL主网站上的应用程序安装了它。在我的情况下(OS X),我发现文件在/Library/PostgreSQL/9.1/include/安装结束后。如果你已经安装了PostgreSQL,你也可以在其他地方安装这个文件,这取决于你的系统。

多亏了这个关于如何为gem安装添加额外路径的链接,我可以用下面的命令将gem指向lib:

export CONFIGURE_ARGS="with-pg-include=/Library/PostgreSQL/9.1/include/"
gem install pg

在那之后,它就工作了,因为它现在知道在哪里找到丢失的库。只需将路径替换为libpq-fe.h的正确位置

其他回答

看起来在Ubuntu中,头文件是libpq-dev包的一部分(至少在以下Ubuntu版本中是这样的: 11.04 (Natty Narwhal), 10.04 (Lucid Lynx), 11.10 (Oneiric Ocelot), 12.04 (Precise穿山甲),14.04 (Trusty Tahr)和18.04(仿生海狸)):

...
/usr/include/postgresql/libpq-fe.h
...

所以试着为你的操作系统安装libpq-dev或类似的软件:

Ubuntu/Debian系统:sudo apt-get install libpq-dev RHEL系统:yum install postgresql-devel 对于Mac Homebrew: brew安装postgresql 对于Mac MacPorts PostgreSQL: gem install pg -- --with-pg-config=/opt/local/lib/ PostgreSQL[版本号]/bin/pg_config . exe OpenSuse:在postgresql-devel中使用zypper 对于ArchLinux: pacman -S postgresql-libs

libpq-fe.h的位置取决于PostgreSQL的安装位置(这取决于你如何安装它)。使用locate (http://en.wikipedia.org/wiki/Locate_%28Unix%29)在您的机器上查找libpq-fe.h文件。如果它存在,它将在PostgreSQL安装的include目录中。

$ locate libpq-fe.h
/Library/PostgreSQL/9.1/include/libpq-fe.h

包含pg_config的bin目录将位于与include目录相同的目录中。正如错误提示的那样,使用——with-pg-config选项来安装gem:

$ gem install pg --with-pg-config="/Library/PostgreSQL/9.1/bin/pg_config"

注意,如果你从未使用过locate,或者自从安装PostgreSQL后就没有更新过,你可能需要运行updatedb。

在OS X 10.9 (Mavericks)上,我自定义安装了postgresql,然后我必须rvm重新安装Ruby。我现在很开心:)

我在使用Postgresql 9.6时遇到了这个问题。我可以通过这样做来修复它:

brew upgrade postgresql@9.6
brew link postgresql@9.6 --force
gem install pg

我最终解决了这个问题,但没有使用前面描述的方法。

使用brew install postgresql,我发现它已经安装了,但没有链接。

找出PostgreSQL安装的位置,并删除它, 然后再次安装postgresql, 酿造链接postgresql Gem install pg