Rails 3 - 无法安装pg gem

100
每当我尝试运行bundle(bundle install)时,总是会出现以下提示:
Installing pg (0.13.2) with native extensions 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /Users/ryan/.rvm/rubies/ruby-1.9.2-p290/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=/Users/ryan/.rvm/rubies/ruby-1.9.2-p290/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 /Users/ryan/.rvm/gems/ruby-1.9.2-p290/gems/pg-0.13.2 for inspection.
Results logged to /Users/ryan/.rvm/gems/ruby-1.9.2-p290/gems/pg-0.13.2/ext/gem_make.out
An error occured while installing pg (0.13.2), and Bundler cannot continue.
Make sure that `gem install pg -v '0.13.2'` succeeds before bundling.

我使用的是Mac OS X 10.6操作系统,安装的PostgreSQL版本是9.1。我发现问题出在libpq-dev上,我应该如何安装/修复它呢?


2
你在 Mac 上如何安装 Postgres? - shingara
1
方式 - user984621
点击安装程序?Flink?还是MacPort? - shingara
我尝试通过这种方式安装:sudo port install libpq-dev,但又出现了另一个问题 - Error: Port libpq-dev not found。要报告错误,请参见<http://guide.macports.org/#project.tickets>。这太糟糕了,还有一些问题... - user984621
1
请查看这个问题:这里 - banditKing
16个回答

100

1
感谢,就像我的情况一样 - 不需要运行bundle gem install pg。 - valk
1
截至2013年11月,在我运行Ubuntu 13.04时有效! - Starkers
为什么这个明显是针对错误操作系统的帖子会有这么多赞? - sevenseacat
1
@sevenseacat,它明确说明了应该在哪个操作系统上使用。很明显,许多使用Linux操作系统的人在研究这个问题时会找到这个答案。 - E.E.33
如果您使用存储库,这应该可以工作。显然,如果您从源代码编译,则无法工作。但是,在使用存储库时,在Ubuntu 14.04上它运行得非常好。 - Donato
这使它起作用了。关键点是需要在您的计算机上安装PostgreSQL。 - JackHasaKeyboard

78

根据您的错误日志,您需要传递pg_config的路径。尝试使用以下命令安装gem:

gem install pg -- --with-pg-config= 'PATH_TO_YOUR_PG_CONFIG'
如果你不确定你的pg_config在哪里,假设你使用的是Linux或Mac系统,你可以运行以下命令:
which pg_config

根据您安装PostgreSQL的方式,您的pg-config可能位于不同的位置。


3
这里的二进制包:http://www.enterprisedb.com/products-services-training/pgdownload#osx 绝对包含了 'pg_config'(例如,对于 9.1.x,在 /Library/PostgreSQL/9.1/bin/pg_config 中)和开发头文件。这是我用来测试 pg gem 的开发工具包。 - Michael Granger
8
“which pg_config” 比 “find / -name pg_config” 更快吗? - Justin D.
13
在 macOS 上,您也可以使用 brew 安装 libpqxx 头文件,只需执行 brew install libpqxx 即可,一切都会顺利进行。 - Vetsin
由于某些原因,“which”未找到“pg_config”文件的位置。对我来说,它位于“/Library/PostgreSQL/9.1/bin/pg_config”。以防这可能会帮助其他人。 - Mike McCoy
如果您正在使用Heroku的POSTGRES应用程序,则pg_config位于/Applications/Postgres.app/Contents/MacOS/bin/pg_config。 - jmstone617
显示剩余4条评论

58
如果您正在使用Postgress.app,则需要访问其命令行工具。在终端或PATH配置文件中输入以下行:
 PATH="/Applications/Postgres.app/Contents/MacOS/bin:$PATH"

gem install pg 现在应该可以工作了。(这是我成功的方法。)

注意: 新版本的路径看起来像:

/Applications/Postgres.app/Contents/Versions/<version>/bin

3
非常感谢!我花了半个小时翻遍了一些无用的东西,最终在这里发现了这个解决方法。 :D - Peter Brown
1
不需要使用brew的好解决方案。 - lucian303
7
请注意,Postgre.app在93版本中进行了更改。现在的路径是:/Applications/Postgres93.app/Contents/MacOS/bin。 - Alain
2
这对我有用,尽管我的bin在一个非常不同的位置 -> /Library/PostgreSQL/9.3/bin - Jeremiah
Postgres.app在这个文档页面中解释了更新后要使用的路径:http://postgresapp.com/documentation/cli-tools.html - Jeff French
5
如果你正在使用Postgres.app,那么从9.3.5版本开始,/Applications/Postgres.app/Contents/Versions/9.3/bin是现在的路径。请注意,此为路径翻译,不包含任何解释性内容。 - Ashley

22

12

搜索libpq:

brew search libpq

应该输出libpqxx

然后尝试安装它:

brew install libpqxx

8
您只需要安装libpq-dev
sudo apt-get install libpq-dev

那么这个宝石应该会成功安装。

在我的情况下,只有这个帮助了。谢谢! - kovpack

7

适用于OSX 10.9.5, 默认的Ruby版本为2.0,rbenv本地版本为1.9.3。 - jlucasps
适用于OSX 10.11。谢谢。 - Yarin
适用于OSX 10.11.4。谢谢! - Martyn Chamberlin

3
我遇到的问题是由于某种原因,它试图使用 /usr/bin/gcc-4.2 进行编译。我通过更改 mkmf.rb 中的 try_cpp(在堆栈跟踪中看到)来发现这一点,并引发了一个异常与编译失败的行。
我将 gcc 软链接到 gcc-4.2 上,然后它就可以工作了:
sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2

为什么它要使用gcc-4.2?我不知道。

实际编译命令行:

/usr/bin/gcc-4.2 -E -I/Users/dfrankow/.rvm/rubies/ruby-1.9.3-p125/include/ruby-1.9.1/x86_64-darwin11.4.0 -I/Users/dfrankow/.rvm/rubies/ruby-1.9.3-p125/include/ruby-1.9.1/ruby/backward -I/Users/dfrankow/.rvm/rubies/ruby-1.9.3-p125/include/ruby-1.9.1 -I. -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE   -I/usr/local/Cellar/postgresql/9.1.4/include  -O3 -ggdb -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wshorten-64-to-32 -Wimplicit-function-declaration  -fno-common -pipe  conftest.c -o conftest.i (RuntimeError)

3

我遇到了同样的问题,但我的Postgres是安装在

/Library/PostgreSQL/9.3

通过添加以下内容更新了~/.bash_profile:

export PATH=/Library/PostgreSQL/9.3/bin:$PATH

打开一个新的终端,运行bundle update,这对我也起作用了。感谢Ari。


但是为了让我能够工作,需要检查版本,然后导出路径=/应用程序/Postgres.app/Contents/Versions/9.4/bin:$PATH。 - PKul

1

我在我的openSUSE13.1 KDE机器上也遇到了同样的问题。在遇到这个问题之前,我只安装了postgresqlpostgresql-server两个包,使用zypper命令安装。然后又安装了另外两个包:

[arup@to_do_app]$ sudo zypper in postgresql-devel postgresql-contrib
root's password:
Loading repository data...
Reading installed packages...
Resolving package dependencies...
#....

然后,我再次运行了bundle install,成功了!!!

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