Rails 3 - 无法安装pg gem

时间:2012-03-12 14:17:42

标签: ruby-on-rails macos postgresql-9.1 pg libpq

当我尝试运行bundle(bundle install)时,我总是得到

Installing pg (0.13.2) with native extensions 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /Users/ryan/.rvm/rubies/ruby-1.9.2-p290/bin/ruby 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=/Users/ryan/.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


Gem files will remain installed in /Users/ryan/.rvm/gems/ruby-1.9.2-p290/gems/pg-0.13.2 for inspection.
Results logged to /Users/ryan/.rvm/gems/ruby-1.9.2-p290/gems/pg-0.13.2/ext/gem_make.out
An error occured while installing pg (0.13.2), and Bundler cannot continue.
Make sure that `gem install pg -v '0.13.2'` succeeds before bundling.

我使用的是Mac OS X 10.6,安装的PostgreSQL的版本是9.1。我发现问题出现在 libpq-dev 中,我该如何安装/修复?

16 个答案:

答案 0 :(得分:98)

如果你在Linux上运行,你可能会对我有用的东西感兴趣:

sudo apt-get install postgresql
sudo apt-get install libpq-dev

然后

gem install pg

然后

bundle install

src:http://wikimatze.de/installing-postgresql-gem-under-ubuntu-and-mac

答案 1 :(得分:78)

如错误日志中所述,您需要传递pg_config的路径。尝试使用以下方法安装gem:

gem install pg -- --with-pg-config= 'PATH_TO_YOUR_PG_CONFIG'

如果您不确定pg_config的位置,并假设您使用的是Linux或Mac,则可以运行以下命令:

which pg_config

您的pg-config可以位于不同的位置,具体取决于您安装postgres的方式。

答案 2 :(得分:56)

如果您使用的是Postgress.app,那么您需要访问其command-line tools。在终端或PATH profile

中输入以下行
 PATH="/Applications/Postgres.app/Contents/MacOS/bin:$PATH"

gem install pg现在应该可行了。 (这对我有用。)

注意新版本路径如下:

/Applications/Postgres.app/Contents/Versions/<version>/bin

答案 3 :(得分:21)

如果你有自制软件,只需输入:

  

$ brew install postgresql

如果您没有,请在终端下载:

  

ruby​​ -e“$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)”

答案 4 :(得分:12)

搜索libpq:

brew search libpq

应输出libpqxx

然后尝试安装它:

brew install libpqxx

答案 5 :(得分:7)

您只需要安装libpq-dev

sudo apt-get install libpq-dev

然后宝石应该安装得很好。

答案 6 :(得分:7)

按照安装后的说明进行操作:http://postgresapp.com/documentation/configuration-ruby.html

要安装pg gem,请确保已正确设置$ PATH(如http://postgresapp.com/documentation/cli-tools.html中所述),然后运行

sudo ARCHFLAGS="-arch x86_64" gem install pg

强烈建议阅读这两个页面。只是撇去他们,失去了我生命中的一小时。阅读它们,问题解决了。

答案 7 :(得分:3)

我遇到的问题是由于某种原因,它试图用/usr/bin/gcc-4.2进行编译。我发现通过更改mkmf.rb中的try_cpp(我在堆栈跟踪中看到)来使用失败的编译行引发异常。

我将gcc软链接到gcc-4.2并且工作正常:

sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2

为什么要尝试使用gcc-4.2?不知道。

实际编译行:

/usr/bin/gcc-4.2 -E -I/Users/dfrankow/.rvm/rubies/ruby-1.9.3-p125/include/ruby-1.9.1/x86_64-darwin11.4.0 -I/Users/dfrankow/.rvm/rubies/ruby-1.9.3-p125/include/ruby-1.9.1/ruby/backward -I/Users/dfrankow/.rvm/rubies/ruby-1.9.3-p125/include/ruby-1.9.1 -I. -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE   -I/usr/local/Cellar/postgresql/9.1.4/include  -O3 -ggdb -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wshorten-64-to-32 -Wimplicit-function-declaration  -fno-common -pipe  conftest.c -o conftest.i (RuntimeError)

答案 8 :(得分:3)

我遇到了同样的问题,但我的Postgres安装在

/Library/PostgreSQL/9.3

通过添加:

更新了〜/ .bash_profile
export PATH=/Library/PostgreSQL/9.3/bin:$PATH

打开一个新终端,运行bundle update,它也适用于我。谢谢Ari。

答案 9 :(得分:1)

您可以设置构建配置选项bundle config build.pg --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config并使用gem install pg而不使用任何选项(pg_config的路径可能与您不同,这一个适用于Postgresapp 9.3.5.0)

答案 10 :(得分:1)

我的openSUSE13.1 KDE计算机遇到了同样的问题。在我遇到该问题之前,我使用postgresql命令仅安装了包postgresql-serverzypper。然后又安装了2个包:

[arup@to_do_app]$ sudo zypper in postgresql-devel postgresql-contrib
root's password:
Loading repository data...
Reading installed packages...
Resolving package dependencies...
#....

然后,我再次跑bundle install,成功!!!

答案 11 :(得分:0)

对于有效的OSX解决方案,请参阅本指南http://krugerdavid.com/journal/how-to-install-xcode-homebrew-git-rvm-postgresql-ruby-1-9-3-on-snow-leopard/

它将引导您使用自制软件安装PostgreSQL。在OSX 10.8.3,PostgreSQL 9.2.3和Ruby 2.0.0-p0上为我测试和工作

答案 12 :(得分:0)

首先,卸载所有Homebrew版本。 --force选项使其卸载所有版本。

brew rm postgresql --force

相应地更改您的版本路径。

sudo /sbin/SystemStarter stop postgresql-8.4
sudo rm -rf /Applications/PostgreSQL\ 8.4
sudo rm -rf /etc/postgres-reg.ini
sudo rm -rf /Library/StartupItems/postgresql-8.4
sudo rm -rf /Library/PostgreSQL/8.4
sudo dscl . delete /users/postgres

编辑/ etc / profile并删除任何引用“postgres”的行。

nano /etc/profile

安装PostgresSQL

brew update
brew install postgresql

安装de PG GEM

gem install pg

就是这样。问候。

答案 13 :(得分:0)

您可以先查看lib文件,查看终端中是否有postrgresql文件。转cd~ / opt / local / lib /然后输入ls和enter按钮。这将显示lib目录中所有文件的列表。

1.如果你没有postreseql,你可以通过macports下载。 sudo port install postgresql93 @ 9.3.2_1

现在回到你试图捆绑安装的文件夹

  1. 让你的pg使用你刚刚下载的postgesql文件 gem install pg - --with-pg-config = / opt / local / lib / postgresql93 / bin / pg_config
  2. 现在运行 捆绑安装

答案 14 :(得分:0)

关于Fedora:

dnf install postgresql-devel

答案 15 :(得分:0)

在El Capitan上对我有用的是将ruby从系统默认值升级到2.3.1似乎找到了pg gem所需的正确库。