Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 473
Tiago Avatar asked Jan 20 '21 13:01

Tiago


2 Answers

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
like image 99
wbharding Avatar answered Oct 29 '22 20:10

wbharding


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
  1. (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
  1. re-bundle your project and enjoy ruby on rosetta 2 (for now)
$ bundle install
$ rails s
like image 39
amlutz160 Avatar answered Oct 29 '22 19:10

amlutz160