ARM64 architecture (M1 chip): Cannot install pg gem (using PostgresApp)
Asked Answered
H

3

28

I've been trying to install a rails project on my computer (Macbook Pro 2020 with M1) running Big Sur.

I have the PostgresApp installed.

When running bundle install it fails to build the pg gem so I tried to install the gem manually (by doing gem install pg - tried also with gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/10/bin/pg_config).

I get an error saying:

ERROR:  Error installing pg:
ERROR: Failed to build gem native extension.

Can't find the PostgreSQL client library (libpq)
*** extconf.rb failed ***

When checking the error logs I see:

have_library: checking for PQconnectdb() in -lpq... -------------------- no

ld: warning: ignoring file /Applications/Postgres.app/Contents/Versions/13/lib/libpq.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
Undefined symbols for architecture arm64:
  "_PQconnectdb", referenced from:
      _t in conftest-db479f.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
checked program was:
/* begin */
 1: #include "ruby.h"
 2:
 3: #include <libpq-fe.h>
 4:
 5: /*top*/
 6: extern int t(void);
 7: int main(int argc, char **argv)
 8: {
 9:   if (argc > 1000000) {
10:     printf("%p", &t);
11:   }
12:
13:   return 0;
14: }
15: int t(void) { void ((*volatile p)()); p = (void ((*)()))PQconnectdb; return !p; }
/* end */

Any idea how to solve this?

Hoffert answered 20/1, 2021 at 13:11 Comment(2)
Do you need to use Postgres.app? I ran into the same issue. I deleted it and used a homebrew version of postgres. Works well.Charbonnier
@IngmarasKeleras yes, the native version has some collation issues in MacOS.Hoffert
H
93

For those who are just trying to install the pg gem and don't care about PostgresApp, the key to fixing pg on the M1 is ensuring the existence of the libpq. These steps allowed me to get the pg gem install on my M1 mac without resorting to using x86 version or build flags:

brew install libpq
export PATH="/opt/homebrew/opt/libpq/bin:$PATH"
gem install pg
Hippomenes answered 11/12, 2021 at 16:37 Comment(6)
This should be the accepted answer.Current
This worked for me! Agree it should be the accepted answer.Thayer
Thanks! Works for me on M1 proBertelli
This is not optimum as it skips Postgres.app, but going back to x86 homebrew seems insane to me. I don't see myself doing this without having to deal with other numerous compatibility issues. But I may be wrong. It feels extreme anyway.Abortifacient
Freaking genius.Oecology
This was very helpful, but homebrew has recently changed postgresql installation, using explicit version tags, so installing libpq with brew didn't help--- it didn't know where my "real" postgresql installation was. I had to find my real pg_config file and add the path to there: export PATH="/opt/homebrew/opt/postgresql@14/bin:$PATH". Then I had to specify pg version 1.4.3 in my Gemfile and bundle install.Undergraduate
W
11

I ran into the same problem with M1 + the ruby pg gem. The problem was that I had a mix of ARM + x86 binaries on my system, and pg apparently can only be compiled with x86 at present. As FYI, there are new issues reported in its github repo, so hopefully it'll be solved soon here

My work around:

  1. uninstall ARM based homebrew + rbenv and remove your .gem + .rbenv directories from your home dir (homebrew uninstall instructions)

  2. re-install homebrew as x86 intel-based

$ arch -x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
  1. install rbenv as x86 (or whatever your preferred ruby version manager)
$ arch -x86_64 brew install rbenv
$ arch -x86_64 rbenv install 2.7.2

3a) (optional) for postgres.app, you can pre-configure where pg-config lives so you don't have to run the manual gem install when it chokes. eg

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

3b) (optional) for brew, you can do the same:

$ bundle config build.pg --with-pg-config=$(brew --prefix)/opt/libpq/bin/pg_config
  1. re-bundle your project and enjoy ruby on rosetta 2 (for now)
