错误:在Mavericks上构建宝石本机扩展失败。

46

我正在尝试在OSX 10.9上运行我的Rails项目中的bundle。但当它到达pg gem时失败,并显示以下错误:

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /Users/kyledecot/.rvm/rubies/ruby-2.0.0-p247/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... yes
checking for libpq/libpq-fs.h... yes
checking for pg_config_manual.h... yes
checking for PQconnectdb() in -lpq... yes
checking for PQconnectionUsedPassword()... yes
checking for PQisthreadsafe()... yes
checking for PQprepare()... yes
checking for PQexecParams()... yes
checking for PQescapeString()... yes
checking for PQescapeStringConn()... yes
checking for PQescapeLiteral()... yes
checking for PQescapeIdentifier()... yes
checking for PQgetCancel()... yes
checking for lo_create()... yes
checking for pg_encoding_to_char()... yes
checking for pg_char_to_encoding()... yes
checking for PQsetClientEncoding()... yes
checking for PQlibVersion()... yes
checking for PQping()... yes
checking for PQsetSingleRowMode()... yes
checking for rb_encdb_alias()... yes
checking for rb_enc_alias()... no
checking for rb_thread_call_without_gvl()... yes
checking for rb_thread_call_with_gvl()... yes
checking for rb_thread_fd_select()... yes
checking for rb_w32_wrap_io_handle()... no
checking for PGRES_COPY_BOTH in libpq-fe.h... no
checking for PGRES_SINGLE_TUPLE in libpq-fe.h... no
checking for PG_DIAG_TABLE_NAME in libpq-fe.h... no
checking for struct pgNotify.extra in libpq-fe.h... yes
checking for unistd.h... yes
checking for ruby/st.h... yes
creating extconf.h
creating Makefile

make "DESTDIR="
compiling gvl_wrappers.c
clang: warning: argument unused during compilation: '-fno-fast-math'
compiling pg.c
clang: warning: argument unused during compilation: '-fno-fast-math'
pg.c:272:9: warning: implicit declaration of function 'PQlibVersion' is invalid in C99 [-Wimplicit-function-declaration]
        return INT2NUM(PQlibVersion());
               ^
In file included from pg.c:48:
In file included from ./pg.h:17:
In file included from /Users/kyledecot/.rvm/rubies/ruby-2.0.0-p247/include/ruby-2.0.0/ruby.h:33:
/Users/kyledecot/.rvm/rubies/ruby-2.0.0-p247/include/ruby-2.0.0/ruby/ruby.h:1167:21: note: instantiated from:
# define INT2NUM(v) INT2FIX((int)(v))
                    ^
pg.c:272:9: note: instantiated from:
        return INT2NUM(PQlibVersion());
               ^
pg.c:272:17: note: instantiated from:
        return INT2NUM(PQlibVersion());
                       ^
pg.c:375:48: error: use of undeclared identifier 'PQPING_OK'
        rb_define_const(rb_mPGconstants, "PQPING_OK", INT2FIX(PQPING_OK));
                                                      ^
pg.c:375:56: note: instantiated from:
        rb_define_const(rb_mPGconstants, "PQPING_OK", INT2FIX(PQPING_OK));
                                                              ^
pg.c:377:52: error: use of undeclared identifier 'PQPING_REJECT'
        rb_define_const(rb_mPGconstants, "PQPING_REJECT", INT2FIX(PQPING_REJECT));
                                                          ^
pg.c:377:60: note: instantiated from:
        rb_define_const(rb_mPGconstants, "PQPING_REJECT", INT2FIX(PQPING_REJECT));
                                                                  ^
pg.c:379:57: error: use of undeclared identifier 'PQPING_NO_RESPONSE'
        rb_define_const(rb_mPGconstants, "PQPING_NO_RESPONSE", INT2FIX(PQPING_NO_RESPONSE));
                                                               ^
pg.c:379:65: note: instantiated from:
        rb_define_const(rb_mPGconstants, "PQPING_NO_RESPONSE", INT2FIX(PQPING_NO_RESPONSE));
                                                                       ^
pg.c:381:56: error: use of undeclared identifier 'PQPING_NO_ATTEMPT'
        rb_define_const(rb_mPGconstants, "PQPING_NO_ATTEMPT", INT2FIX(PQPING_NO_ATTEMPT));
                                                              ^
