Rails的PostgreSQL gem无法安装,即使已经安装了home brew和应用程序。

8
我尝试着按照这个答案的方法让gem工作,但是它没有生效。我的项目设置成每个项目都有自己的gems而不是所有的gems都在全局空间中,然后我使用binstubs来让我做一些像bin/rails这样的事情。
所以所有的gems都安装在每个项目的.bundle/gems/中。最常给我带来麻烦的是postgresql。接下来我们来看看步骤。
我运行: bundle 它爆炸了并显示以下错误消息:
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
Using config values from /Applications/Postgres.app/Contents/Versions/9.3/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 ***
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=/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
    --with-pg
    --without-pg
    --enable-windows-cross
    --disable-windows-cross
    --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}/
    --with-pqlib
    --without-pqlib
    --with-libpqlib
    --without-libpqlib
    --with-ms/libpqlib
    --without-ms/libpqlib


Gem files will remain installed in /Users/Adam/Documents/Rails-Projects/AisisPlatform/.bundle/gems/gems/pg-0.18.1 for inspection.
Results logged to /Users/Adam/Documents/Rails-Projects/AisisPlatform/.bundle/gems/gems/pg-0.18.1/ext/gem_make.out
An error occurred while installing pg (0.18.1), and Bundler cannot continue.
Make sure that `gem install pg -v '0.18.1'` succeeds before bundling.

因为我安装了自制版本的 PostgreSQL 9.4.0 和 posgresql.app,所以我接着执行了以下操作:

bundle config build.pg --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config

紧接着,因为我使用的是18.1版本。
gem install pg -v '0.18.1'

我得到了:

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

接下来,我尝试了bundle。但是我们又回到了起点,因为即使这个 gem 已经安装,我仍然收到了完全相同的错误提示。

这是因为我是在全局范围内安装 pg gem 而不是本地范围内吗?我该如何解决这个问题呢?每次我执行rm -rf .bundle/gems命令时,这个项目总会出现相同的问题。

需要指出的是,即使我使用 Homebrew 的 psql pg_config,按照上述步骤进行操作,也会得到相同的结果。


你尝试过这里的任何解决方案吗?(https://dev59.com/42025IYBdhLWcg3wHR8Y) - Antarr Byrd
6个回答

15

归根结底,它是:

ARCHFLAGS="-arch x86_64" bundle install

这对我很有效。

原因在于:

默认情况下,它会尝试编译一个通用二进制文件,但显然会失败... 因此这个环境变量只会使其编译x86版本,这就是你所需要的全部

您可以将此行添加到〜/ .profile或类似文件中:export ARCHFLAGS="-arch x86_64"

更多阅读请参见:这个OSX README


感谢您发布这个问题和后续的答案!我仍然有点不清楚为什么这样可以解决问题,但似乎我们现在告诉它使用64位架构,并且这个解决方法只是因为与pg gem相关的某些内容没有正确编译为通用二进制文件。 - Drew Ogryzek

10

首先确保在您的计算机上安装了Postgres。

对于Ubuntu系统:sudo apt-get install libpq-dev 对于RHEL系统:yum install postgresql-devel 对于Mac系统:brew install postgresql

然后运行bundle install。


3

我使用了这个链接中的命令:

rails 4.2.0: can't install pg gem on ubuntu 14.04

您需要安装带有PostgreSQL头文件的postgreSQL开发包。

sudo apt-get install libpq-dev

您也可以尝试以下命令:

sudo apt-get install postgresql-client

sudo apt-get install postgresql postgresql-contrib


0
尝试使用以下命令安装pg:env ARCHFLAGS="-arch x86_64" gem install pg

如果您正在使用rbenv,rbenv-bundler插件也可能会有所帮助。 - Antarr Byrd
完全相同的错误。我不知道发生了什么,也不知道为什么这个项目会如此失败。宝石安装得很好,但“bundle”失败了,说我需要确保宝石先安装...除了上面概述的错误之外。 - TheWebs
我正在使用rvm,并且我正在使用ruby 2.2.0。我的所有项目都使用RVM。为了简单起见,正如所述,每个项目的所有gems都安装在.bundle/gems/中,以避免在全局空间中发生任何冲突。我还使用binstubs来允许我执行bin/rails而不是rails。没有其他使用pg gem和2.2.0的项目面临这个问题,我有另外三个项目。 - TheWebs

-1
尝试安装postgresql-devel包:

yum -y install postgresql-devel

请注意,由于-y,上述命令不会询问是否允许安装。

-1

确认安塔尔·伯德的答案,

 sudo env ARCHFLAGS='-arch x86_64' gem install pg  

对我有用。


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