views:

95

answers:

1

On my web host's system, installing and using gems seems to require installing my own copy of ruby. With their help on a forum, I've gotten that done. I'm trying to use a particular gem (called Image Science) in a Rails app.

At this point, if I open irb and type the following, this is what I get:

require 'rubygems' #true
require 'image_science' #LoadError: libfreeimage.so.3: cannot open shared 
                         #object file: No such file or directory (etc)....

On the host's advice, I go back to bash and type this:

export LD_LIBRARY_PATH=~/ruby1.8/lib

That command allows irb to require image_science - it returns 'true.' As I understand it, it's saying, "hey, also look in this directory for gems."

The problem is that this doesn't change what my Ruby scripts can access. (It also only persists for the session, but I suppose I can add it to my .bashrc.) In my Rails app, I still get an error if I try to require that gem.

So I've got two questions:

  1. How do I make this gem available to my Ruby scripts - specifically, a Rails model file?
  2. Should I even need to put the "require" command in the model file, or is there some other way that the gem should get loaded?
+2  A: 

If the gem is only going to be used by one model, I generally just do require 'gem' on that model. If the app is going to use the gem, say in the view or a controller, I create a file called app.rb and stick it in config/initializers that includes all the require statements. You can also include it in config/environment.rb, inside the initializer block:

config.gem 'pg', :lib => 'pg'

which will require that gem before the project loads, however, I've had trouble with that with certain gems, like ruby facets.

For the LD_LIBRARY_PATH, put this in one of the config/environments/*.rb files (customize for your environments, but development is most likely different from production)

ENV['LD_LIBRARY_PATH'] = "#{ENV['LD_LIBRARY_PATH']}:#{ENV['JAVA_HOME']}/jre/lib/amd64:#{ENV['JAVA_HOME']}/jre/lib/amd64/server"
igkins
So this is a colon-separated list of locations, and I should add the one above to it?
Nathan Long
I tried this: `ENV['LD_LIBRARY_PATH'] = "#{ENV['LD_LIBRARY_PATH']}:~/ruby1.8/lib"`No change so far. This is on a Linux box.
Nathan Long
try it with the full, absolute path to the library, not the ~
igkins