Ruby-on-rails 尝试安装 pg gem 时找不到“libpq-fe.h”头文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6040583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:10:28  来源:igfitidea点击:

Can't find the 'libpq-fe.h header when trying to install pg gem

ruby-on-railsruby-on-rails-3postgresqlrails-postgresqlpg

提问by demonchand

I am using the Ruby on Rails 3.1 pre version. I like to use PostgreSQL, but the problem is installing the pggem. It gives me the following error:

我正在使用 Ruby on Rails 3.1 预版本。我喜欢使用 PostgreSQL,但问题是安装pggem。它给了我以下错误:

$ gem install pg
Building native extensions.  This could take a while...
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

        /home/u/.rvm/rubies/ruby-1.9.2-p0/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=/home/u/.rvm/rubies/ruby-1.9.2-p0/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 /home/u/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.11.0 for inspection.
Results logged to /home/u/.rvm/gems/ruby-1.9.2-p0/gems/pg-0.11.0/ext/gem_make.out

How do I solve this problem?

我该如何解决这个问题?

回答by mu is too short

It looks like in Ubuntu that header is part of the libpq-devpackage(at least in the following Ubuntu versions: 11.04(Natty Narwhal), 10.04(Lucid Lynx), 11.10(Oneiric Ocelot), 12.04(Precise Pangolin), 14.04(Trusty Tahr) and 18.04(Bionic Beaver)):

看起来在 Ubuntu 中,标头是libpq-dev包的一部分(至少在以下 Ubuntu 版本中: 11.04(Natty Narwhal)、10.04(Lucid Lynx)、11.10(Oneiric Ocelot)、12.04(Precise Pangolin)、14.04(Trusty Tahr)和18.04(仿生海狸)):

...
/usr/include/postgresql/libpq-fe.h
...

So try installing libpq-devor its equivalent for your OS:

因此,请尝试libpq-dev为您的操作系统安装或等效:

  • For Ubuntu/Debian systems: sudo apt-get install libpq-dev
  • On Red Hat Linux(RHEL) systems: yum install postgresql-devel
  • For Mac Homebrew: brew install postgresql
  • For Mac MacPortsPostgreSQL: gem install pg -- --with-pg-config=/opt/local/lib/postgresql[version number]/bin/pg_config
  • For OpenSuse: zypper in postgresql-devel
  • For ArchLinux: pacman -S postgresql-libs
  • 对于 Ubuntu/Debian 系统: sudo apt-get install libpq-dev
  • Red Hat Linux(RHEL) 系统上:yum install postgresql-devel
  • 对于 Mac自制软件brew install postgresql
  • 对于 Mac MacPortsPostgreSQL:gem install pg -- --with-pg-config=/opt/local/lib/postgresql[version number]/bin/pg_config
  • 对于OpenSusezypper in postgresql-devel
  • 对于ArchLinuxpacman -S postgresql-libs

回答by iwasrobbed

On macOS(previously Mac OS Xand OS X), use Homebrewto install the proper headers:

macOS(以前是Mac OS XOS X)上,使用Homebrew安装正确的头文件:

brew install postgresql

and then running

然后运行

gem install pg

should work.

应该管用。

Alternatively, instead of installing the whole postgresql, you can brew install libpqand export the correct PATHand PKG_CONFIG_PATHas explained in the 'Caveats' section

或者,postgresql您可以不安装整个 ,而是按照“注意事项”部分中的说明brew install libpq导出正确的PATHPKG_CONFIG_PATH

回答by Michael Durrant

I had also tried doing gem install libpq-dev, but I received this error:

我也试过做gem install libpq-dev,但我收到了这个错误:

Can't find the 'libpq-fe.h header
*** extconf.rb failed ***

However I found that installing with sudo apt-get(which I try to avoid using with Ruby on Rails) worked, i.e.

但是我发现安装sudo apt-get(我尽量避免与 Ruby on Rails 一起使用)有效,即

sudo apt-get install libpq-dev
# or
apt-get install postgres-server-dev-{pg.version}
# for postgresql 9.4 on Ubuntu 14.04

