【问题标题】:POSTGRESQL Failed to build gem native extensionPOSTGRESQL 无法构建 gem 原生扩展
【发布时间】:2016-03-08 19:29:20
【问题描述】:

好的,当我尝试创建一个新的 Rails 项目时,我遇到了这个问题, 但首先我会告诉你我已经设置了常见的问题(Ruby 版本、bundler、brew postgresql

~
▶ type rbenv
rbenv is a shell function

▶ gem list | grep bundler
bundler (1.10.6)

▶ rbenv local
rbenv: no local version configured for this directory

▶ which psql
/usr/local/bin/psql

▶ which bundler
/Users/hectorleon/.rbenv/shims/bundler

▶ which postgresql
postgresql not found

▶ which psql     
/usr/local/bin/psql
▶ brew list
autoconf    git     mpfr      openssl     python      readline    xz
boost     gmp     mysql     pcre      python3     redis     zsh
gcc     isl     node      pkg-config    rbenv     ruby-build    zsh-completions
gdbm      libmpc      numpy     postgresql    rbenv-gem-rehash  sqlite

▶ createuser myusername
createuser: creation of new role failed: ERROR:  role "myusername" already exists

▶ brew install postgresql
Warning: postgresql-9.4.5_2 already installed

▶ ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]

▶ gem install pg
Building native extensions.  This could take a while...
Successfully installed pg-0.18.4
Parsing documentation for pg-0.18.4
Done installing documentation for pg after 2 seconds
1 gem installed

▶ brew list
autoconf    git     libpqxx     numpy     postgresql    rbenv-gem-rehash  sqlite
boost     gmp     mpfr      openssl     python      readline    xz
gcc     isl     mysql     pcre      python3     redis     zsh
gdbm      libmpc      node      pkg-config    rbenv     ruby-build    zsh-completions

好的,使用 Homebrew 和 rbenv 安装了一堆东西。现在让我们创建一个 rails 项目,看看会发生什么。

▶ rails new whygodwhy -d postgresql
      create  
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/views/layouts/application.html.erb
      create  app/assets/images/.keep
      create  app/mailers/.keep
      create  app/models/.keep
      create  app/controllers/concerns/.keep
      create  app/models/concerns/.keep
      create  bin
      create  bin/bundle
      create  bin/rails
      create  bin/rake
      create  bin/setup
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/secrets.yml
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/assets.rb
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/cookies_serializer.rb
      create  config/initializers/filter_parameter_logging.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/session_store.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  lib
      create  lib/tasks
      create  lib/tasks/.keep
      create  lib/assets
      create  lib/assets/.keep
      create  log
      create  log/.keep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/robots.txt
      create  test/fixtures
      create  test/fixtures/.keep
      create  test/controllers
      create  test/controllers/.keep
      create  test/mailers
      create  test/mailers/.keep
      create  test/models
      create  test/models/.keep
      create  test/helpers
      create  test/helpers/.keep
      create  test/integration
      create  test/integration/.keep
      create  test/test_helper.rb
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/javascripts
      create  vendor/assets/javascripts/.keep
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.keep
         run  bundle install


Your user account isn't allowed to install to the system Rubygems.
You can cancel this installation and run:

    bundle install --path vendor/bundle

to install the gems into ./vendor/bundle/, or you can enter your password
and install the bundled gems to Rubygems using sudo.

Password: 
Fetching gem metadata from https://rubygems.org/............
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies....
Using rake 10.4.2
Using i18n 0.7.0
Using json 1.8.3
Using minitest 5.8.3
Using thread_safe 0.3.5
Using tzinfo 1.2.2
Using activesupport 4.2.5
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile2 2.0.0
Using nokogiri 1.6.7
Using rails-deprecated_sanitizer 1.0.3
Using rails-dom-testing 1.0.7
Using loofah 2.0.3
Using rails-html-sanitizer 1.0.2
Using actionview 4.2.5
Using rack 1.6.4
Using rack-test 0.6.3
Using actionpack 4.2.5
Using globalid 0.3.6
Using activejob 4.2.5
Using mime-types 2.99
Using mail 2.6.3
Using actionmailer 4.2.5
Using activemodel 4.2.5
Using arel 6.0.3
Using activerecord 4.2.5
Using debug_inspector 0.0.2
Using binding_of_caller 0.7.2
Using bundler 1.10.6
Using byebug 8.2.1
Using coffee-script-source 1.10.0
Using execjs 2.6.0
Using coffee-script 2.4.1
Using thor 0.19.1
Using railties 4.2.5
Using coffee-rails 4.1.0
Using concurrent-ruby 1.0.0
Using multi_json 1.11.2
Using jbuilder 2.3.2
Using jquery-rails 4.0.5
Installing pg 0.18.4 with native extensions

Gem::Installer::ExtensionBuildError: 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.

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
  --without-pg-config
  --with-pg_config
  --without-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 /var/folders/m0/v30d40yd31bc5hf1lqrt5zn00000gn/T/bundler20151205-52465-pssww8pg-0.18.4/gems/pg-0.18.4 for inspection.
