初心者インフラエンジニアが困ったときに試行錯誤するブログ

インフラエンジニアが,開発の勉強をするときのログを残します

Macのローカルにpgをインストールするときにはまったこと

実現したいこと

postgresqlをgemでインストールしたかった

やったこと

Gemfile

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.3.7'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.0'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'
$ bundle install

これでとりあえずアプリは動くよなー

と,思いきや...

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

current directory:
/private/var/folders/sw/fst19tjs2bnf_05_g6gg8wcc0000gn/T/bundler20181111-33236-o5h7onpg-1.1.3/gems/pg-1.1.3/ext
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby -r
./siteconf20181111-33236-70rx9k.rb extconf.rb
checking for pg_config... yes
Using config values from /usr/local/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)

こんな感じのエラーが出て...

$ brew install libpq

これで解決!?

かと思いきや,また同じエラーが...

これに従い,

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

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"

パスを3つ追加してもだめ,

$ source ~/.bash_profile

もしているけど,同じエラーが出る.

これで解決

$ sudo env ARCHFLAGS="-arch x86_64" gem install pg

参考

以下の記事を参考にさせていただきました.

ruby on rails - Can't find the PostgreSQL client library (libpq) - Stack Overflow

本記事について

本記事は,学習を目的に書かれています.

間違い,改良案などございましたら,コメントしていただけると幸いです.

基本的に編集途中ですので,何を書いてほしいかもコメントいただけると,

その情報を中心に追記します.