Rails 4.2.0: 无法在 Ubuntu 14.04 上安装 pg gem

14

最近我尝试将我的rails 3.2*应用程序升级到rails 4.2.*。但是,在安装'pg' gem时遇到了问题。当我搜索解决方法时,大多数都与OSX有关。但是我使用的是ubuntu 14.04。请提供以下问题的建议。

我已经在我的机器上安装了postgresql 9.3.5版本。

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /usr/local/rvm/rubies/ruby-2.1.2/bin/ruby extconf.rb 
checking for pg_config... yes
Using config values from /usr/bin/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=/usr/local/rvm/rubies/ruby-2.1.2/bin/ruby
    --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/rvm/gems/ruby-2.1.2@r4/gems/pg-0.18.1 for inspection.
Results logged to /usr/local/rvm/gems/ruby-2.1.2@r4/extensions/x86_64-linux/2.1.0/pg-0.18.1/gem_make.out
An error occurred while installing pg (0.18.1), and Bundler cannot continue.
Make sure that `gem install pg -v '0.18.1'` succeeds before bundling.

@mr-black,请您检查我的答案并将其确认为正确答案。 - Ryan Rauh
在Ubuntu 22.04(Windows 10 WSL2)中,这个答案对我有用。 - Nelson Jerez Vidal
3个回答

32

您需要安装具有PostgreSQL头文件的PostgreSQL开发软件包

sudo apt-get install libpq-dev

你也可以尝试

sudo apt-get install postgresql-client
sudo apt-get install postgresql postgresql-contrib

1
我已经安装了那些依赖包。但是,我又失败了。 - Mr. Black
1
你看过这个网址吗 https://dev59.com/FXA75IYBdhLWcg3wxcHV - Hardik Hardiya
sudo apt-get install postgresql postgresql-contrib,可能适用于您。 - Hardik Hardiya
是的,我已经查看了给定链接和在 SF 中的其他链接。但这些对我毫无帮助。 - Mr. Black
第一个例子在我们的Ubuntu 14.04服务器上非常好用,谢谢。 - aross

11

在尝试使用PostgreSQL 9.4在Ubuntu 14.04上安装pg-0.18.1时,遇到了完全相同的问题。

sudo apt-get install libpq-dev build-essential postgresql-server-dev-9.4

基本上,我缺少 postgresql-server-dev-${version}

在你的情况下,请尝试

sudo apt-get install libpq-dev build-essential postgresql-server-dev-9.3

3
谢谢!顺便说一下,sudo apt-get install libpq-dev 就足够了。 - mondayguy
我已经苦苦挣扎了几天,直到安装了“build-essential”软件包(对于“gem pg 0.21”)才解决了问题。我在其他地方没有看到过这个,所以谢谢! - kevlarr
2
尝试使用apt-get install libpq-dev gcc make而不是完整的build-essential,也可以工作。 - kevlarr

0

在Ubuntu 16.04上执行以下命令

sudo apt install libpq-dev postgresql-server
sudo -u postgres createuser -s $(whoami); createdb $(whoami)
gem install pg

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