PG gem 在 Rails 应用中无法安装:Gem::Ext::BuildError: 错误:未能构建本地扩展的宝石。

7

我正在尝试将我的Rails应用部署到Heroku,但当我在gemfile中添加pg宝石并运行bundle install时,我会遇到一个错误:

An error occurred while installing pg (1.1.3), and Bundler cannot continue.
Make sure that `gem install pg -v '1.1.3'` succeeds before bundling.

我的Gemfile文件底部如下:
group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
  # Use sqlite3 as the database for Active Record
  gem 'sqlite3'
end

group :production do
  gem 'pg'
end

当我运行gem install pg时,会收到以下信息:

ERROR:  Error installing pg:
        ERROR: Failed to build gem native extension.

    current directory: /home/scott_imunro/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/pg-1.1.3/ext
/home/scott_imunro/.rbenv/versions/2.4.2/bin/ruby -r ./siteconf20181115-1119-1gb3plu.rb 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/scott_imunro/.rbenv/versions/2.4.2/bin/$(RUBY_BASE_NAME)
        --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

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /home/scott_imunro/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/extensions/x86_64-linux/2.4.0-static/pg-1.1.3/mkmf.log

extconf failed, exit code 1

我正在Windows 10上运行一个Ubuntu bash,并且在使用Rails 5。有什么解决这个错误的方法吗?

3个回答

25

我也遇到了同样的问题,原因是您的Linux安装需要安装PostgreSQL的开发库才能构建宝石。

以下是我解决问题的方法:

打开终端并运行以下命令

sudo apt-get install libpq-dev

然后尝试使用以下命令重新安装PostgreSQL gem:

gem install pg

就是这些了。

希望这有所帮助。


5
这很可能是由于缺少依赖项导致的。在终端中手动运行以下命令gem install pg -v '1.1.3',查看输出结果。你很可能缺少该gem所需的前置库/头文件。请尝试:
sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev postgresql-client-common postgresql-client libpq-dev

建议阅读这篇文章:https://medium.com/@colinrubbert/installing-ruby-on-rails-in-windows-10-w-bash-postgresql-e48e55954fbf

该文章介绍了如何在Windows 10上使用Bash和PostgreSQL安装Ruby on Rails。

运行两个sudo命令,然后执行gem install命令就可以了。非常感谢! - user10410465

0

答案就在你得到的反馈中:

没有pg_config...尝试安装。如果构建失败,请使用--with-pg-config=/path/to/pg_config再次尝试

这正是你需要做的。

pg_config位于与postgrespsql相同的目录中,因此使用which postgreswhich psql来查找它。

然后运行(以下是我的系统路径示例 - 用你找到的路径替换它):

gem install pg -- --with-pg-config=/usr/local/opt/postgresql@11/bin/pg_config


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