【问题标题】:Can't install pg gem on Rails无法在 Rails 上安装 pg gem
【发布时间】:2016-03-27 18:34:44
【问题描述】:

我正在尝试捆绑安装,但由于某些奇怪的原因,pg gem 在安装时返回以下错误:

$ gem install pg -v '0.18.4'
Building native extensions.  This could take a while...
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

    /Users/username/.rvm/rubies/ruby-2.2.1/bin/ruby -r ./siteconf20151221-23315-1tkv3fd.rb 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... no
Can't find the 'libpq-fe.h header
*** 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
    --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=/Users/username/.rvm/rubies/ruby-2.2.1/bin/$(RUBY_BASE_NAME)
    --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}/lib

extconf failed, exit code 1

Gem files will remain installed in /Users/username/.rvm/gems/ruby-2.2.1/gems/pg-0.18.4 for inspection.
Results logged to /Users/username/.rvm/gems/ruby-2.2.1/extensions/x86_64-darwin-15/2.2.0/pg-0.18.4/gem_make.out

谁能帮我安装这个 gem 并让我的包成功完成?

【问题讨论】:

  • 谷歌搜索错误信息(类似这样的好第一步),即 "Can't find the 'libpq-fe.h header" 揭示了很多问题为此,例如stackoverflow.com/questions/6040583/…
  • 先擦bundle config build.pg --with-pg-config=/Applications/Postgres.app/Contents/Versions/(YOUR POSTGRES VERSION)/bin/pg_config 然后bundle install
  • 感谢@AwladLiton 的工作:)

标签: ruby-on-rails ruby


【解决方案1】:

您可能应该安装 PostgreSQL 开发库。

如果您使用的是 Ubuntu,这将有所帮助:

sudo apt-get install libpq-dev

在 Mac 上:

brew install postgresql

【讨论】:

  • 看起来他们在 OS X 上。
  • 对于 Alpine,使用 postgres-dev: apk add postgresql-dev
  • 对于 Fedora:dnf install libpqxx-devel
【解决方案2】:

您需要正确配置 pg。

运行:

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

然后尝试运行 bundle:

bundle install

【讨论】:

    【解决方案3】:

    如果安装了 PostgreSQL 客户端库,那么您需要告诉 gem 在哪里查找包含文件,它通过询问 pg_config 来完成。我使用一个小 shell 文件让 gem 更容易找到该实用程序:

    #!/bin/sh -x
    
    PATH=/Library/PostgreSQL/9.4/bin:$PATH
    gem install pg $@
    

    您也可以手动调整 PATH 以始终包含 PostgreSQL bin 目录,这应该可以永久解决问题。

    如果您升级 PostgreSQL,该路径将会改变,因此您可能需要重新安装 pg 和/或更改您的 PATH 设置。

    安装 pg 后,无需重新运行脚本即可安装 gem 的进一步升级。

    【讨论】:

      猜你喜欢
      • 2012-03-28
      • 2015-04-06
      • 1970-01-01
      • 2017-03-19
      • 2020-02-28
      • 2013-02-20
      • 2015-03-16
      相关资源
      最近更新 更多