错误: 安装pg时出错: 错误: 无法构建本地扩展宝石

5
我克隆了一个 Rails 仓库,现在正在尝试运行 bundle install。
背景信息:
- 操作系统:OSX El Capitan - Ruby 版本:2.2.3p173(2015 年 8 月 18 日修订版 51636)[x86_64-darwin15] - Rails 版本:rails -v
出现错误提示:在您的 Gemfile 中未找到 gem 'pg (>= 0) ruby',也没有这个 gem 可用于此计算机。请运行 bundle install 安装缺失的 gem。
bundle install
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 jbuilder 2.3.2
Using jquery-rails 4.0.5


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:
Installing pg 0.18.3 with native extensions

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /usr/local/opt/ruby/bin/ruby -r ./siteconf20151105-3981-12lvpnr.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=/usr/local/Cellar/ruby/2.2.3/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 /var/folders/lj/c7hfzz254cd3lt2nxscv_wgw0000gn/T/bundler20151105-3981-149quvepg-0.18.3/gems/pg-0.18.3 for inspection.
Results logged to /var/folders/lj/c7hfzz254cd3lt2nxscv_wgw0000gn/T/bundler20151105-3981-149quvepg-0.18.3/extensions/x86_64-darwin-15/2.2.0/pg-0.18.3/gem_make.out
An error occurred while installing pg (0.18.3), and Bundler cannot continue.
Make sure that `gem install pg -v '0.18.3'` succeeds before bundling.

目前,我运行“gem install pg -v '0.18.3'”命令。

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

    /usr/local/opt/ruby/bin/ruby -r ./siteconf20151105-4004-1ylkqpt.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=/usr/local/Cellar/ruby/2.2.3/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 /usr/local/lib/ruby/gems/2.2.0/gems/pg-0.18.3 for inspection.
Results logged to /usr/local/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-15/2.2.0/pg-0.18.3/gem_make.out

我的 gem 文件底部有以下内容

# Use sqlite3 as the database for Active Record
group :development, :test do
  gem 'sqlite3'
end

group :production do
  gem 'rails_12factor'
  gem 'pg'
end

我在查看以下SO问题,但似乎无法找到相当于OSX的等价物。

1个回答

10

看起来您没有安装Postgres。使用类似以下的命令进行安装:

brew install postgres
(如果您已经安装了Homebrew)然后再尝试编译gem扩展。

可以了!但是错误信息中哪里提到了Postgres呢? - Beast_Code
1
“pg” gem 是 Postgres 的意思 :) - Dmitry Sokurenko

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