Search code examples
ruby-on-railspostgresqlrubygemsbundlerruby-on-rails-7

Not able to install pg gem - I this message: An error occurred while installing pg (1.2.3), and Bundler cannot continue


I'm on macOS Big Sur and recently installed homebrew. The installation is working fine and shows Your system is ready to brew. when I run brew doctor.

I've also successfully installed PostgreSQL using homebrew. The app uses ruby version 3.1.2 and Rails 7

Now I have a new Rails app and when I run bundle, I keep getting this error:

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

    current directory: /Users/max/.rvm/gems/ruby-3.1.2/gems/pg-1.2.3/ext
/Users/max/.rvm/rubies/ruby-3.1.2/bin/ruby -I /Users/max/.rvm/rubies/ruby-3.1.2/lib/ruby/3.1.0 -r ./siteconf20221017-65333-4ldjbt.rb extconf.rb
--with-cflags\=-Wno-error\=implicit-function-declaration
checking for pg_config... yes
Using config values from /usr/local/opt/libpq/bin/pg_config
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/Users/max/.rvm/rubies/ruby-3.1.2/bin/$(RUBY_BASE_NAME)
    --with-pg
    --without-pg
    --enable-windows-cross
    --disable-windows-cross
    --with-pg-config
    --without-pg-config
    --with-pg_config
    --without-pg_config
    --with-pg-dir
    --without-pg-dir
    --with-pg-include
    --without-pg-include=${pg-dir}/include
    --with-pg-lib
    --without-pg-lib=${pg-dir}/lib
/Users/max/.rvm/rubies/ruby-3.1.2/lib/ruby/3.1.0/mkmf.rb:498:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
    from /Users/max/.rvm/rubies/ruby-3.1.2/lib/ruby/3.1.0/mkmf.rb:591:in `try_link0'
    from /Users/max/.rvm/rubies/ruby-3.1.2/lib/ruby/3.1.0/mkmf.rb:609:in `try_link'
    from extconf.rb:40:in `<main>'

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /Users/max/.rvm/gems/ruby-3.1.2/extensions/x86_64-darwin-19/3.1.0/pg-1.2.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/max/.rvm/gems/ruby-3.1.2/gems/pg-1.2.3 for inspection.
Results logged to /Users/max/.rvm/gems/ruby-3.1.2/extensions/x86_64-darwin-19/3.1.0/pg-1.2.3/gem_make.out

  /Users/max/.rvm/rubies/ruby-3.1.2/lib/ruby/3.1.0/rubygems/ext/builder.rb:95:in `run'
  /Users/max/.rvm/rubies/ruby-3.1.2/lib/ruby/3.1.0/rubygems/ext/ext_conf_builder.rb:47:in `block in build'
  /Users/max/.rvm/rubies/ruby-3.1.2/lib/ruby/3.1.0/tempfile.rb:317:in `open'
  /Users/max/.rvm/rubies/ruby-3.1.2/lib/ruby/3.1.0/rubygems/ext/ext_conf_builder.rb:26:in `build'
  /Users/max/.rvm/rubies/ruby-3.1.2/lib/ruby/3.1.0/rubygems/ext/builder.rb:161:in `build_extension'
  /Users/max/.rvm/rubies/ruby-3.1.2/lib/ruby/3.1.0/rubygems/ext/builder.rb:195:in `block in build_extensions'
  /Users/max/.rvm/rubies/ruby-3.1.2/lib/ruby/3.1.0/rubygems/ext/builder.rb:192:in `each'
  /Users/max/.rvm/rubies/ruby-3.1.2/lib/ruby/3.1.0/rubygems/ext/builder.rb:192:in `build_extensions'
  /Users/max/.rvm/rubies/ruby-3.1.2/lib/ruby/3.1.0/rubygems/installer.rb:853:in `build_extensions'
  /Users/max/.rvm/gems/ruby-3.1.2/gems/bundler-2.3.16/lib/bundler/rubygems_gem_installer.rb:71:in `build_extensions'
  /Users/max/.rvm/gems/ruby-3.1.2/gems/bundler-2.3.16/lib/bundler/rubygems_gem_installer.rb:28:in `install'
  /Users/max/.rvm/gems/ruby-3.1.2/gems/bundler-2.3.16/lib/bundler/source/rubygems.rb:207:in `install'
  /Users/max/.rvm/gems/ruby-3.1.2/gems/bundler-2.3.16/lib/bundler/installer/gem_installer.rb:54:in `install'
  /Users/max/.rvm/gems/ruby-3.1.2/gems/bundler-2.3.16/lib/bundler/installer/gem_installer.rb:16:in `install_from_spec'
  /Users/max/.rvm/gems/ruby-3.1.2/gems/bundler-2.3.16/lib/bundler/installer/parallel_installer.rb:186:in `do_install'
  /Users/max/.rvm/gems/ruby-3.1.2/gems/bundler-2.3.16/lib/bundler/installer/parallel_installer.rb:177:in `block in worker_pool'
  /Users/max/.rvm/gems/ruby-3.1.2/gems/bundler-2.3.16/lib/bundler/worker.rb:62:in `apply_func'
  /Users/max/.rvm/gems/ruby-3.1.2/gems/bundler-2.3.16/lib/bundler/worker.rb:57:in `block in process_queue'
  /Users/max/.rvm/gems/ruby-3.1.2/gems/bundler-2.3.16/lib/bundler/worker.rb:54:in `loop'
  /Users/max/.rvm/gems/ruby-3.1.2/gems/bundler-2.3.16/lib/bundler/worker.rb:54:in `process_queue'
  /Users/max/.rvm/gems/ruby-3.1.2/gems/bundler-2.3.16/lib/bundler/worker.rb:91:in `block (2 levels) in create_threads'

An error occurred while installing pg (1.2.3), and Bundler cannot continue.

In Gemfile:
  pg

The other gems install fine but pg appears to be the problem. PostgreSQL is already installed on the machine and I can access it with psql command.

libpq is also installed because I have it listed when I run the brew list command.

I've investigated both of these lines in red on the Terminal and could not find a solution.

Error 1: Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

Error 2: An error occurred while installing pg (1.2.3), and Bundler cannot continue.

Please help.


Solution

  • I was able to fix this by reinstalling Xcode and installing coreutils with brew install coreutils

    I also had to install pg by using the pg_config path flag. gem install pg -- --with-pg-config=/path-to-pg-config/pg_config

    It takes a little while then installs the pg gem.