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

我怎么解决这个问题?


当前回答

对于CentOS 6.4,

yum install postgresql-devel
gem install pg

工作得很好!

其他回答

对于AltLinux,是postgresqlx包。X-devel(在我的例子中是postgresql9.5-devel)必须安装:

apt-get install postgresql9.5-devel

我也尝试过做gem安装libpq-dev,但我收到了这个错误:

Can't find the 'libpq-fe.h header
*** extconf.rb failed ***

然而,我发现使用sudo apt-get(我尽量避免在Ruby on Rails中使用)安装是有效的,即。

sudo apt-get install libpq-dev
# or
apt-get install postgres-server-dev-{pg.version}
# for postgresql 9.4 on Ubuntu 14.04

然后我就能做了

gem install pg

没有问题。

在MacOS M1上解决:

brew install libpq

然后我输入我的路径

echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc

And:

gem install pg

Ubuntu 20.04.1 LTS,这似乎对我有用:

sudo apt-get install ruby ruby-dev libpqxx-dev libpq-fe-dev libpq-dev 
sudo gem install pg -- --with-pg-config=/usr/include/postgresql/pg_config --with-pg-include=/usr/include/postgresql/

我可以用另一种方法解决这个问题。我在系统里找不到图书馆。因此,我使用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的正确位置