为什么bundle install不能完全安装pg gem?

3

我已经按照所示安装了pg gem,但是...


Building native extensions.  This could take a while...
    Successfully installed pg-0.11.0
    ERROR:  Could not find a valid gem '—' (>= 0) in any repository
    ERROR:  Could not find a valid gem '–with-pg-config=/usr/local/pg/bin/pg_config' (>= 0) in any repository
    1 gem installed
    Installing ri documentation for pg-0.11.0...
    Installing RDoc documentation for pg-0.11.0...

当我运行bundle install时,出现了一些奇怪的情况,pg gem无法完全安装:

Fetching source index for http://rubygems.org/
Using rake (0.9.2) 
Using abstract (1.0.0) 
Using activesupport (3.0.10) 
Using builder (2.1.2) 
Using i18n (0.5.0) 
Using activemodel (3.0.10) 
Using erubis (2.6.6) 
Using rack (1.2.3) 
Using rack-mount (0.6.14) 
Using rack-test (0.5.7) 
Using tzinfo (0.3.29) 
Using actionpack (3.0.10) 
Using mime-types (1.16) 
Using polyglot (0.3.2) 
Using treetop (1.4.10) 
Using mail (2.2.19) 
Using actionmailer (3.0.10) 
Using arel (2.0.10) 
Using activerecord (3.0.10) 
Using activeresource (3.0.10) 
Using annotate (2.4.0) 
Using bundler (1.0.18) 
Using diff-lcs (1.1.2) 
Using factory_girl (1.3.3) 
Using rdoc (3.9.3) 
Using thor (0.14.6) 
Using railties (3.0.10) 
Using rails (3.0.10) 
Using factory_girl_rails (1.0) 
Using faker (0.3.1) 
Using gravatar_image_tag (1.0.0.pre2) 
Using nokogiri (1.5.0) 
Installing pg (0.11.0) with native extensions /Users/******/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/installer.rb:552:in `rescue in block in build_extensions': ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)

然后我会看到一些提示信息,告诉我可能缺少必要的库或头文件:

   *** 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=/Users/TonyNg/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
    --with-pg
    --without-pg
    --with-pg-dir
    --without-pg-dir
    --with-pg-include
    --without-pg-include=${pg-dir}/include
    --with-pg-lib
    --without-pg-lib=${pg-dir}/lib
    --with-pg-config
extconf.rb:24:in ``': No such file or directory - /users/****/

这是我的 Gem 文件:

  source 'http://rubygems.org'

gem 'rails', '3.0.10'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'pg'
gem 'gravatar_image_tag', '1.0.0.pre2'
gem 'will_paginate', '3.0.pre2'



group :development do
  gem 'rspec-rails', '2.6.1'
  gem 'annotate', '2.4.0'
  gem 'faker', '0.3.1'
end

group :test do
  gem 'rspec-rails', '2.6.1'
  gem 'webrat', '0.7.1'
  gem 'spork', '0.9.0.rc8'
  gem 'factory_girl_rails', '1.0'
end

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

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
# gem 'ruby-debug'
# gem 'ruby-debug19', :require => 'ruby-debug'

# Bundle the extra gems:
# gem 'bj'
# gem 'nokogiri'
# gem 'sqlite3-ruby', :require => 'sqlite3'
# gem 'aws-s3', :require => 'aws/s3'

# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
# group :development, :test do
#   gem 'webrat'
# end

我该如何解决这个问题?提前感谢您。

编辑 答案:关键是要安装pg gem到正确的路径下,而之前它没有完全安装。我在我的最后一条评论中运行了 'gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config' ,针对我所提供的路径。我只需要找到我的pg_config文件并在其中运行gem install。


你是否已经安装了PostgreSQL及其相关库?mkmf.log文件中有什么信息呢? - mu is too short
@mu太短了,我在我的Mac上搜索了一下,但找不到这个特定事件的“mkmf.log”。我肯定没有安装该库,因为我得到了以下错误:“检查pg_config...否 没有pg_config...尝试继续。如果构建失败,请使用--with-pg-config=/path/to/pg_config再次尝试 检查libpq-fe.h...否”。我已经安装了PostgreSQL84。但是当我捆绑安装库时,我遇到了这些错误。 - Tony
如果您使用的是Lion操作系统,您需要指定架构。请在Google中搜索“lion pg gem x86_64 archflag env”。 - unmounted
@mu太短了。是的,我有,它在/opt/local/lib/postgresql84/bin/pg_config下,我刚刚打开了它。你说的是执行文件吗?我还有一个pg_config文件夹和.h文件。我不在Lion上,但我尝试过“sudo env ARCHFLAGS="-arch x86_64" gem install pg”,也没有成功。 - Tony
@mu太短了。我明白了!很抱歉打扰您,我是新手。诀窍是在正确的路径下安装pg gem,而之前它没有完全安装。我运行了'gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config',针对我在上一条评论中提到的路径。非常感谢! - Tony
6个回答

19

在此之前,您需要安装libpq-dev。


在OSX上安装它的好方法是从enterprisedb网站下载PG。 - Chris

5

这个技巧是在正确的路径下安装pg gem,之前它并没有完全安装。我找到了pg_config执行文件后,运行了gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config。我只需找到我的pg_config文件并在该路径下运行gem install即可。


5
在Ubuntu 18.04中,需要运行sudo apt install libpq-dev

棒极了 @FiftiN - Yuri Toledo

3

对于Windows系统和pg gem的版本0.13.0及以上,您可以使用以下命令进行安装:

subst X: "C:\Program Files\PostgreSQL\9.0"
gem install pg -- --with-pg=X:
subst X: /D

然后你可以成功地运行bundle install并在输出中看到以下行:

Using pg (0.13.0)

而不是出现错误。

[这与相关问题“无法在Windows上安装pg gem”此答案相同。]


2
在Ubuntu 18.04上运行以下命令。
sudo apt update
sudo apt install libpq-dev

0

iOS? 我不认为 Homebrew 可以在 iOS 上使用,也没有一个可以运行它的 shell。你是不是指 macOS? - Kenny Evitt

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