关于ruby on rails:在Mac上安装Postgres gem时出错

Error installing Postgres gem on Mac

这是我运行gem install pg时得到的错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby -r ./siteconf20151004-5579-1t2l557.rb extconf.rb
checking for pg_config... yes
Using config values from /usr/local/bin/pg_config
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for pg_config_manual.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 ***

如果我运行brew install postgres我得到:

1
Warning: postgresql-9.4.4 already installed

同样,gem list显示pg (0.18.3),简单的gem -v pg给出:

1
2.4.4

运行sudo ARCHFLAGS="-arch x86_64" gem install pg返回:

1
2
3
4
5
6
7
Building native extensions.  This could take a while...
Successfully installed pg-0.18.3
invalid options: -f fivefish
(invalid options are ignored)
Parsing documentation for pg-0.18.3
Done installing documentation for pg after 4 seconds
1 gem installed

但是bundle install引发了这个错误:

1
2
An error occurred while installing pg (0.18.3), and Bundler cannot continue.
Make sure that `gem install pg -v '0.18.3'` succeeds before bundling.

我在mkmf.log中看到了这一点,我认为这可能是最大的线索:

1
2
3
4
5
ld: warning: ignoring file /opt/local/lib/postgresql93/libpq.dylib, file was built for x86_64 which is not the architecture being linked (i386): /opt/local/lib/postgresql93/libpq.dylib
Undefined symbols for architecture i386:
 "_PQconnectdb", referenced from:
      _t in conftest-fc6c12.o
ld: symbol(s) not found for architecture i386

鉴于file /opt/local/lib/postgresql93/libpq.dylib返回x86_64而不是i386,这很奇怪:

1
/opt/local/lib/postgresql93/libpq.dylib: Mach-O 64-bit dynamically linked shared library x86_64

这也不起作用:

1
sudo env ARCHFLAGS="-arch i386 -arch x86_64" gem install pg


看起来你几乎就在那里,因为sudo ARCHFLAGS="-arch x86_64" gem install pg有效。尝试全局设置ARCHFLAGS,因此bundle也可以看到它:

1
2
$ export ARCHFLAGS="-arch x86_64"
$ bundle


从昨天开始,我一直在解决这个问题。我最终卸载了Postgres,自制软件,并从头开始。当试图将'pg'gem添加到我的Ruby on Rail项目时,它仍然失败。挖掘输出我一直看到它尝试链接i386库。所以我尝试添加标志:

1
--arch-flags='x86_64'

这似乎没有任何影响。

然后我找到了一个在'sudo'之后设置的地方。这似乎成功了。

这工作!!!!!!!!

sudo ARCHFLAGS="-arch x86_64" gem install pg -v '0.18.3' -- --with-pg-config=/usr/local/bin/pg_config --arch-flags='x86_64'

在项目中,我实际上不得不在项目目录中出于某种原因重复这些步骤。然后重新重复它们并升级到0.18.4版。

之后,我可以运行'捆绑',所有内容都正确包含在内。


这里有几个答案:

1
http://stackoverflow.com/questions/19625487/impossible-to-install-pg-gem-on-my-mac-with-mavericks

建议在执行gem install时指定postgresql配置文件。它虽然年纪较大,但可能提供前进的方向。搜索"检查已安装的brew中是否有pg_config",其中说明了如何查找正确的文件名。