宮水の日記

宮水の日記

主に書評や資格取得について記事を書いています。

M2 Macでbundle installすると、pgとsqllite3がinstallできない問題

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

current directory: /Users/miyazakimizuki/Desktop/works/menta/taskManagement/vendor/bundle/ruby/2.7.0/gems/pg-1.1.4/ext
/Users/miyazakimizuki/.rbenv/versions/2.7.6/bin/ruby -I /Users/miyazakimizuki/.rbenv/versions/2.7.6/lib/ruby/2.7.0 -r ./siteconf20220907-45614-6vcv45.rb extconf.rb

    • with-pg-config\=/usr/local/Cellar/libpq/14.5/bin/pg_config

Using config values from /usr/local/Cellar/libpq/14.5/bin/pg_config
sh: /usr/local/Cellar/libpq/14.5/bin/pg_config: No such file or directory
sh: /usr/local/Cellar/libpq/14.5/bin/pg_config: No such file or directory
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header

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/miyazakimizuki/.rbenv/versions/2.7.6/bin/$(RUBY_BASE_NAME)
--with-pg
--without-pg
--enable-windows-cross
--disable-windows-cross
--with-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

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

/Users/miyazakimizuki/Desktop/works/menta/taskManagement/vendor/bundle/ruby/2.7.0/extensions/arm64-darwin-21/2.7.0/pg-1.1.4/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/miyazakimizuki/Desktop/works/menta/taskManagement/vendor/bundle/ruby/2.7.0/gems/pg-1.1.4 for inspection.
Results logged to /Users/miyazakimizuki/Desktop/works/menta/taskManagement/vendor/bundle/ruby/2.7.0/extensions/arm64-darwin-21/2.7.0/pg-1.1.4/gem_make.out

An error occurred while installing pg (1.1.4), and Bundler cannot continue.
Make sure that `gem install pg -v '1.1.4' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
pg

```


pgは以下のコマンドで直りました。
```
bundle config build.pg --with-pg-config=/opt/homebrew/Cellar/libpq/14.5/bin/pg_config
arch -arm64 sudo gem install pg -v '1.1.4' --source 'https://rubygems.org/'
arch -arm64 sudo bundle install
```

sqlite3は.zshrcに以下のpathを通したり、
ネットで調べたコマンドに`arch -arm64`を先頭につけてコマンドを打つと直ってました。
```
export PATH="/opt/homebrew/opt/sqlite/bin:$PATH"
export LDFLAGS="-L/opt/homebrew/opt/sqlite/lib"
export CPPFLAGS="-I/opt/homebrew/opt/sqlite/include"
```

ここまで3日かかった...。
やっぱりDockerで開発するのがいいですね。。

参考:
M1 MacのHomebrewでのインストール時に「Cannot install under Rosetta 2 in ARM default prefix (/opt/homebrew)!」エラーが出る場合の解決法 – Webrandum
macOSでgem install pgしてNo pg_configとなる対処法 - Qiita