无法安装pg gem

256
我尝试使用gem install pg,但好像不起作用。 gem install pg出现了这个错误。
Temporarily enhancing PATH to include DevKit...
Building native extensions.  This could take a while...
ERROR:  Error installing pg:
        ERROR: Failed to build gem native extension.

C:/Ruby/bin/ruby.exe 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:/Ruby/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 C:/Ruby/lib/ruby/gems/1.8/gems/pg-0.10.1 for
inspection.
Results logged to C:/Ruby/lib/ruby/gems/1.8/gems/pg-0.10.1/ext/gem_make.out

3
如果您使用的是Mac电脑,请按照这篇教程操作:http://matthew.mceachen.us/blog/howto-wrangle-the-pg-postgresql-gem-with-macports-and-rvm-1159.html - Noah W. Smith
2
@NatchiQ 链接失效了吗? - kingsfoil
1
在我的情况下,错误日志显示找不到 libpq,所以我安装了 sudo apt install postgresql postgresql-contrib libpq-dev pgadmin3 -y - Raj
1
尝试打开一个新的终端选项卡并重试。这对我有效。我刚刚安装了psql,而我正在尝试安装gem的窗口中没有将psql添加到其路径中。 - Jared Menard
25个回答

438

我遇到了这个问题,以下方法对我有效:

安装 postgresql-devel 包,这将解决 pg_config 缺失的问题。

sudo apt-get install libpq-dev

142
通过“apt-get install libpq-dev”对我很有用。感谢提示。 - Ryanmt
5
确认一下,从 pg gem Wiki 首页 上得知:"在 Ubuntu 上,/usr/bin/pg_config 是由 libpq-dev 包提供的。" - Mark Berry
在 openSUSE 中对我有用:zypper in postgresql-devel。修复了配置和 libpq。 - Hiasinho
27
抱歉回复有些晚了,如果您已经安装了Homebrew,可以使用命令brew install postgresql获取所需的软件包。 - Alex LaFroscia
24
请在Mac上尝试使用brew install postgresql安装PostgreSQL。 - Mahattam
显示剩余3条评论

110

问题出在宝石依赖,因此在安装pg之前请确保您已经安装了"libpq-dev"

Ubuntu系统:

sudo apt-get install libpq-dev

RHEL系统:

yum安装postgresql-devel

Mac:

brew安装postgresql


9
Mac的答案是正确的。只是因为我们没有在这台机器上安装PostgreSQL。 - Hoang Le
在Centos 7上,通过使用命令yum install postgresql-devel解决了我在安装'pg' .gem时与pg_config相关的错误。顺便说一下,我选择使用刚发布的PostgreSQL 10版本。 - Arthur

69

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

可以安装一个名为“pg”的ruby gem,这个gem可以用于连接PostgreSQL数据库。在安装时,需要指定使用的pg_config文件路径,以确保正确编译安装。

3
这对我很有帮助(在CentOS 6.2上) - TuK
14
在我的 OS X 系统上成功了,但需要使用路径 /Applications/Postgres.app/Contents/MacOS/bin/pg_config(我使用的是独立的 Postgres.app)。 - Matt
10
在使用Postgres App的Yosemite系统上安装pg gem,可以运行以下命令:gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config - skilleo
在 PC 上工作得很好,但在 Mac 上,以 env ARCHFLAGS="-arch x86_64" 为前缀对我来说是一次重大的改变。 - Janusz Lenar
3
我使用了 gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/latest/bin/pg_config 在 OSX 10.12 上安装。 - Tim Krins
显示剩余3条评论

46

@Winfield 说过::

pg gem需要与PostgreSQL客户端绑定。这个错误通常意味着它找不到您的Postgres库。您可能没有安装它们,或者可能需要在gem install命令中传递--with-pg-dir=参数。

更进一步,您只需要使用--with-pg-config=就可以安装它。

在Mac上

如果您在Mac上通过网站捆绑包(bundle)安装了postgresql,它应该在类似于/Applications/Postgres.app/Contents/Versions/9.3/bin的地方。

因此,您要么在gem install命令中传递它:

gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config

或者你可以正确设置PATH环境变量。但这可能有点麻烦,所以你可以临时按以下步骤设置PATH:

export PATH=%PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin/

3
针对特定版本进行安装:gem install pg -v '0.17.1' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config - Dan Sandland
Postgres.app 内容文件夹中有一个名为 latest 的符号链接文件夹,如果不再提供 9.3 版本,则很有用。gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/latest/bin/pg_config - Stephen Silber

29

这在我的情况下起作用:

sudo apt-get install libpq-dev

我使用了:

  • Ubuntu 14.04.2 LTS
  • Ruby 2.2.2
  • Rails 4.2.1

25

我没有安装postgresql,所以我只是使用以下命令进行了安装

sudo apt-get install postgresql postgresql-server-dev-9.1

在Ubuntu 12.04上,这个问题已经解决了。


更新:

请使用最新版本:

sudo apt-get install postgresql-9.3 postgresql-server-dev-9.3

问题对我来说已经解决了,但在我的 Mac 上(使用 Homebrew),我必须运行以下命令:brew install postgresql - gMale

14

在这里回答:

无法在Windows上安装pg gem

最新版本的pg(0.10.0)昨天发布,没有Windows本地版本,但如果您安装0.9.0,则应该可以安装二进制文件而不会出现问题。


14

如果你在 Mac 上使用 Postgres.app,你可以像这样一劳永逸地解决这个问题:

首先运行gem uninstall pg,然后编辑你的~/.bash_profile 或者 ~/.zshrc 文件或其它等价文件,添加以下内容:

# PostgreSQL bin path
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.4/bin

然后执行 bundle installgem install pg 命令,两者都应该能够正常工作。


谢谢,使用brew安装的PostgreSQL也可以正常工作。

PostgreSQL二进制路径

export PATH=$PATH:/usr/local/Cellar/postgresql/9.5.0/bin/
- Benoit
谢谢!我从使用brew安装的Postgres切换到Postgress.app后遇到了这个问题。 - luigi7up

11
在macOS(El Capitan)上,您可以简单地使用:brew install postgresql来安装PostgreSQL。

1
是的!这就是我需要的全部!在干净的OS X上安装,不需要Postgres.app。 - Lane Rettig

10
$ PATH=$PATH:/Library/PostgreSQL/9.1/bin sudo gem install pg

用你系统安装的版本替换9.1。


1
这对我在 Mac 上有所帮助,有人知道默认路径是什么以及如何更改吗? - Steve
适用于OS X Maverick操作系统 - Marek Kirejczyk

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