无法在Rails上安装pg gem

19

我正尝试进行捆绑安装,但是由于某种奇怪的原因,pg宝石在安装时返回以下错误:

$ 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 并使我的 bundle 成功完成吗?


1
谷歌搜索错误信息(任何类似问题的好第一步),例如“Can't find the 'libpq-fe.h header”,会显示很多关于此问题的SO问题,例如https://dev59.com/EG025IYBdhLWcg3wYE-G。 - Max Williams
2
首先运行 bundle config build.pg --with-pg-config=/Applications/Postgres.app/Contents/Versions/(YOUR POSTGRES VERSION)/bin/pg_config,然后运行 bundle install - Awlad Liton
谢谢@AwladLiton,这个方法可行 :) - gespinha
3个回答

37

你可能需要安装PostgreSQL开发库。

如果你使用的是Ubuntu,可以按照以下方法处理:

sudo apt-get install libpq-dev

在Mac电脑上:

brew install postgresql

1
看起来他们正在使用OS X操作系统。 - Dave Newton
1
对于Alpine,请使用postgres-devapk add postgresql-dev - gabe
对于 Fedora:dnf install libpqxx-devel - Dogmatixed

10
你需要正确配置pg。
运行:
bundle config build.pg --with-pg-config=/Applications/Postgres.app/Contents/Versions/(YOUR POSTGRES VERSION)/bin/pg_config

然后尝试运行bundle:

bundle install

1
如果已安装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 就可以在不重新运行脚本的情况下安装。

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