$ bundle install
$ rails s
Wreckage answered 25/1, 2021 at 15:58 Comment(3)
I ended up installing the native postgres (didn't want ruby to run "emulated" for performance issues). I'll wait until postgres.app releases an ARM64 version :)Hoffert
@Hoffert I asked a question about this hoping that I didn't have to resort to install postgres using homebrew. I guess I will do the same. My current instance is on a 10-year-old High Sierra computer so I want to move to it to my M1 computer.Hedda
Postgres.app now released a beta arm version available here: github.com/PostgresApp/PostgresApp/releases/tag/v2.5beta1, the pg gem now installs with it!Doublestop
T
5

Encountered the same issue with brew PostgreSQL, but I installed pg gem successfully with Ruby 2.7 on macOS M1(12 Monterey), I have both x86 and arm64 brew installed. Since pg must be compiled with x86 libpq, so I installed libpq with x86 brew


❯ which brow
brow: aliased to arch --x86_64 /usr/local/Homebrew/bin/brew

❯ brow install libpq

❯ brew install PostgreSQL # Install arm64 PostgreSQL 
❯ brew services start postgresql
❯ ps -ef | grep postgresql
  501 23655     1   0  2:29PM ??         0:00.10 /opt/homebrew/opt/postgresql/bin/postgres -D /opt/homebrew/var/postgres

❯ which brew
/opt/homebrew/bin/brew
❯ brew -v
Homebrew 3.3.2-50-geca16a2
Homebrew/homebrew-core (git revision ec99d74792c; last commit 2021-11-05)
Homebrew/homebrew-cask (git revision 2ab51af9c3; last commit 2021-11-05)

Then I can install the pg gem

❯ ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]

❯ gem install pg -v 1.2.3 -- --with-pq-dir=/usr/local/Cellar/libpq/13.3
Building native extensions with: '--with-pq-dir=/usr/local/Cellar/libpq/13.3'
This could take a while...
Successfully installed pg-1.2.3
Parsing documentation for pg-1.2.3
Done installing documentation for pg after 0 seconds

But failed with Ruby 2.6, I don't know why

gem install pg -v 1.2.3 -- --with-pq-dir=/usr/local/Cellar/libpq/13.3
Building native extensions with: '--with-pq-dir=/usr/local/Cellar/libpq/13.3'
This could take a while...
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

    current directory: /Users/felix/.rvm/gems/ruby-2.6.6/gems/pg-1.2.3/ext
/Users/felix/.rvm/rubies/ruby-2.6.6/bin/ruby -I /Users/felix/.rvm/rubies/ruby-2.6.6/lib/ruby/site_ruby/2.6.0 -r ./siteconf20211105-41969-1oxcuyy.rb extconf.rb --with-pq-dir\=/usr/local/Cellar/libpq/13.3
checking for pg_config... yes
Using config values from /opt/homebrew/bin/pg_config
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for pg_config_manual.h... yes
checking for PQconnectdb() in -lpq... no
checking for PQconnectdb() in -llibpq... no
checking for PQconnectdb() in -lms/libpq... no
Can't find the PostgreSQL client library (libpq)

Update 2021-11-06

Installed pg successfully with Ruby 2.6 too.

  1. First install x86 postgresql
# build Postgresql from source, as 
# Error: postgresql: no bottle available! 
# You can try to install from source with:  
# brew install --build-from-source postgresql


brow install --build-from-source postgresql

  1. Install pg gem with prefix
gem install pg -v 1.2.3 -- --with-pg_config=/usr/local/Cellar/postgresql/13.3/bin/pg_config --with-pq-dir=/usr/local/Cellar/libpq/13.3 
Tilly answered 5/11, 2021 at 8:42 Comment(1)
Thank you for this, I was looking all over for a solution to this and it was brew install PostgreSQL that allowed me to run gem install pg without errors.Rufinaruford

© 2022 - 2024 — McMap. All rights reserved.