在Mac上安装Postgres gem时出错

时间:2015-10-04 16:50:35

标签: ruby-on-rails ruby macos postgresql gem

这是我运行gem install pg时出现的错误:

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,我会:

Warning: postgresql-9.4.4 already installed

同样,gem list显示pg (0.18.3),而简单gem -v pg则显示:

2.4.4

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

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引发了这个错误:

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看到了这一点,我认为这可能是最大的线索:

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:

,这很奇怪
/opt/local/lib/postgresql93/libpq.dylib: Mach-O 64-bit dynamically linked shared library x86_64

这也不起作用:

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

3 个答案:

答案 0 :(得分:2)

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

$ export ARCHFLAGS="-arch x86_64"
$ bundle

答案 1 :(得分:1)

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

--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。

之后我可以运行'bundle'并且所有内容都被正确包含在内。

答案 2 :(得分:0)

这里有几个答案:

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

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