phillipjones1
phillipjones1

Reputation: 327

I have an issue with ruby/gem management and postgres libpq

I've switched from RVM to rbenv to now using chruby. I made sure the RVM and rbenv were uninstalled completely. I am using ruby 2.2.5. I am having issues when bundle installing:

Installing pg 0.18.4 with native extensions

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb
checking for pg_config... yes
Using config values from /usr/local/bin/pg_config
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)
*** 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.

I have a question about how my ruby gems are being managed. I notice the bundler is looking in the system file (Ruby.framework/Versions/2.0) for gems but I am using ruby 2.2.5. I also notice the gem files are installed in a ruby/2.2.5/ruby/2.0.0/gems --(see code below)--

Gem files will remain installed in /Users/phillipjones/.gem/ruby/2.2.5/ruby/2.0.0/gems/pg-0.18.4 for inspection.
Results logged to /Users/phillipjones/.gem/ruby/2.2.5/ruby/2.0.0/gems/pg-0.18.4/ext/gem_make.out

Here is my Gem env:

ruby 2.2.5p319 (2016-04-26 revision 54774) [x86_64-darwin14]
~/w/Rafftopia ❯❯❯ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 2.4.5.1
  - RUBY VERSION: 2.2.5 (2016-04-26 patchlevel 319) [x86_64-darwin14]
  - INSTALLATION DIRECTORY: /Users/phillipjones/.gem/ruby/2.2.5
  - RUBY EXECUTABLE: /Users/phillipjones/.rubies/ruby-2.2.5/bin/ruby
  - EXECUTABLE DIRECTORY: /Users/phillipjones/.gem/ruby/2.2.5/bin
  - SPEC CACHE DIRECTORY: /Users/phillipjones/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /Users/phillipjones/.rubies/ruby-2.2.5/etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-darwin-14
  - GEM PATHS:
     - /Users/phillipjones/.gem/ruby/2.2.5
     - /Users/phillipjones/.rubies/ruby-2.2.5/lib/ruby/gems/2.2.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /Users/phillipjones/.gem/ruby/2.2.5/bin
     - /Users/phillipjones/.rubies/ruby-2.2.5/lib/ruby/gems/2.2.0/bin
     - /Users/phillipjones/.rubies/ruby-2.2.5/bin
     - /usr/local/bin
     - /usr/local/sbin
     - /usr/bin
     - /bin
     - /usr/sbin
     - /sbin

I've tried many suggestions to resolve this issue with no avail. Any help would be greatly appreciated.

Upvotes: 1

Views: 173

Answers (1)

Ilya Vassilevsky
Ilya Vassilevsky

Reputation: 1001

The pg gem contains an extension for the Ruby interpreter, written in C. When you install the gem, the extension is being compiled with a C compiler installed on your system. When you launch your application and it connects to the PostgreSQL server, this extension is loaded and executed as part of Ruby.

However, the extension needs PostgeSQL libraries and C header files to be compiled. Bundler cannot find them and cannot compile the extension.

You need to tell Bundler where to find the libraries for compiling the pg extension. It can get this information from the pg_config program so you need to tell it where to find pg_config. Assuming you run the latest PostgreSQL version:

bundle config build.pg -- --with-pgconfig=/usr/local/Cellar/postgresql/9.5.3/bin/pg_config

Run this command and then bundle install again. It should work.

When you use any of the Ruby version managers, you no longer should use system Ruby from Ruby.framework/Versions/2.0. A Ruby version manager should make it so when you run ruby, you are running a custom version you chose with it.

Upvotes: 0

Related Questions