使用環境
Windows10
Ubuntu18.0
やりたかったこと
以下のコマンドでDBをpostgresqlに指定しRailsアプリを作成。
$ rails new app_name -d postgresql
エラー状況
1)上記rails newの実行途中でpg 1.2.3が足りないようで、passwordを入力しbundle installを実行。
Fetching pg 1.2.3
Your user account isn't allowed to install to the system RubyGems.
You can cancel this installation and run:
bundle install --path vendor/bundle
to install the gems into ./vendor/bundle/, or you can enter your password
and install the bundled gems to RubyGems using sudo.
Password:
Installing pg 1.2.3 with native extensions
/usr/lib/ruby/vendor_ruby/rubygems/defaults/operating_system.rb:10: warning: constant Gem::ConfigMap is deprecated
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
2)bundle install 実行中に” libpq-fe.h header が見つかりません。”と表示。
Using config values from /usr/bin/pg_config
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.
3)さらに読み進めると"pg -v '1.2.3'がインストールされているか確認してください。"と表示。
An error occurred while installing pg (1.2.3), and Bundler
cannot continue.
Make sure that `gem install pg -v '1.2.3' --source
'https://rubygems.org/'` succeeds before bundling.
解決法
手順
Step.1 libpq-fe.h headerを追加するためlibpq -dev packageをインストール。
Step.2 gem pg -v 1.2.3をインストール。
Step.3 bundle installを実行。
Step.4 rails new app_name -d postgresqlを再実行。
Step.1 libpq-fe.h headerを追加するためlibpq -dev packageをインストール。
libpq-fe.h headerはlibpq -dev packageの一部のようです。
#Step1. libpq -dev をインストール
$ sudo apt-get install libpq-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
gyp libc-ares2 libhttp-parser2.7.1 libjs-async libjs-inherits
libjs-node-uuid libjs-underscore libssl1.0-dev libuv1-dev
nodejs-doc
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
libpq5
Suggested packages:
postgresql-doc-13
The following NEW packages will be installed:
libpq-dev
The following packages will be upgraded:
libpq5
1 upgraded, 1 newly installed, 0 to remove and 13 not upgraded.
Step.2 gem pg -v 1.2.3をインストール。
#Step.2 gem pg -v 1.2.3をインストール。
$ sudo gem install pg -v '1.2.3'
Step.3 bundle installを実行。
#Step.3 bundle installを実行。
$ bundle install
Step.4 rails new app_name -d postgresqlを再実行。
#Step.4 rails new app_name -d postgresqlを再実行。
$ rails new app_name -d postgresql
最初に実行した際に作成されたファイルがあるので、上書確認をされれば”y”で上書き実行し、アプリ作成完了。
Overwrite /home/XXXXXX/.gitignore? (enter "h" for h lp) [Ynaqdhm] y
おわりに
rails学習を開始したばかりの初学者です。
学習に詰まった箇所への対応策のアウトプットです。
間違い等ありましたら、ご指摘頂けますと幸いです。
参考
Step.1で参考にさせて頂いた記事。
stackoverflow: Can't find the 'libpq-fe.h header when trying to install pg gem
ubuntu packages: パッケージ: libpq-dev (10.15-0ubuntu0.18.04.1 など)