Tag Archives: rubygems

How to Build the Unbuildable pg Gem: Ubuntu 13.04, rvm, and openssl

A couple weeks ago I spent several hours trying to get the pg gem to build on Ubuntu 13.04. There’s a Stack Overflow answer, How to Install Gem PG on Ubuntu, that is wrong, but kept me busy for an hour or so. This post is mostly so I won’t forget to do this again next time. This solution is particularly applicable if you are getting an error message that says PGConnect is being called with the wrong number of arguments.

The Problem

Ubuntu ships with a version of ruby that is compiled against one set of openssl libraries. Rvm installs its own version of openssl. That’s all fine and good until you install the postgres server (which builds against the Ubuntu version of openssl) and then try to install the pg gem (which builds against the rvm version). In short, you end up with a version of the postgresql client that can’t connect to the postgresql server–in fact, it won’t even build. People will give you “helpful” advice like “remove your openssl libraries” (which makes it impossible to build the postgresql server) or “use rvmsudo to force rvm’s openssl libs” (which forces building an incompatible version of the gem).

The Solution

I don’t know when they added this, but I just wanna hug the rvm developers:

rvm autolibs enable

Run that, and from now on, rvm will be MUCH smarter about using system libs versus its own libs. That one command made the pg gem build without a hitch, and more importantly, it made it build against the system version of openssl, making it compatible with my postgresql server.

Thanks, rvm guys!