PG gem won't install in Rails app: Gem::Ext::BuildError: ERROR: Failed to build gem native extension


New Member
I'm trying to deploy my Rails app to Heroku and when I add the pg gem to my gemfile and run bundle install I get an error:
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.
The bottom of my gemfile looks like this:
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:
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
  # Use sqlite3 as the database for Active Record
  gem 'sqlite3'

group :production do
  gem 'pg'
When I run gem install pg I get the following:
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
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:

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


extconf failed, exit code 1
I'm running Rails 5 with an Ubuntu bash for Windows 10. Any ideas how to resolve this error?


Thành viên BQT
This will likely be due to missing dependencies. Run the command manually in the terminal gem install pg -v '1.1.3' see what output you get.

You're likely missing the pre-requisite libs/headers for the gem. Try:

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
Might be worth having a look over this article: