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?

Shayan Shafiq
  • 1,447
  • 5
  • 18
  • 25
YankeeJohn
  • 405
  • 1
  • 4
  • 4

10 Answers10

103

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

César Faria
  • 1,031
  • 1
  • 6
  • 5
57

if you're using OSX, you could try running

brew install postgresql

and then installing the gem

Elena
  • 1,719
  • 19
  • 24
21

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

The Whiz of Oz
  • 6,763
  • 9
  • 48
  • 85
luigi7up
  • 5,779
  • 2
  • 48
  • 58
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.

Shayan Shafiq
  • 1,447
  • 5
  • 18
  • 25
Jean Suarez
  • 126
  • 1
  • 3
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
Ahmed Gasanov
  • 139
  • 1
  • 6
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.

uday
  • 1,421
  • 10
  • 19
  • 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 '18 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 '18 at 04: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) – Glenn 'devalias' Grant Oct 09 '19 at 03: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
Esoul
  • 41
  • 2
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

Gil Perez
  • 853
  • 10
  • 13
1

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
Dozon Higgs
  • 473
  • 2
  • 5
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!

Roman Pushkin
  • 5,639
  • 3
  • 40
  • 58