在Mavericks系统中使用Postgres.app,无法安装pg gem

76
我正在尝试在我的本地机器上安装pg gem以与Postgres.app一起使用。 我正在运行Mavericks操作系统。
Postgres.app已安装并正常运行,但我无法让gem工作。 我已经完成了以下操作:
1. 使用Postgres.app文档中的命令“env ARCHFLAGS =“ -arch x86_64”gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config” 2. 更新Homebrew并安装Apple GCC 4.2 3. 安装Xcode开发人员工具 4. 更新我的$ PATH以引用Postgres.app bin和lib目录
都没有成功。 这是我收到的具体错误消息:
Building native extensions with: '--with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config'
This could take a while...
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

    /Users/Brian/.rvm/rubies/ruby-2.0.0-p353/bin/ruby extconf.rb --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config
Using config values from /Applications/Postgres.app/Contents/MacOS/bin/pg_config
sh: /Applications/Postgres.app/Contents/MacOS/bin/pg_config: No such file or directory
sh: /Applications/Postgres.app/Contents/MacOS/bin/pg_config: No such file or directory
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
    --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/Brian/.rvm/rubies/ruby-2.0.0-p353/bin/ruby
    --with-pg
    --without-pg
    --with-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}/

我很感激你所提供的任何帮助。谢谢!


8个回答

262

您可能为--with-pg-config指定了错误的路径,请检查该路径是否存在。

您可以使用以下方式找到正确的pg_config路径:

find /Applications -name pg_config

在最新的Postgres.app版本中,路径为:

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

非常尴尬,就是这样。在升级到最新版本的Postgres.app之后,路径发生了变化,我没有注意到。谢谢! - blundin
这也是我的结论! - user1113883
23
谢谢!由于某种原因,在我的系统上,我需要运行 $ gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config 来安装pg gem。 - Gabriel Osorio
3
较新的Postgres.app安装可能需要您使用以下命令:gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config - Sean
1
为了进一步参考,我昨天刚下载了Postgres.app,我的pg_config位于Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config。通过使用“ls”查看“Applications/Postgres.app/Contents”目录,应该能够找出自己的pg_config所在位置。请查看是否有“/Contents/MacOS”或“/Contents/Versions”... - peterhurford
显示剩余7条评论

16
在我的情况下(运行的是Postgres.app v9.3.4.2),只有在前置环境架构标志时,它似乎才能正常工作:
env ARCHFLAGS="-arch x86_64" gem install pg -- \
--with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config 

你可以通过包含 export ARCHFLAGS="-arch x86_64" 将 ARCHFLAGS 放入你的 .bash_profile(或等效文件)中。对我来说,如果没有这个,PG 就无法工作。 - peterhurford
我正在使用Bundler,花了一点时间才弄清楚如何将其添加到“bundle”命令中。您可以使用“bundle config pg.build“....””,更多信息请参见http://bundler.io/v1.3/man/bundle-config.1.html。 - Chad Gorshing

14

在全新的 Mac 上,这是我所做的:

  1. 从应用商店安装 Xcode 工具
  2. 打开 Xcode 工具并接受许可协议
  3. 现在运行以下命令(希望是未来兼容的):

    version=$(ls /Applications/Postgres.app/Contents/Versions/ | tail -1) gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/$version/bin/pg_config .

如果你有问题,可以通过查看 mkmf.log 文件中的实际错误进行一些故障排除。你可以通过运行以下命令找到该文件(如果使用 rvm):

cd ~/.rvm ; find . -name mkmf.log | grep pg

这是Postgres App最新版本的正确路径。 - Michael Shimmins
-- --非常重要,pg可能存在一个错误。 - BAR
“latest”现在作为一个版本,有一个符号链接。话虽如此,我有一台新的M1芯片的Mac,这对我不起作用,我不知道为什么。我检查了路径,一切都在那里。也许是因为它不希望libpq有一个dylib。 - Mig

6
我可以使用以下命令安装pg:

我能够使用这个命令安装pg

    gem install pg -- --with-pg-config=/Library/PostgreSQL/9.3/bin/pg_config

我通过跑步找到了自己的方向。
    sudo find / -name "pg_config"

我成功安装了pg-0.17.1。


使用以下命令可以更容易地找到 pg_config 文件,无需在计算机上搜索所有文件:$ find /Applications/Postgres.app/ -name "pg_config" 在您的计算机上搜索 /Library/ 也可以。 - Powers

3
将PostgreSQL的bin目录添加到路径中也可以解决问题。只需像这样将bin添加到路径中。在最近的安装中,最新的符号链接确保该路径应为未来版本升级的“稳定”路径。
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin

谢谢,将这个添加到 gem install 的前面可以正常工作:PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin gem install pg - Christoph Geschwind

2
为了解决这个问题,我在终端窗口中使用以下命令使用Homebrew安装了Postgres:
brew install postgres

Homebrew can be found here

http://brew.sh


4
这解决了问题,但意味着需要安装另一个完整的Postgres,仅为了那个头文件。 - superluminary
6
如果你正在使用Postgres.app,请不要这样做。事实上,如果存在的话,你应该运行brew uninstall postgres - peterhurford

1

为了以后参考,因为我们很多人都在发布新版本号的新路径:

目前我看到一个名为latest的符号链接在/Applications/Postgres.app/Contents/Versions/中。

您应该能够只需执行以下操作:

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

忘记版本号。这可能不适用于每个(较旧的)版本,但我自己正在寻找可以保存和重复使用的代码片段。


1
这对我很有用:

这对我很有用:

gem install pg -- --with-pg-config=`which pg_config`

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