10
votes

I need to install postgresql in production environment in an application ROR for after use this with heroku, but when I try to install the gem 'pg' the following error is triggered. I'm a newbie, so I don't know that do

I'm using ruby 1.9.3p547 (2014-05-14 revision 45962) [i686-linux] rails

Rails 3.2.19 Ubuntu 12.04

GemFile

source 'https://rubygems.org'

gem 'rails', '3.2.19'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'mysql2'
gem 'hirb'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

group :production do
  gem 'pg'

end


# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'

The problem appears when I run bundle update or bundle install

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /home/fernando/.rvm/rubies/ruby-1.9.3-p547/bin/ruby -r ./siteconf20141203-14261-3uu564.rb extconf.rb 
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/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.

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=/home/fernando/.rvm/rubies/ruby-1.9.3-p547/bin/ruby
    --with-pg
    --without-pg
    --with-pg-config
    --without-pg-config
    --with-pg_config
    --without-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

extconf failed, exit code 1

Gem files will remain installed in /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/gems/pg-0.17.1 for inspection.
Results logged to /home/fernando/.rvm/gems/ruby-1.9.3-p547@ticket_master/extensions/x86-linux/1.9.1/pg-0.17.1/gem_make.out
An error occurred while installing pg (0.17.1), and Bundler cannot continue.
Make sure that `gem install pg -v '0.17.1'` succeeds before bundling.
4

4 Answers

31
votes

Your problem is described in the log No pg_config... trying anyway.

This config is related to the libpq dependency, when I was starting with rails I faced this kind of problems, so, I think that the answer is just follow the following steps

Run this command in your console.

sudo apt-get install libpq-dev 
sudo gem install pg
14
votes

The above solution didn't work for me, but the instructions here did. I'll summarize below:

  • Make sure you've installed PostgreSQL
  • Find your new pg_config path with sudo find / -name "pg_config"
  • Copy the path
  • Run gem install pg -- --with-pg-config=path/to/pg_config
  • Success!
0
votes

As per rderoldan1's solution I ran as below (using Fedora 23):

dnf install libpq-dev 

but I've got this message:

No package libpq-dev available. Error: Unable to find a match.

Initially, PostgreSQL was installed with the following command:

dnf install postgresql-server postgresql-contrib

And when I've run which pg_config I got this:

/usr/bin/which: no pg_config in (/usr/local/heroku/bin:/usr/local/rvm/gems/ruby-2.2.4/bin:/usr/local/rvm/gems/ruby-2.2.4@global/bin:/usr/local/rvm/rubies/ruby-2.2.4/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/usr/local/rvm/bin:/home/mayur/.local/bin:/home/mayur/bin)

So I installed PostgreSQL again with this command:

dnf install postgresql-devel

Now when I run which pg_config I get:

/usr/bin/pg_config

and I can run bundle install without any error.

0
votes

In my case, I use Postgres 9.5.2, Ruby 2.3.0 and Rails 4.2.6 on OSX El Capitan 10.11.5. All properly set up and work nicely until I tried to deployment production env on local machine. Tried the solution above, none of them work for me though it point me to the right direction.

So my problem is mainly about bundler not the gem install. To make this right,firstly the gem should be properly install again as instructed above. The path for pg_config might varies by Postgres version.

$gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.5/bin/pg_config

To bundle it, you might need to configure it proper before bundle install

$bundle config build.pg --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.5/bin/pg_config
You are replacing the current global value of build.pg, which is currently "--with-pg-config=/opt/local/lib/postgresql91/bin/pg_config"

Now you should be able to run bundle install without problem.

Hope this help.