Gem::Ext::BuildError: 错误:无法构建gem本地扩展。PostgreSQL和ROR相关。

10
我需要在ROR应用程序的生产环境中安装postgresql,以便在heroku上使用。但是,在尝试安装'pg'宝石时会触发以下错误。由于我是新手,所以不知道该怎么办。
我正在使用ruby 1.9.3p547(2014-05-14修订45962)[i686-linux]和Rails 3.2.19以及Ubuntu 12.04。
GemFile:
源 'https://rubygems.org'
gem 'rails','3.2.19'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'mysql2'
gem 'hirb'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

group :production do
  gem 'pg'

end


# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'

当我运行bundle update或bundle install时,出现了问题。
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /home/fernando/.rvm/rubies/ruby-1.9.3-p547/bin/ruby -r ./siteconf20141203-14261-3uu564.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
    --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=/home/fernando/.rvm/rubies/ruby-1.9.3-p547/bin/ruby
    --with-pg
    --without-pg
    --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 /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/pg-0.17.1 for inspection.
Results logged to /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/extensions/x86-linux/1.9.1/pg-0.17.1/gem_make.out
An error occurred while installing pg (0.17.1), and Bundler cannot continue.
Make sure that `gem install pg -v '0.17.1'` succeeds before bundling.
4个回答

32

您的问题在日志中被描述为No pg_config... trying anyway.

这个配置涉及libpq依赖项,当我开始使用Rails时,我也遇到了这种问题,因此,我认为答案就是按照以下步骤操作:

在控制台中运行此命令。

sudo apt-get install libpq-dev 
sudo gem install pg

2
谢谢rderoldan1。它完美地解决了问题。对于新手来说,面对这些问题确实很困难。 - daniel barrera
2
sudo apt-get install libpq-dev 这解决了我的问题。 - Vishwas Nahar

14

以上解决方案对我无效,但这里的说明有效。以下是总结:

  • 确保安装了PostgreSQL
  • 使用sudo find / -name "pg_config"查找新的pg_config路径
  • 复制路径
  • 使用gem install pg -- --with-pg-config=path/to/pg_config运行
  • 成功!

对我来说有效。谢谢。gem install pg -v '1.2.3' -- --with-pg-config=/usr/pgsql-9.5/bin/pg_config - Alessandro Gurgel

0
根据 rderoldan1 的解决方案,我在 Fedora 23 上运行了以下命令:
dnf install libpq-dev 

但是我收到了这条消息:

No package libpq-dev available. Error: Unable to find a match.

最初,PostgreSQL是通过以下命令安装的:

dnf install postgresql-server postgresql-contrib

当我运行which pg_config时,我得到了这个:

/usr/bin/which: no pg_config in (/usr/local/heroku/bin:/usr/local/rvm/gems/ruby-2.2.4/bin:/usr/local/rvm/gems/ruby-2.2.4@global/bin:/usr/local/rvm/rubies/ruby-2.2.4/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/usr/local/rvm/bin:/home/mayur/.local/bin:/home/mayur/bin)

所以我再次使用以下命令安装了 PostgreSQL:

dnf install postgresql-devel

现在当我运行which pg_config时,我得到:
/usr/bin/pg_config

而且我可以正常运行bundle install没有任何错误。


0
在我的情况下,我使用的是Postgres 9.5.2,Ruby 2.3.0和Rails 4.2.6,在OSX El Capitan 10.11.5上。所有都设置好了,并且在本地机器上进行生产环境部署时工作良好。尝试了上面的解决方案,但对我来说都不起作用,只是给了我正确的方向。
所以,我的问题主要是关于bundler而不是gem安装。为了解决这个问题,首先需要按照上面的说明重新正确安装gem。pg_config的路径可能因Postgres版本而异。
$gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.5/bin/pg_config

在打包之前,您可能需要正确地配置它,然后进行打包安装

$bundle config build.pg --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.5/bin/pg_config
You are replacing the current global value of build.pg, which is currently "--with-pg-config=/opt/local/lib/postgresql91/bin/pg_config"

现在你应该能够顺利运行bundle install

希望这能帮到你。


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