then I was able to do

然后我就可以做到

gem install pg

without issues.

没有问题。

回答by David Pelaez

I could solve this in another way. I didn't find the library on my system. Thus I installed it using an app from PostgreSQL main website. In my case (OS X) I found the file under /Library/PostgreSQL/9.1/include/once the installation was over. You may also have the file somewhere else depending on your system if you already have PostgreSQL installed.

我可以用另一种方式解决这个问题。我没有在我的系统上找到图书馆。因此,我使用 PostgreSQL 主网站上的应用程序安装了它。在我的情况下(OS X),我/Library/PostgreSQL/9.1/include/在安装结束后找到了该文件。如果您已经安装了 PostgreSQL,您可能还会在其他地方拥有该文件,具体取决于您的系统。

Thanks to this link on how to add an additional path for gem installation, I could point the gem to the lib with this command:

感谢这个关于如何为 gem 安装添加额外路径的链接,我可以使用以下命令将 gem 指向 lib:

export CONFIGURE_ARGS="with-pg-include=/Library/PostgreSQL/9.1/include/"
gem install pg

After that, it works, because it now knows where to find the missing library. Just replace the path with the right location for your libpq-fe.h

之后,它就可以工作了,因为它现在知道在哪里可以找到丢失的库。只需将路径替换为 libpq-fe.h 的正确位置

回答by lfender6445

Can't find the libpq-fe.h header

找不到 libpq-fe.h 标头

i had success on CentOS 7.0.1406running the following commands:

我在CentOS 7.0.1406上成功运行以下命令:

~ % psql --version # => psql (PostgreSQL) 9.4.1
yum install libpqxx-devel
gem install pg -- --with-pg-config=/usr/pgsql-9.4/bin/pg_config

Alternatively, you can configure bundler to always install pgwith these options (helpful for running bundler in deploy environments),

或者,您可以将 bundler 配置为始终pg使用这些选项进行安装(有助于在部署环境中运行 bundler),

  • bundle config build.pg --with-pg-config=/usr/pgsql-9.4/bin/pg_config
  • bundle config build.pg --with-pg-config=/usr/pgsql-9.4/bin/pg_config

回答by Arivarasan L

For CentOS6.4,

对于CentOS6.4,

yum install postgresql-devel
gem install pg

worked well!

工作得很好!

回答by pablomarti

Just for the record:

仅作记录:

Ruby on Rails 4 application in OS X with PostgresApp (in this case 0.17.1 version needed - kind of an old project):

带有 PostgresApp 的 OS X 中的 Ruby on Rails 4 应用程序(在这种情况下需要 0.17.1 版本 - 一种旧项目):

gem install pg -v '0.17.1' -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config

回答by Jiemurat

On Mac OS X run like this:

在 Mac OS X 上运行如下:

gem install pg -- --with-pg-config=***/path/to/pg_config***

***/path/to/pg_config***is path to pg_config

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

回答by Johann

In my case it was package postgresql-server-dev-8.4(I am on Ubuntu 11.04(Natty Narwhal), 64 bits).

就我而言,它是包postgresql-server-dev-8.4(我使用的是Ubuntu 11.04(Natty Narwhal),64 位)。

回答by jelder

The right answer for Mac users with Postgres.app is to build against the libpqprovided with that package. For example, with the 9.4 release (current as of this writing), all you need is:

使用 Postgres.app 的 Mac 用户的正确答案是根据libpq该软件包提供的内容进行构建。例如,对于 9.4 版本(撰写本文时的最新版本),您只需要:

export CONFIGURE_ARGS="with-pg-include=/Applications/Postgres.app/Contents/Versions/9.4/include"
gem install pg

This will keep your pggem in sync with exactly the version of PostgreSQL you have installed. Installing something from Homebrew is a waste in this case.

这将使您的pggem 与您安装的 PostgreSQL 版本完全同步。在这种情况下,从 Homebrew 安装一些东西是一种浪费。