Ruby on Rails

[Mac] Rails7 프로젝트 생성 후 pg gem 오류 해결방법

juyeong.lee 2023. 7. 9. 21:07
반응형
$ rails new [프로젝트이름] --database=postgresql

로 생성한 후

$ bundle install

과정에서 아래와 같은 오류가 발생했다.

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

current directory:
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/pg-1.5.3/ext
/Users/juyeong/.rbenv/versions/3.2.2/bin/ruby extconf.rb
Calling libpq with GVL unlocked
checking for pg_config... no
checking for libpq per pkg-config... no
Using libpq from
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*****************************************************************************

Unable to find PostgreSQL client library.

Please install libpq or postgresql client package like so:
  brew install libpq

or try again with:
  gem install pg -- --with-pg-config=/path/to/pg_config

or set library paths manually with:
gem install pg -- --with-pg-include=/path/to/libpq-fe.h/
--with-pg-lib=/path/to/libpq.so/

*** 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/juyeong/.rbenv/versions/3.2.2/bin/$(RUBY_BASE_NAME)
	--with-pg
	--without-pg
	--enable-gvl-unlock
	--disable-gvl-unlock
	--enable-windows-cross
	--disable-windows-cross
	--with-pg-config
	--without-pg-config
	--with-pg_config
	--without-pg_config
	--with-libpq-dir
	--without-libpq-dir
	--with-libpq-include
	--without-libpq-include=${libpq-dir}/include
	--with-libpq-lib
	--without-libpq-lib=${libpq-dir}/lib
	--with-libpq-config
	--without-libpq-config
	--with-pkg-config
	--without-pkg-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

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

/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/extensions/x86_64-darwin-22/3.2.0/pg-1.5.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/pg-1.5.3 for
inspection.
Results logged to
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/extensions/x86_64-darwin-22/3.2.0/pg-1.5.3/gem_make.out

/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/ext/builder.rb:119:in
`run'
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/ext/ext_conf_builder.rb:27:in
`build'
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/ext/builder.rb:187:in
`build_extension'
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/ext/builder.rb:221:in
`block in build_extensions'
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/ext/builder.rb:218:in
`each'
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/ext/builder.rb:218:in
`build_extensions'
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/3.2.0/rubygems/installer.rb:843:in
`build_extensions'
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.4.13/lib/bundler/rubygems_gem_installer.rb:72:in
`build_extensions'
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.4.13/lib/bundler/rubygems_gem_installer.rb:28:in
`install'
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.4.13/lib/bundler/source/rubygems.rb:198:in
`install'
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.4.13/lib/bundler/installer/gem_installer.rb:54:in
`install'
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.4.13/lib/bundler/installer/gem_installer.rb:16:in
`install_from_spec'
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.4.13/lib/bundler/installer/parallel_installer.rb:156:in
`do_install'
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.4.13/lib/bundler/installer/parallel_installer.rb:147:in
`block in worker_pool'
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.4.13/lib/bundler/worker.rb:62:in
`apply_func'
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.4.13/lib/bundler/worker.rb:57:in
`block in process_queue'
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.4.13/lib/bundler/worker.rb:54:in
`loop'
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.4.13/lib/bundler/worker.rb:54:in
`process_queue'
/Users/juyeong/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.4.13/lib/bundler/worker.rb:90:in
`block (2 levels) in create_threads'

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

In Gemfile:
  pg

우선 위쪽에 나와있는 대로

$ brew install libpq

를 진행했다.

 

하지만 다시 bundle install을 진행하면 동일한 오류가 발생하는데...

 

libpq 설치 완료 후 나왔던 내용을 참고하면 된다.

libpq is keg-only, which means it was not symlinked into /usr/local,
because conflicts with postgres formula.

If you need to have libpq first in your PATH, run:
  echo 'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> ~/.zshrc

For compilers to find libpq you may need to set:
  export LDFLAGS="-L/usr/local/opt/libpq/lib"
  export CPPFLAGS="-I/usr/local/opt/libpq/include"

For pkg-config to find libpq you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/libpq/lib/pkgconfig"

iTerm 창에 해당 내용을 입력해주면 bundle install이 문제없이 잘 진행된다.

$ export PATH="/usr/local/opt/libpq/bin:$PATH"
반응형