Are shared libraries being loaded in some other context?

I have come across an oddity and wanted to ask if anyone else has looked
into this.

  1. I have a locally installed gem: RMagick.
  2. It loads and works just fine from script/console when I have
    LD_LIBRARY_PATH and GEM_PATH set in the shell.

However, in the live environment it just does not want to play nicely.
I have both LD_LIBRARY_PATH and GEM_PATH set in environment.rb and echo
those values after a failure to load RMagick and see they are set. Here
is some debug output from the controller trying to load RMagick in a
live run:

error is: libMagick.so.10: cannot open shared object file: No such file
or directory - /home/myusername/gems/gems/rmagick-1.14.1/lib/RMagick.so

LD_LIBRARY_PATH is: /home/myusername/local/lib

GEM_PATH is:/home/myusername/gems:/usr/lib/ruby/gems/1.8

This is all running under Apache so I have checked that the Apache user
can read all the relevant directories. I have also tried PassEnv in my
.htaccess to no avail - in any event it is clear from the above output
that the environment variables are being set properly.

My theory on things: The one main difference between the console and
live sessions is that with the console the environment variables are
already set before I invoke Ruby, whereas they are set in the live
session only after things have started up. Is it possible that Ruby or
RMagick are remembering the LD_LIBRARY_PATH from a time before I get a
chance to set them in environment.rb and using that (erroneous) value?

This has really got me stumped. I have trawled this forum and the net
and seen all sorts of solutions but nothing suggested has worked. I
feel so close to solving this and yet so far away. It perplexes me that
the console is quite happy to load RMagick and that RMagick finds
libMagick.so but in the live environment it simply fails to do the same.

Any and all tips and help will be gladly welcomed.

Thanks, Chad.