ERROR: Error installing pg: ERROR: Failed to build gem native extension

ERROR: Error installing pg: ERROR: Failed to build gem native extension

我克隆了一个 rails 存储库,我现在正尝试捆绑安装

背景:

rails -v 在您的 Gemfile 中列出的或本机可用的任何 gem 源中找不到 gem 'pg (>= 0) ruby'。 运行 bundle install 安装缺少 gems.

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 question,但似乎无法找出与 OSX.

等价的内容

看来您只是没有安装 Postgres。使用类似的东西安装它:

brew install postgres

(如果你有 Homebrew

然后再次尝试编译 gem 扩展。