13
votes

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?

7
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
make sure you installed postresql on your machine - Martin Zinovsky
What OS are you using? If you are using a mac then you need to update XCode probably. - Verty00
You may also need to stop and restart Postgres, or uninstall and reinstall it - John Skiles Skinner
I am using Rails 5.1.4 - YankeeJohn

7 Answers

51
votes

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

28
votes

if you're using OSX, you could try running

brew install postgresql

and then installing the gem

7
votes

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.

2
votes

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.

1
votes

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

1
votes

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 Posgres 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/Posgres.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

0
votes

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