无法在Mountain Lion上安装pg gem

8

我在升级到Mountain Lion后,Rails出现了很多问题。

为了解决这些问题,我在网上搜索了数小时,并采取了以下措施,以确保我的系统在升级后具备运行Rails的所有依赖项:

  1. I updated my Homebrew installation
  2. I updated my RVM installation
  3. I installed the XCode command line tools
  4. I installed GCC as mentioned
  5. I upgraded my version of Ruby to

    ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0]

  6. My Rails version is verified as Rails 3.2.6
  7. I also found out that Mountain Lion has some issues with Postgres so I saw this post and found out that I need to add this line to my Bash profile:

    export PATH="/usr/local/bin:/usr/bin:$PATH"
    

现在我要去我的Rails应用程序,以前在Snow Leopard上运行得很好,但在我升级后,当我在命令行中运行rails s时,我会收到以下错误:

AM@~/Documents/RailsWS/app0815 >gem install pg
 Building native extensions.  This could take a while...
 ERROR:  Error installing pg:
 ERROR: Failed to build gem native extension.
 /Users/AM/.rvm/rubies/ruby-1.9.2-p290/bin/ruby extconf.rb
 checking for pg_config... yes
 Using config values from /usr/bin/pg_config
 checking for libpq-fe.h... *** 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/AM/.rvm/rubies/ruby-1.9.2-p290/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
--without-pg-config
--with-pg_config
--without-pg_config
 /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:368:in `try_do': 
 The complier failed to generate an executable file. (RuntimeError)
 You have to install development tools first.
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:452:in `try_cpp'
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:853:in `block in   find_header'
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:693:in `block in checking_for'
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:280:in `block (2  levels) in postpone'
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:254:in `open'
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:280:in `block in postpone'
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:254:in `open'
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:276:in `postpone'
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:692:in `checking_for'
from /Users/AM/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/mkmf.rb:852:in `find_header'
from extconf.rb:41:in `<main>'


 Gem files will remain installed in /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/pg-0.14.1 for inspection.
 Results logged to /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/pg-0.14.1/ext/gem_make.out

当我进入Rails应用并尝试运行bundle install时,pg安装失败。

请注意,您的Rails和Ruby版本都比较老,并且存在已知的安全漏洞。截至目前,最新版本为Rails 3.2.8和Ruby 1.9.2-p320/1.9.3-p194。 - Andrew Marshall
1
你是否像我之前建议的那样,通过homebrew安装了postgres,就像你之前(现在已删除?)的问题中提到的那样? - deefour
@Deefour,我不记得看到你的回复了。 - banditKing
4个回答

13

http://www.enterprisedb.com/products-services-training/pgdownload#osx下载PostgreSQL安装程序。

然后将您的路径设置为类似于以下内容:

PATH=$PATH:/Library/PostgreSQL/9.0/bin/ gem install pg

这样做就可以了!

注意:请在路径中正确指定您安装的PostgreSQL版本。

更新(10/11/2015)(摘自Heroku

在使用Homebrew的OS X上:

  1. 使用以下命令安装postgresql:

    brew install postgresql

  2. 然后使用以下命令安装gem:

    gem install pg -- --with-pg-config=/usr/local/bin/pg_config

在使用MacPorts的OS X上:

gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config

希望能帮到你!


1
哥们,你真是我的英雄!我花了约一个小时才找到解决方案! - Stepan Suvorov

12
如banditKing在他的回答中所说,需要告诉gem二进制文件在哪里可以找到postgres。你可以使用Heroku提供的出色且易于使用的postgres.app(http://postgresapp.com)中包含的二进制文件来安装此gem,而不是使用EnterpriseDb版本。虽然EnterpriseDB版本很棒,但对于开发机器来说,安装过程较为复杂。
在您的系统上下载Postgres.app 并将其内部bin目录添加到路径中。我将其放在/Applications/Postgres.app中,因此我使用了以下命令:
gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config

在 10.8.2 版本、Ruby 1.9.3、pg-0.14.1 和 XCode CLI 工具上对我有效


与其他解决方案相比,这个对我起作用了!谢谢 :) - chris

0

0
安装 Mavericks 后,bundle update 在 pg gem 上出现错误。
我使用 Brew 管理我的软件包,postgresql 已经安装了,但仍然出现“没有 pg_config”的错误。
解决方法是只需执行 brew uninstall postgresql,然后执行 brew install postgresql。之后,我立即能够成功运行 bundle update

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