39

I am still fairly new to Rails. I am trying to push to Heroku and I am getting errors.

The first error is when I run a Bundle Install I get this error message:

"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."

I have tried to run this command

gem install pg -v '1.1.3'

But it fails and gives me this error message:

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

Does anyone have a solution to this?

6
  • Which version of Rails are you using? As I recall, not all versions of the pg gem are compatible with all versions of Rails.
    – jvillian
    Sep 14, 2018 at 21:13
  • make sure you installed postresql on your machine
    – Martin
    Sep 14, 2018 at 21:14
  • What OS are you using? If you are using a mac then you need to update XCode probably.
    – Verty00
    Sep 14, 2018 at 21:30
  • You may also need to stop and restart Postgres, or uninstall and reinstall it Sep 14, 2018 at 21:57
  • I am using Rails 5.1.4
    – YankeeJohn
    Sep 14, 2018 at 21:58

10 Answers 10

103

I had the exact same problem, and solved it by running sudo apt install postgresql-contrib libpq-dev. Then bundle worked just fine.

5
  • 3
    This worked for me! I should note that I am using the Windows Subsystem for Linux (WSL) - so for anyone facing similar issues with WSL, this does help fix it! :) Oct 9, 2019 at 16:18
  • This works for me as well on Ubuntu 18.04 with Rails 6. Thanks a lot.
    – Fatima
    Jun 16, 2020 at 11:26
  • 2
    sudo apt install libpq-dev was sufficient for me Mar 21, 2022 at 13:49
  • 1
    This works fine on Ubuntu 20.04 too Apr 15, 2022 at 12:01
  • This fixed it for me too on Ubuntu 23.04 and Ruby 3.2 Jul 10 at 19:15
57

if you're using OSX, you could try running

brew install postgresql

and then installing the gem

0
22

TL;DR; If you installed the Postgres using the PostgresAPP instead of BREW then the problem might be that you don't have the Postgres bin folder in the $APTH

The solution is to find the Postgres app installation folder an in it the /bin folder.

In my case if I run which postgres doesn't work which means I don't have it in my $PATH, so what I did is I navigated to: cd /Applications/Postgres.app then to Contents and then to Versions until I found the latest folder:

/Applications/Postgres.app/Contents/Versions/latest/bin

Now, with this I can install the PG GEM by providing the path to the bin folder of my Postgres app installation

Finally, in the terminal in the root of my Rails project I ran where I provide the postgres config file path to the GEM installer:

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

1
  • 1
    thank you, this worked for me!
    – Dan
    Oct 19, 2021 at 19:34
11

I encountered the same problem. I am using Ubuntu. I installed Postgres using this Ubuntu guide. After installing Postgres, I ran this code sudo apt install postgresql-contrib libpq-dev then bundle install.

7

The accepted answer is not correct. If you installed postgresql-contrib and libpq-dev as mentioned in Cesar Faria answer, and you still getting the same error, most probable you missed the list of packages which are considered essential for building Debian packages. All that packages included in build-essential package. So, all you need to do to get rid of the error is the command below.

sudo apt install libpq-dev build-essential
0
4

try instaling with pg-config like this:
gem install pg -v 1.1.3 -- --with-pg-config=/usr/pgsql-9.X/bin/pg_config.

In pg-config path mention the posgtres version installed in you're system.

3
  • I have version 10.0, can I ask you one more question? The correct command for 10.0 would be gem install pg -v 1.1.3 -- --with-pg-config=/usr/pgsql-10.0.X/bin/pg_config ?
    – YankeeJohn
    Sep 17, 2018 at 17:36
  • gem install pg -v 1.1.3 -- --with-pg-config=/usr/pgsql-10.0/bin/pg_config without X
    – uday
    Sep 18, 2018 at 4:21
  • A variation that uses which to pass the path directly into the command: gem install pg -v 1.1.3 -- --with-pg-config=$(which pg_config) Note: you could also use backticks in place of the $(), but it's impossible hard to try and format a code block on SO with backticks (since the code marker uses backticks) Oct 9, 2019 at 3:03
4

Postgresql needs to be in local in order to install gem pg. Try this

brew install postgresql

and the re-install bundle

bundle install

or If you are using yum. Try this

sudo yum install postgresql-devel
3

for me (macOS ventura, ruby 3) I needed to add psql first.

I basically followed this https://stackoverflow.com/a/49689589/4883677

brew install libpq
brew link --force libpq
bundle install
2

The following is only for your local environment:

Note: Update the command below to match your version postgresql/13.2/...

> brew install postgres
> sudo gem install pg -v '1.1.4' --source 'https://rubygems.org/' -- --with-pg-config=/usr/local/Cellar/postgresql/13.2/bin/pg_config

Also, make sure to start your server > pg_ctl -D /usr/local/var/postgres start

0

Things have changed a little bit on MacOS. You want to:

brew formulae | grep postgresql@

Then pick a version from the list, and install it. For example:

brew install postgresql@12

Keep the note of installation instructions or open another tab in your terminal. There is a chance you gonna need them.

Then try to install this gem again:

bundle install pg

If this doesn't work, look at your post-install output from Homebrew. In my case I needed to do this:

echo 'export PATH="/opt/homebrew/opt/postgresql@12/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

(But I am using Zsh! If you're bash user, it's going to be different)

Now the command bundle install pg should work.

After that you can bundle install again.

And here is a hint from your fellow engineer. Did you know that if you run bundle install -j 32 things are going to be a little bit faster? ;) Try this out!

If you still have failures, check out that they're related to pg gem. I am sorry if this was too hard, it shouldn't be that hard working with Rails! I wish you a good day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.