如何在Ubuntu上安装PostgreSQL的pg gem?

309

我想要安装Ruby的PostgreSQL's pg宝石(gem)。

我执行了以下命令:

gem install pg

我使用 RVM 安装了 Ruby 1.9.2。

上述命令显示了以下错误信息。

错误信息如下:

Building native extensions.  This could take a while...

ERROR:  Error installing pg:

ERROR: Failed to build gem native extension.

/home/User/.rvm/rubies/ruby-1.9.2-preview3/bin/ruby extconf.rb

checking for pg_config... yes
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for PQconnectdb() in -lpq... no
checking for PQconnectdb() in -llibpq... no
checking for PQconnectdb() in -lms/libpq... no
Can't find the PostgreSQL client library (libpq)

*** 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/User/.rvm/rubies/ruby-1.9.2-preview3/bin/ruby
 --with-pg
 --without-pg
 --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
 --enable-static-build
 --disable-static-build
 --with-pqlib
 --without-pqlib
 --with-libpqlib
 --without-libpqlib
 --with-ms/libpqlib
 --without-ms/libpqlib

Gem files will remain installed in /home/user/.rvm/gems/ruby-1.9.2-preview3/gems/pg-0.9.0 for inspection.

Results logged to /home/user/.rvm/gems/ruby-1.9.2-preview3/gems/pg-0.9.0/ext/gem_make.out

我不知道错误在哪里...


3
在Ubuntu 18.04上安装PostgreSQL服务器开发所有依赖包,请运行命令apt install postgresql-server-dev-all - Richard Peck
19个回答

6

5
在尝试其他方法均失败后,我在Ubuntu 12.04上采用了这种方法解决了问题。
rvmsudo gem install pg -v '0.12.2' -- --with-pg-lib=/usr/include/postgresql

我的解决方案几乎相同;我正在使用rbenv,对我有用的是:gem install pg -- --with-pg-lib=/usr/lib/postgresql/ --with-pg-include=/usr/include/postgresql/;我不需要指定版本。 - Dan Tenenbaum

3

我正在尝试在新安装的Ubuntu 16.04上设置一个Rails项目。当运行bundle时,我遇到了同样的问题。

sudo apt-get install aptitude

紧接着是
sudo apt-get install libpq-dev

对我来说解决了。


3

对于所有在尝试本页面上的所有答案后仍然遇到问题的人,以下方法(终于)起作用:

sudo apt-get install libgmp3-dev
gem install pg

在完成本页提到的所有其他操作之后,出现了这种情况。

postgresql 9.5.8
Ubuntu 16.10


这就是我在Ubuntu 18.04上所需要的。 - Weston Ganger

2

解决这个问题的另一个方法是使用Homebrew / linuxbrew安装PostgreSQL:

brew install postgresql

习惯上,我不喜欢除非必须使用sudo。


Homebrew 只适用于 Mac。 - IIllIIll
2
有一个Linux分支。 - echristopherson
如果您不想安装完整的PostgreSQL,可以尝试以下命令:brew install libpq && brew link libpq --force - Andrew

2

对于那些试图安装Redmine的人,我错过了上述所有尝试后,需要安装sudo apt-get install ruby-all-dev

最初的错误是mkmf.rb无法在/usr/lib/ruby/include/ruby.h找到ruby的头文件


1

apt-get install ruby-dev 对我来说有效。


1
我曾经遇到同样的问题,并尝试了很多不同的变体。经过一些尝试,我成功地使用了sudo gem install,但仍然无法在没有sudo的情况下安装它。
最后,我找到了一个解决方法 - 重新安装rvm帮助了我。也许能够节省其他人的时间。

0
另一种选择是使用在Linux和macOS上工作的Homebrew仅安装支持库: brew install libpq 然后 brew link libpq --force (需要--force选项,因为它与postgres公式冲突。)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接