LoginSignup
4
1

[Rails] pgをインストールできずrails newでアプリを新規作成できない

Posted at

環境

  • MacBookAir 13インチ M1(2020)
    OS:Sonoma 14.3
    メモリ:8GB
  • Rails: version 6.1.7.6

やりたいこと

rails newコマンドで新しくアプリケーションを作成する。

$ rails _6.1.7.6_ new sample -d postgresql

エラー内容

上記のコードを実行すると下記のようにエラーが発生。

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

In Gemfile:
  pg
         run  bundle binstubs bundler
Could not find gem 'pg (~> 1.1)' in rubygems repository https://rubygems.org/ or
installed locally.
The source does not contain any versions of 'pg'
       rails  webpacker:install
Could not find gem 'pg (~> 1.1)' in rubygems repository https://rubygems.org/ or installed locally.
The source does not contain any versions of 'pg'
Run `bundle install` to install missing gems.

エラー内容から、pgがうまくインストールされていないことが原因。

また、pgをインストールしようとしてもエラーが発生。

gem install pg -v '1.5.4' -- --with-cflags="-Wno-error=implicit-function-declaration"
実行結果&エラー内容
Building native extensions with: '--with-cflags=-Wno-error=implicit-function-declaration'
This could take a while...
ERROR:  Error installing pg:
        ERROR: Failed to build gem native extension.

    current directory: /Users/********/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/pg-1.5.4/ext
/Users/********/.rbenv/versions/3.0.1/bin/ruby -I /Users/********/.rbenv/versions/3.0.1/lib/ruby/3.0.0 -r ./siteconf20240131-87718-t3b1lr.rb extconf.rb --with-cflags\=-Wno-error\=implicit-function-declaration
Calling libpq with GVL unlocked
checking for pg_config... yes
Using config values from /Users/********/opt/anaconda3/bin/pg_config
checking for whether -Wl,-rpath,/Users/********/opt/anaconda3/lib is accepted as LDFLAGS... yes
Using libpq from /Users/********/opt/anaconda3/lib
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)
*****************************************************************************

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.

〜〜〜〜〜中略〜〜〜〜〜

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

  /Users/********/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/extensions/arm64-darwin-23/3.0.0/pg-1.5.4/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/********/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/pg-1.5.4 for inspection.
Results logged to /Users/********/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/extensions/arm64-darwin-23/3.0.0/pg-1.5.4/gem_make.out

調査

googleで下記のように調べた。

  • 「rails bundle pg install エラー」

それだけでは自力で解決できず、メンターさんに聞いた。その時に検索したワードは下記。

  • 「error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: role "postgres" does not exist」

原因

  • pgがローカルに存在せず、インストールもできないためbundleを実行した際にエラーが発生する
  • pgをインストールするには「libpq」が必要

解決策

下記の記事に書いてあることを試して解決。

gem install pg でエラーが出た時の対策

まずは、homebrewで「libpq」をインストール

$ brew install libpq

その後、Pathを通す必要があるので下記を実行して.zshrcにPATH情報を追記。

$ echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc

.zshrcの情報を反映させるためにターミナルを再起動または下記のコマンドを実行。

$ source ~/.zshrc 

Pathが通ったらgem installでpgをインストール。今回はその時点での最新版のver.1.5.4を選択。

$ gem install pg -v '1.5.4'

これでpgをインストール完了。

最後にアプリケーションのルートディレクトリでbundleを実行したらGemfile.lockが作成されて無事完了。

エラーメッセージに解決方法が書かれていたので、しっかり読めば解決できました。

解決までに試したこと

  • PostgreSQLがインストールされているか
    →homebrewでインストール済み
  • PostgreSQLが起動されているか
    →brew services psql を実行済み(restartも実行した)
  • PostgreSQLのデータベースにアクセスできるか
    →psql postgreでアクセス可能
  • PostgreSQLのバージョン確認
    →psql —versionで確認(14.10)
  • GemでPostgreSQLの過去のバージョンをインストール(0.18.4)
    →libpqが入っていないと古いバージョンもインストールできない

参考

gem install pg でエラーが出た時の対策
pgがインストールできない - Qiita

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1