Search code examples
rubyherokubundlejrubypg

No such file to load when requiring jruby pg on Heroku


I'm looking to use the jRuby pg gem in a jRuby application. The gem runs fine locally, but when run on heroku it is unable to load the pg gem.

LoadError: no such file to load -- pg
  require at org/jruby/RubyKernel.java:1071
  require at /app/vendor/ruby-1.9.3-jruby-1.7.19/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:54
   (root) at text.rb:19

My Gemfile is:

ruby '1.9.3', :engine => 'jruby', :engine_version => '1.7.19'
gem 'pg', '0.17.1', :platform => :jruby, :git => 'git://github.com/headius/jruby-pg.git', :branch => :master

Heroku bundle show returns:

Gems included by the bundle:
 * pg (0.17.1 c236a38)

Heroku ruby -v returns:

jruby 1.7.19 (1.9.3p551) 2015-02-06 fffffff on OpenJDK 64-Bit Server VM 1.8.0_60-cedar14-b24 +jit [linux-amd64]

text.rb contains:

require 'rubygems'
require 'pg'
puts "cool!"

So far I have:

  • run bundle locally and repushed
  • run bundle install locally and repushed
  • cleared Gemfile.lock repushed and then run bundle on heroku
  • updated heroku toolbelt
  • updated ruby gems
  • checked to see that I'm running the same version of jruby locally and on heroku
  • uninstalled pg gem and reinstalled
  • required rubygems in the text.rb file
  • required the absolute path of pg.rb on heroku, this resulted in an unable to load pg_ext error

Solution

  • In order for this to work, you'll need to prefix your Procfile command with bundle exec (because it's a non-standard gem Heroku can't put it on the path automatically).

    For example:

    test: bundle exec ruby text.rb
    

    However, this will add a bit of overhead to your JRuby startup time because Bundler has to launch two JRuby processes (one for itself, and then another with the correct gem context).

    Another option is to use the activerecord-jdbcpostgresql-adapter, which is more widely adopted.