Skip to content

Instantly share code, notes, and snippets.

@tomholford
Last active April 18, 2024 20:37
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save tomholford/f38b85e2f06b3ddb9b4593e841c77c9e to your computer and use it in GitHub Desktop.
Save tomholford/f38b85e2f06b3ddb9b4593e841c77c9e to your computer and use it in GitHub Desktop.
Install postgresql gem `pg` on macOS

Installing pg gem on macOS

If you're trying to install the postgresql gem pg and it is failing with the following error message:

Installing pg 1.2.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: ~/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/pg-1.2.3/ext
~/.rbenv/versions/3.0.0/bin/ruby -I ~/.rbenv/versions/3.0.0/lib/ruby/3.0.0 -r ./siteconf20210125-97201-pycpo.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

The following helped me resolve the issue without having to install the entire postgresql as recommended in the stop Stack Overflow post (already using it in a Docker container):

  1. brew install libpq
  2. gem install pg -- --with-pg-config=/usr/local/opt/libpq/bin/pg_config
@hueyAtFetchly
Copy link

bundle config build.pg --with-pg-config="$(brew --prefix)/opt/libpq/bin/pg_config"

Amazing, many other solutions didn't work (from fresh Ventura OS install).

@mikekavouras
Copy link

On Apple Silicon, after installing the libpq, running gem install pg -- --with-pg-config=/opt/homebrew/opt/libpq/bin/pg_config

should do the trick.

@isheebo you just saved me 10000 hours I owe you everything

@alexbrahastoll
Copy link

alexbrahastoll commented Jul 19, 2023

What worked for me was the suggestion by @oamado:

gem install pg -v '0.18.4' -- --with-cflags="-Wno-error=implicit-function-declaration"

I am on an older Mac (MacBook Pro 2018) running macOS 12.5.1 (Monterey).

@StoreCarCharIsInNoSave
Copy link

StoreCarCharIsInNoSave commented Aug 3, 2023

To use with bundler / apple silicon:

brew install libpq
bundle config build.pg --with-pg-config=/opt/homebrew/opt/libpq/bin/pg_config
bundle

This worked for m1, Postgres.app. Thank you
But after that, psql may not work. The following helped me:
brew link --force libpq

@raaynaldo
Copy link

gem install pg -- --with-pg-config=/opt/homebrew/opt/libpq/bin/pg_config

Works for me. Thanks!

@alexandrule
Copy link

To use with bundler / apple silicon:

brew install libpq
bundle config build.pg --with-pg-config=/opt/homebrew/opt/libpq/bin/pg_config
bundle

Thanks! It works!

@fabianoalmeida
Copy link

To use with bundler / apple silicon:

brew install libpq
bundle config build.pg --with-pg-config=/opt/homebrew/opt/libpq/bin/pg_config
bundle

Worked for me. I'm using Apple M1 Sonoma.

@hooopo
Copy link

hooopo commented Jan 23, 2024

gem install pg -- --with-pg-config=/opt/homebrew/opt/libpq/bin/pg_config

You may need to do a user-level install depending on permissions.

 gem install pg --user-install -- --with-pg-config=/opt/homebrew/opt/libpq/bin/pg_config

works for me.

@Rajat16nov
Copy link

Switching the ruby version worked for me
rbenv global 3.2.2
sudo chown -R $(whoami) ~/.rbenv
gem install pg

@antonengelhardt
Copy link

Merci!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment