Just do IT

思うは招く

[Ruby] Ubuntuにgemのpgをbundlerでインストールするときに出るエラー対処

問題

環境

の環境でRubygemsのpgをbundlerでインストールしようとした。

Gemfile

gem "pg"

インストール。

bundle install --path vendor/bundle

そしたらこんなエラーが出た。

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

詳しく見ると

You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a
client-side application.
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a
client-side application.
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header

PostgreSQLの開発用ライブラリがないとのこと。

解決

以下をインストールしたら解決できた。 今回はUbuntuだけどdebianでもいけるはず。CentOSならyumなのかな(詳しくない)。

sudo apt update
sudo apt install libpq-dev

あとはこれで使える。

require "pg"

qiita.com

www.ownway.info