Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@nativestranger
Copy link

nativestranger commented May 17, 2021

+1

@isheebo
Copy link

isheebo commented Aug 2, 2021

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.

@holstvoogd
Copy link

holstvoogd commented Sep 2, 2021

You can also symlink pg_config into a bin dir so it can be found:

ln -s /opt/homebrew/Cellar/libpq/13.4/bin/pg_config /opt/homebrew/bin

This my be handy when using bundler with multiple projects and specific versions of PG or whatever

@oamado
Copy link

oamado commented Oct 5, 2021

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

@MrHarigo
Copy link

MrHarigo commented Feb 1, 2022

@holstvoogd for apple silicon this worked 👍

@windix
Copy link

windix commented Apr 17, 2022

To use with bundler / apple silicon:

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

@m21e-de
Copy link

m21e-de commented Apr 28, 2022

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

Thank you that worked

@avosa
Copy link

avosa commented Jun 7, 2022

Worked like a charm!

@cbschuld
Copy link

cbschuld commented Jul 2, 2022

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

@andey
Copy link

andey commented Jul 2, 2022

You can also symlink pg_config into a bin dir so it can be found:

ln -s /opt/homebrew/Cellar/libpq/13.4/bin/pg_config /opt/homebrew/bin

This my be handy when using bundler with multiple projects and specific versions of PG or whatever

For people in this thread, keep in mind that the version is hardcoded in the system link. So at the time of writing this the version was actually 14.4

@chockenberry
Copy link

chockenberry commented Jul 29, 2022

💯

@damuz91
Copy link

damuz91 commented Sep 7, 2022

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

I confirm this made it to work, Apple M1 Max , thank you!

@EraliaKats
Copy link

EraliaKats commented Sep 14, 2022

gem install pg didn't work before I installed postgresql -> brew install postgresql added all the necessary headers for me

@Bartuz
Copy link

Bartuz commented Sep 21, 2022

For intel mac:

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

@darrenterhune
Copy link

darrenterhune commented Sep 29, 2022

Yo pg_config should be on your machine after a brew install postgresql so no need to do libpq or gem install dances

sudo find / -name "pg_config" -print
bundle config set build.pg --with-pg-config=/path/to/pg_config

Also symlink is not ideal but will work

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