Rails安装pg - 找不到'libpq-fe.h'头文件

125
$ sudo bundle install

结果

Fetching gem metadata from https://rubygems.org/...........
Fetching gem metadata from https://rubygems.org/..
Using rake (0.9.2.2) 
Using i18n (0.6.1) 
Using multi_json (1.3.6) 
Using activesupport (3.2.8) 
Using builder (3.0.4) 
Using activemodel (3.2.8) 
Using erubis (2.7.0) 
Using journey (1.0.4) 
Using rack (1.4.1) 
Using rack-cache (1.2) 
Using rack-test (0.6.2) 
Using hike (1.2.1) 
Using tilt (1.3.3) 
Using sprockets (2.1.3) 
Using actionpack (3.2.8) 
Using mime-types (1.19) 
Using polyglot (0.3.3) 
Using treetop (1.4.11) 
Using mail (2.4.4) 
Using actionmailer (3.2.8) 
Using arel (3.0.2) 
Using tzinfo (0.3.33) 
Using activerecord (3.2.8) 
Using activeresource (3.2.8) 
Using bundler (1.2.1) 
Using coffee-script-source (1.4.0) 
Using execjs (1.4.0) 
Using coffee-script (2.2.0) 
Using rack-ssl (1.3.2) 
Using json (1.7.5) 
Using rdoc (3.12) 
Using thor (0.16.0) 
Using railties (3.2.8) 
Using coffee-rails (3.2.2) 
Using jquery-rails (2.1.3) 
Installing pg (0.14.1) with native extensions 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.8 extconf.rb 
checking for pg_config... yes
Using config values from /usr/bin/pg_config
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
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/bin/ruby1.8
    --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 /var/lib/gems/1.8/gems/pg-0.14.1 for inspection.
Results logged to /var/lib/gems/1.8/gems/pg-0.14.1/ext/gem_make.out
An error occurred while installing pg (0.14.1), and Bundler cannot continue.
Make sure that `gem install pg -v '0.14.1'` succeeds before bundling.

我正在执行$ gem install pg -v '0.14.1',但这并没有帮助到我。

我的Gemfile文件如下:

source 'https://rubygems.org'

#gem 'rails', '3.0.9'

#gem 'sqlite3', '1.3.6', :group => :development


gem 'rails', '3.2.8'

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


gem 'sqlite3'
gem 'pg'
gem 'taps'

gem 'json'

# 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
#group :development do
  #gem 'sqlite3'
#end

gem_make.out

/usr/bin/ruby1.8 extconf.rb 
checking for pg_config... yes
Using config values from /usr/bin/pg_config
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
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/bin/ruby1.8
    --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

mkmf.log

find_executable: checking for pg_config... -------------------- yes

--------------------

find_header: checking for libpq-fe.h... -------------------- no

"gcc -E -I. -I/usr/lib/ruby/1.8/i686-linux -I. -D_FORTIFY_SOURCE=2 -D_FILE_OFFSET_BITS=64 -D_FORTIFY_SOURCE=2 -I -fno-strict-aliasing -g -g -O2  -fPIC    conftest.c -o conftest.i"
conftest.c:1:22: fatal error: libpq-fe.h: No such file or directory
compilation terminated.
checked program was:
/* begin */
1: #include <libpq-fe.h>
/* end */

--------------------

请帮助我,谢谢。

很抱歉这段文字。 您的帖子没有足够的上下文来解释代码部分,请更清楚地解释您的情况。


1
你是如何安装PostgreSQL的? - Stefan
1
可能是重复的问题:找不到PostgreSQL客户端库(libpq) - skolima
2
可能是重复的问题:尝试安装pg gem时找不到'libpq-fe.h'头文件 - iconoclast
16个回答

256

只需制作此内容

$ sudo apt-get install libpq-dev

1
我尝试在Ubuntu 14.04上做这件事,但是它给了我这个错误信息:"libpq-dev: 依赖于 libpq5 (= 9.3.4-1),但是要安装的版本是 9.3.10-0ubuntu0.14.04"。 - Michael Lafayette
但是当我尝试安装libpq5时,它会显示“libpq5已经是最新版本”。 - Michael Lafayette
1
太棒了!我正在编译的应用程序还需要明确指定到 libpq-fe.h 的路径,使用 --with-cflags=-I/usr/include/postgresql,以防其他人遇到相同的问题。 - Ben Johnson
对于那些好奇 libpq-dev 是什么意思的人:它包含了构建第三方应用程序所需的 PostgreSQL_ 二进制文件和头文件的最小集合。 - IronMan007
2
这是最终对我起作用的方法,或者至少在 Mac 版本上是这样的,即 brew install libpq。然后我还需要执行 brew link --force libpq 才能成功完成 gem install pg - Scott Schupbach
显示剩余3条评论

91
这个问题在无法找到'libpq-fe.h'头文件安装pg gem时已经得到了解答。
根据你的环境不同,可以采取以下措施:
  • Mac: 运行brew install postgresql
  • Ubuntu: 运行sudo apt-get install libpq-dev
  • RHEL: 运行yum install postgresql-devel
  • Alpine: 运行apk add postgresql-dev
然后再次运行gem install pg

2
我尝试在Ubuntu 14.04上执行此操作,但出现以下错误:"libpq-dev: 依赖于 libpq5 (= 9.3.4-1),但将安装 9.3.10-0ubuntu0.14.04"。 - Michael Lafayette
但是当我尝试安装libpq5时,它会显示“libpq5已经是最新版本了”。PostgreSQL也是一样。 - Michael Lafayette
PostgreSQL的版本是:psql --version: psql (PostgreSQL) 9.3.10。PostgreSQL 9.3.10运行在x86_64-unknown-linux-gnu上,由gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2编译,为64位系统。 - Michael Lafayette
嘿,Michael,我不确定我能回答你的问题,因为我不熟悉Ubuntu,但你可能想看看Michael Durrant在这里的回答:https://dev59.com/EG025IYBdhLWcg3wYE-G#8887801 - Phil
我曾在Red Hat EL7上工作。在安装DBD::Pg的Perl过程中,我遇到了缺少头文件的问题。 - Marcus
显示剩余4条评论

21
  • SO:Ubuntu
  • 编程语言:C

$ sudo apt-get install libpq-dev

在我的情况下,我将目录路径写错了。所以:

从:gcc -o lib_version lib_version.c -I/usr/include/postgresql -lpq -std=c99

变成:gcc -o lib_version lib_version.c -I /usr/include/postgresql -lpq -std=c99

GL


这个对我在PostgreSQL Kali安装中起作用了。 - Raqun Bob

19
尝试这个:
  1. 安装postgresql:brew install postgresql
  2. 安装pg gem:gem install pg
  3. 执行bundle install

17

在 Fedora/RHEL 系统上,以下操作可以解决问题:

sudo yum install libpqxx-devel

为什么会显示“没有可用的libpqxx-devel软件包”。 - itsji10dra
1
yum list libpq*,然后执行 yum install libpqxx-devel.x86_64。 - erichfw

10

Alpine Linux上,您可以通过以下方式添加libpq-fe.h

apk add postgresql-dev

8

针对亚马逊 Linux (AWS):

sudo yum install postgresql-devel

然后再次运行您的命令。


5
在Mac OS X上执行如下操作:
gem install pg -- --with-pg-config=***/path/to/pg_config***

***/path/to/pg_config*** 是 pg_config 的路径


1
我们如何找到 pg_config 的路径?执行 where pg_config 命令返回 pg_conf 未找到。 - Masroor

4
我在亚马逊AMI上的Jenkins服务器遇到了这个问题。
$ pg_config | grep VERSION
VERSION = PostgreSQL 9.4.9

然后根据您需要的版本安装所需的库。

$ sudo yum list postgresql* | grep devel
postgresql92-devel.x86_64               9.2.18-1.59.amzn1          @amzn-main
postgresql94-devel.x86_64               9.4.9-1.67.amzn1           @amzn-main
postgresql8-devel.x86_64                8.4.20-5.52.amzn1          amzn-main
postgresql93-devel.x86_64               9.3.14-1.62.amzn1          amzn-main
postgresql95-devel.x86_64               9.5.4-1.71.amzn1           amzn-main

然后,您只需安装相应的版本,对于我来说是版本9.4:

sudo yum install postgresql94-devel

1
啊!太好了。具体版本(9.6)也是我在AMI上的障碍。 - DaveGauer

3

在 Mac 上,请确保已经链接了您的 Postgres 数据库。您可以按照以下步骤进行操作:

brew link --overwrite postgresql

这对我解决了问题。

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