postgresql Rails本机gem扩展失败

时间:2015-12-31 08:01:02

标签: ruby-on-rails postgresql rubygems pg

我正在尝试创建一个新的rails-api项目。我从以下命令rails-api new <name> -T -d postgresql开始,我收到以下命令:

Gem files will remain installed in /var/folders/lk/2ckzjmfx03sgkryx70nc3_c40000gn/T/bundler20151231-18995-t8e1f5pg-0.18.4/gems/pg-0.18.4 for inspection.
Results logged to /var/folders/lk/2ckzjmfx03sgkryx70nc3_c40000gn/T/bundler20151231-18995-t8e1f5pg-0.18.4/gems/pg-0.18.4/ext/gem_make.out
An error occurred while installing pg (0.18.4), and Bundler cannot continue.
Make sure that `gem install pg -v '0.18.4'` succeeds before bundling.
         run  bundle exec spring binstub --all
/Library/Ruby/Gems/2.0.0/gems/bundler-1.10.6/lib/bundler/resolver.rb:347:in `block in verify_gemfile_dependencies_are_found!': Could not find gem 'pg (>= 0) ruby' in any of the gem sources listed in your Gemfile or available on this machine. (Bundler::GemNotFound)

我跟进sudo gem install pg -v '0.18.0',我收到以下错误:

Building native extensions.  This could take a while...
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby 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 ***
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.

此时我不确定我能做些什么。我可以找到/usr/local/bin/pg_config的路径我只是不知道如何解决这个问题。任何帮助将不胜感激。

我也试过ARCHFLAGS="-arch i386" bundle install(同样的结果)&amp; ARCHFLAGS="-arch x86_64" bundle install得到了:

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=/usr/pgsql-9.2/bin/pg_config
Using config values from /usr/pgsql-9.2/bin/pg_config
sh: /usr/pgsql-9.2/bin/pg_config: No such file or directory
sh: /usr/pgsql-9.2/bin/pg_config: No such file or directory
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 PQconninfo()... yes
checking for rb_encdb_alias()... yes
checking for rb_enc_alias()... yes
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 rb_str_modify_expand()... yes
checking for rb_hash_dup()... yes
checking for PGRES_COPY_BOTH in libpq-fe.h... yes
checking for PGRES_SINGLE_TUPLE in libpq-fe.h... yes
checking for PG_DIAG_TABLE_NAME in libpq-fe.h... yes
checking for struct pgNotify.extra in libpq-fe.h... yes
checking for unistd.h... yes
checking for inttypes.h... yes
checking for ruby/st.h... yes
checking for C99 variable length arrays... yes
creating extconf.h
creating Makefile

make "DESTDIR="
compiling gvl_wrappers.c
compiling pg.c
compiling pg_binary_decoder.c
pg_binary_decoder.c:52:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
pg_binary_decoder.c:83:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
2 warnings generated.
compiling pg_binary_encoder.c
compiling pg_coder.c
pg_coder.c:24:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
1 warning generated.
compiling pg_connection.c
compiling pg_copy_coder.c
compiling pg_errors.c
compiling pg_result.c
compiling pg_text_decoder.c
compiling pg_text_encoder.c
compiling pg_type_map.c
compiling pg_type_map_all_strings.c
compiling pg_type_map_by_class.c
compiling pg_type_map_by_column.c
compiling pg_type_map_by_mri_type.c
compiling pg_type_map_by_oid.c
compiling pg_type_map_in_ruby.c
pg_type_map_in_ruby.c:145:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
1 warning generated.
compiling util.c
linking shared-object pg_ext.bundle
ld: file not found: dynamic_lookup
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [pg_ext.bundle] Error 1

我再次感谢您可以花时间帮助我。

1 个答案:

答案 0 :(得分:2)

编译本机扩展需要通过系统安装程序安装一些开发库。

Can't find the PostgreSQL client library (libpq)

安装homebrew时,postgresql安装了该库。但是你没有指定正确的架构,这就是为什么它找不到的原因。指定x86_64之后找到了lib:

checking for PQconnectdb() in -lpq... yes

但他的时间错误pg_config被使用了:

Using config values from /usr/pgsql-9.2/bin/pg_config
sh: /usr/pgsql-9.2/bin/pg_config: No such file or directory
sh: /usr/pgsql-9.2/bin/pg_config: No such file or directory

所以你需要手动指定它,如下所示:

ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/usr/local/bin/pg_config