Results logged to /var/folders/m0/v30d40yd31bc5hf1lqrt5zn00000gn/T/bundler20151205-52465-pssww8pg-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 'rails (= 4.2.5) ruby' in any of the gem sources listed in your Gemfile or available on this machine. (Bundler::GemNotFound)
  from /Library/Ruby/Gems/2.0.0/gems/bundler-1.10.6/lib/bundler/resolver.rb:325:in `each'
  from /Library/Ruby/Gems/2.0.0/gems/bundler-1.10.6/lib/bundler/resolver.rb:325:in `verify_gemfile_dependencies_are_found!'
  from /Library/Ruby/Gems/2.0.0/gems/bundler-1.10.6/lib/bundler/resolver.rb:198:in `start'
  from /Library/Ruby/Gems/2.0.0/gems/bundler-1.10.6/lib/bundler/resolver.rb:182:in `resolve'
  from /Library/Ruby/Gems/2.0.0/gems/bundler-1.10.6/lib/bundler/definition.rb:200:in `resolve'
  from /Library/Ruby/Gems/2.0.0/gems/bundler-1.10.6/lib/bundler/definition.rb:140:in `specs'
  from /Library/Ruby/Gems/2.0.0/gems/bundler-1.10.6/lib/bundler/definition.rb:185:in `specs_for'
  from /Library/Ruby/Gems/2.0.0/gems/bundler-1.10.6/lib/bundler/definition.rb:174:in `requested_specs'
  from /Library/Ruby/Gems/2.0.0/gems/bundler-1.10.6/lib/bundler/environment.rb:18:in `requested_specs'
  from /Library/Ruby/Gems/2.0.0/gems/bundler-1.10.6/lib/bundler/runtime.rb:13:in `setup'
  from /Library/Ruby/Gems/2.0.0/gems/bundler-1.10.6/lib/bundler.rb:127:in `setup'
  from /Library/Ruby/Gems/2.0.0/gems/bundler-1.10.6/lib/bundler/setup.rb:18:in `<top (required)>'
  from /Users/myusername/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
  from /Users/myusername/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'

好的...所以我看到说“找不到 PostgreSQL 客户端库 (libpq)” 怎么安装呢?不知道,在 Google 上什么也没找到。

此外,如果我尝试进入 postgresql 文件夹,我会收到此消息

postgres does not know where to find the server configuration file. You must specify the --config-file or -D invocation option or set the PGDATA environment variable

总结:

  • 我成功安装了 pg gem(已成功创建本机扩展),但之后,rails 项目说我无法构建本机扩展。
  • 说当我安装了 bundler 并且 rbenv nice 时我需要密码才能安装 gems:S 没有意义
  • 我没有 libpq,我不知道如何/在哪里或需要什么命令:S
  • 我安装了 postgresql,但没有安装服务器配置文件
  • 我要死了哈哈,真的没有人可以帮助我吗?我在两天内浪费了大约 5 个小时试图解决这个问题,笔记本电脑是新的并且安装了 OSX EL Capitán。

【问题讨论】:

  • 这看起来你正在选择使用系统 ruby​​ 安装的 Rails 版本
  • 尝试运行错误消息的建议并解决可能的依赖关系:gem install pg -v '0.18.4'。当我阅读 msg 时,您似乎缺少 rails 本身??
  • @FrederickCheung 我怎样才能检查采取了什么红宝石? :S
  • @benjamin 两个命令都返回我找不到 Gemfile 但是 gemfile 在已经创建的项目中,所以我不明白
  • which rails 会告诉你 rails install 来自哪里

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


【解决方案1】:

找不到 PostgreSQL 客户端库 (libpq)

您缺少名为 libpq 的库。安装后,查看错误消息的变化情况,如有必要,请重复该过程。

可能是这个库的名称稍有不同,例如 libpq5,所以如果您没有找到 100% 的匹配项,您可能想尝试一下。

【讨论】:

  • 我有 2 个问题:1)我需要 libpq(不知道在哪里或如何安装它:S)2)postgres 不知道在哪里可以找到服务器配置文件。您必须指定 --config-file 或 -D 调用选项或设置 PGDATA 环境变量。
  • SO 上的人更愿意提供帮助,如果他们看到提问者方面的一些研究工作。我希望您参考 GOOGLE 搜索的第一个结果,关键字与您的问题标题相同。无论您是否通过这样做来解决您的问题,您都可能希望更新您的原始帖子。
  • 我看到了类似的问题,但不完全相同,我安装了 ruby​​,我安装了 rbenv,我还安装了使用 brew 和 gem pg 安装的 postgresql(这些是在其他 SO 中解决的常见问题主题),但我仍然有这个问题,所以我认为在我的情况下是另一个问题:S @benjamin
  • Héctor,你过得怎么样?
  • 仍然失败,所以今天下午我将删除有关 postgreSQL 的所有内容并尝试重新安装,因为您使用 mysql 创建了一个新项目并且一切正常。
猜你喜欢
  • 2015-07-20
  • 2015-06-18
  • 2012-07-26
  • 2014-10-25
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
相关资源
最近更新 更多