pg.c:381:64: note: instantiated from:
        rb_define_const(rb_mPGconstants, "PQPING_NO_ATTEMPT", INT2FIX(PQPING_NO_ATTEMPT));
                                                                      ^
1 warning and 4 errors generated.
make: *** [pg.o] Error 1


Gem files will remain installed in /Users/kyledecot/.rvm/gems/ruby-2.0.0-p247@skateboxes/gems/pg-0.17.0 for inspection.
Results logged to /Users/kyledecot/.rvm/gems/ruby-2.0.0-p247@skateboxes/gems/pg-0.17.0/ext/gem_make.out

An error occurred while installing pg (0.17.0), and Bundler cannot continue.
Make sure that `gem install pg -v '0.17.0'` succeeds before bundling.
11个回答

54

确保你已经升级了Xcode,在Mavericks上重新安装命令行工具。在终端中输入:

也可以尝试以下步骤:

xcode-select --install

然后跟随提示。


19
这对我没用。我从CLI安装程序中得到了“无法安装软件,因为它目前不可从软件更新服务器获取”的提示。进一步的研究表明Xcode 5默认带有CLI工具。Sayanee的答案适用于我。 - Joseph Juhnke
2
@JosephJuhnke 我也遇到了同样的问题。你可以从苹果官网手动下载和安装。这对我很有效。https://developer.apple.com/downloads/index.action?=command%20line%20tools# - Robert Wagstaff
奇怪的是,我记得我最近安装了这个软件,但按照指示再次运行安装程序后,问题得到了解决。谢谢! - counterbeing
我已经安装了Xcode,但还没有同意许可协议。运行sudo xcodebuild -license解决了这个问题。 - furtive

47

我尝试过以下方法,对我有用:

brew install postgresql

1
谢谢,这解决了我的问题。 - slycrel
1
有时候你需要在执行了 "brew install postgresql" 之后运行 "gem uninstall pg"。 - d1jhoni1b
我也试过了,有效!谢谢@user702342 - Noah Herron
对我有用!看起来使用brew可以解决这个问题。 - Ben

27

对我来说,在Mavericks和Postgresapp上运行良好:

gem install pg -v '0.17.0' -- --with-pg-config=/Applications/Postgres93.app/Contents/MacOS/bin/pg_config

3
这个方法对我也起作用了,对于那些复制这个命令的人,请注意路径中你的Postgres.app的名称。 - mateusmaso
2
对于任何使用OS X Postgres应用程序进行安装的人,需要使用Postgres/Contents/Versions/9.3/bin/pg_config。 - Dan Bradbury

14

尝试使用--with-pg-config参数:

bundle config build.pg --with-pg-config=/usr/pgsql-9.1/bin/pg_config

展示 pg_config 的路径:

which pg_config

对于Ubuntu用户:

sudo apt-get install postgresql
sudo apt-get install libpq-dev

那么:

bundle install

12

对于我使用 Mavericks、Rails 3.2.13、Ruby 2.0.0-p247 和 PostgreSQL 9.1 的情况,我需要执行以下操作:

gem install pg -v '0.15.1' -- --with-pg-config=/Library/PostgreSQL/9.1/bin/pg_config

谢谢,这个方法也对我有用,我的系统是OSX Mavericks final版本,Python版本是9.3.1。 - Rubytastic

3

这对我在Mac OS X 10.9.3和Postgres.app 9.3.4.2版本上都有效:

1)安装Postgres.app

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

3)gem install pg -v '0.17.1'

4)bundle install


1
对我而言有效的是 gem install pg -v '0.18.1' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config - Naoise Golden

3

我刚刚通过以下方法使其运行:

ARCHFLAGS="-arch x86_64" gem install pg

使用brew安装完Postgres之后


3
当我尝试安装时
apt-get install libpq-dev

安装失败,存在未满足的依赖问题,请尝试解决。
apt-get remove libpq5

然后安装。
apt-get install libpq-dev

最终解决了gem pg的问题。


2

您需要将pg配置为当前版本,我使用的是Yosemite上的9.4版本。以下是相应的命令:

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

您应该使用自己的版本

bundle config build.pg --with-pg-config=/Applications/Postgres.app/Contents/Versions/(YOUR POSTGRES VERSION)/bin/pg_config 

然后执行。
bundle install

2
以下方法适用于我:
gem install pg -v '0.18.1' -- --with-pg config=/Library/PostgreSQL/9.3/bin/pg_config

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