[ruby-on-rails] Installing PG gem on OS X - failure to build native extension

It seems many others have had problems installing the pg gem. None of the solutions posed for others have worked for me.

I have tried to install the pg gem and postgres.app. The pg gem won't install. The first error I get is:

An error occurred while installing pg (0.17.0), and Bundler cannot continue. Make sure that gem install pg -v '0.17.0' succeeds before bundling.

The installation advice about pointing my gem install to the config for pg fails with the following error message (which many others on this forum have encountered):

Failed to build native extensions... Results logged to /Users/melanie/.rvm/gems/ruby-1.9.3-p448/gems/pg-0.17.0/ext/gem_make.out

I don't know how to find or access this log file to search for further clues.

I also get an error message (command not found) when I try using the sudo apt-get install command. I've scoured this forum for the last 6 hours, trying each piece of advice to get pg working with my rails project.

I can't find advice about how to change a path, or specifically, what change is required. My which pg_config returns a file source. I've used that with a command to install pg using that config. It fails.

There are so many people that have had trouble with this. Many answers suggest homebrew. I've had to remove that because it threw up other issues.

This question is related to ruby-on-rails macos postgresql gem

The answer is


If you are using Ubuntu try to install following lib file

sudo apt-get install libpq-dev

and then

gem install pg

worked for me.


For those who are not interested to use brew.

  1. Download PostgreSQL application.
  2. Follow the macOS default instruction to install it.
  3. It is advisable to run PostgreSQl.
  4. Run gem install pg -- --with-pg-config=/path/to/postgress/in/your/applications/folder/`
    • For example, in my machine it is /Applications/Postgres.app/Contents/Versions/12/bin/pg_config

I am using OS X Mavericks (version 10.9)

and when I run the above I got the following message: If builds of PostgreSQL 9 are failing and you have version 8.x installed.

So I run the following command:

ARCHFLAGS="-arch x86_64" gem install pg

and this worked for me, I hope this helps someone :)


running brew update and then brew install postgresql worked for me, I was able to run the pg gem file no problem after that.


I tried everything for hours but the following finally fixed it (I'm on OS X 10.9.4):

  1. Install Xcode command line tools (Apple Developer site)
  2. brew uninstall postgresql
  3. brew install postgresql
  4. ARCHFLAGS="-arch x86_64" gem install pg

I spent a day on this and here's how I got it fixed:

I found that global value of build.pg was set to: /opt/local/lib/postgresql91/bin/pg_config and that was not where postgres was installed.

I fixed it with replacing the value of build.pg to: bundle config build.pg --with-pg-config=/usr/local/Cellar/postgresql/9.4.4/bin/pg_config which is where my postgresql installation is.


i got same problem and i solved

gem update --system 3.0.6

easy step

  1. brew install postgresql
  2. gem install pg -v 'your version'

Ok I also had this problem (psql is v 9.3.0 and ruby is v 2.1.2) and the solution that worked for me was setting the bundle config settings first:

bundle config build.pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config

This answer finally helped me figure it out: https://stackoverflow.com/a/9235107/3546680


Solved! I found some lack of library for PostgreSQL on the system. Only two steps solved it:

brew install postgresql

Then run

gem install pg


I believe the “correct” answer would be to first configure PATH correctly for Postgres.app by adding the following to ~/.profile (.zshrc or ~/.zprofile if using ZSH):

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin

Then open a new tab or window in terminal and install the pg gem with:

ARCHFLAGS="-arch x86_64" gem install pg

Documented here:


On OSX with Postgres installed in /Applications, I simply run the following command (change 0.20 & 9.4 according to your version)

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

You should have :

Building native extensions with: '--with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config' This could take a while... Successfully installed pg-0.20.


Try:

gem install pg -- --with-pg-config=`which pg_config`

Similarly, after installing Mavericks bundle update was throwing an error on the pg gem, which is only used on production and not locally.

I use Brew to manage my packages and postgresql was already installed, but still I was getting the 'no pg_config' error.

The fix was to just brew uninstall postgresql, then brew install postgresql. After which I was immediately able to successfully run bundle update.


Examples related to ruby-on-rails

Embed ruby within URL : Middleman Blog Titlecase all entries into a form_for text field Where do I put a single filter that filters methods in two controllers in Rails Empty brackets '[]' appearing when using .where How to integrate Dart into a Rails app Rails 2.3.4 Persisting Model on Validation Failure How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? Rails: Can't verify CSRF token authenticity when making a POST request Uncaught ReferenceError: React is not defined

Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"

Examples related to postgresql

Subtracting 1 day from a timestamp date pgadmin4 : postgresql application server could not be contacted. Psql could not connect to server: No such file or directory, 5432 error? How to persist data in a dockerized postgres database using volumes input file appears to be a text format dump. Please use psql Postgres: check if array field contains value? Add timestamp column with default NOW() for new rows only Can't connect to Postgresql on port 5432 How to insert current datetime in postgresql insert query Connecting to Postgresql in a docker container from outside

Examples related to gem

Embed ruby within URL : Middleman Blog Why won't bundler install JSON gem? Installing PG gem on OS X - failure to build native extension How to install a specific version of a ruby gem? Unable to install gem - Failed to build gem native extension - cannot load such file -- mkmf (LoadError) How to upgrade rubygems "Could not find a valid gem in any repository" (rubygame and others) Pull is not possible because you have unmerged files, git stash doesn't work. Don't want to commit Uninstall old versions of Ruby gems Could not locate Gemfile