Frustrating "require" problems with RMagick


#1

I am using RMagick in my app and am having problems in deployment. The
deployment machine (Fedora Core 5) can’t find RMagick unless I change
the “require” syntax from that used on the dev machine (OS X). And the
dev machine doesn’t like the syntax that works on the deployment server!
To wit:

[OS X]
$ script/console
Loading development environment.

require_gem ‘rmagick’
=> true

Magick
=> Magick

$ script/console

require ‘rmagick’
=> false

Magick
=> Magick

$ script/console
Loading development environment.
Application strings file ‘config/app_strings.dat’ does not exist.

require ‘RMagick’
[DELETED: Many error of the type:
/usr/local/lib/ruby/site_ruby/1.8/RMagick.rb:32: warning: already
initialized constant PercentGeometry]
NameError: undefined method assoc' for classMagick::ImageList’

[Fedora Core 5]

script/console

require_gem ‘rmagick’
=> true

Magick
NameError: uninitialized constant Magick
from
/usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:123:in
const_missing' from /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:131:inconst_missing’
from (irb):2

script/console

require ‘rmagick’
MissingSourceFile: no such file to load – rmagick

script/console

require ‘RMagick’
=> true

Magick
=> Magick

The gem and the library are installed on both machines (maybe not
entirely correctly, it appears, but they compiled and installed).

To summarize what’s happening:

  1. require_gem returns true on both machines but on FC5 the library
    isn’t really loading: Magick isn’t initialized.
  2. On OS X, “require ‘rmagick’” works fine, but FC5 can’t find the
    library when I spell it in all lowercase.
  3. On FC5, “require ‘RMagick’” works fine, but on OS X using the
    capitalized letters load a broken version of the library.

I’ve have recompiled and reinstalled till the cows come home. Can anyone
give me some guidance on how to set things up so that one of these three
load commands works consistently on both platforms?

Thanks!

/afb


#2

Adam B. wrote:

[OS X]

require ‘RMagick’
[DELETED: Many error of the type:
/usr/local/lib/ruby/site_ruby/1.8/RMagick.rb:32: warning: already
initialized constant PercentGeometry]

Try deleting the non-gem site_ruby installation of RMagick on OSX.

And then use:

require_gem ‘rmagick’
require ‘RMagick’


We develop, watch us RoR, in numbers too big to ignore.


#3

Mark Reginald J. wrote:

Try deleting the non-gem site_ruby installation of RMagick on OSX.

And then use:

require_gem ‘rmagick’
require ‘RMagick’

I find I don’t need to require_gem ‘rmagick’ on OSX…it’s somehow being
loaded when the Rails environment loads:

require_gem “rmagick”
=> false

And removing:

  • /usr/local/lib/ruby/site_ruby/1.8/RMagick.rb
  • /usr/local/lib/ruby/site_ruby/1.8/i686-darwin8.7.3/RMagick.bundle

Doesn’t make the following work, unfortunately:

require ‘RMagick’
[DELETED: Many error of the type:
/usr/local/lib/ruby/site_ruby/1.8/RMagick.rb:32: warning: already
initialized constant PercentGeometry]
NameError: undefined method assoc' for classMagick::ImageList’

My best guess is that there is actually something wrong with the gem
build on FC5. I’ve tried every combination of recompilation so I’m sort
of stuck. ImageMagick command line apps work fine and as far as I can
tell all the libraries are there and the library paths are set in
/etc/ld.so.conf.

Thanks for your suggestions!

/afb


#4

Adam B. wrote:

My best guess is that there is actually something wrong with the gem
build on FC5. I’ve tried every combination of recompilation so I’m sort
of stuck. ImageMagick command line apps work fine and as far as I can
tell all the libraries are there and the library paths are set in
/etc/ld.so.conf.

The gem build works fine for me on FC6, but I do need to use
the double-barreled require

require_gem 'rmagick'
require 'RMagick'

which is not what the docs say you need to do.

Perhaps update to the latest ImageMagick and ImageMagick-devel
packages, or build ImageMagick from source.


We develop, watch us RoR, in numbers too big to ignore.


#5
require_gem 'rmagick'
require 'RMagick'

which is not what the docs say you need to do.

And that does work on my FC5 machine. But a capitalized require
‘RMagick’ makes the Mac barf and it isn’t worth hassling with anymore. I
worked around it by catching any LoadError on the lowercase require and
then loading the capitalized one if necessary. Not nice, but I’ve wasted
enough time on this.

Re: your last three suggestions, done, done, and done. Really it all
installs and compiles beautifully. I’m starting to think the Mac is the
odd one out but I guess we’ll never know. :slight_smile:

Thanks again for your help.

/afb


#6

The trick is that HFS+ filesystem of Mac OS X is case-insensitive by
default. So “RMagick” and “rmagick” is the same on osx. But it’s not
the case in other case-sensitive filesystems.

I had the completely the same issue deploying my RoR app to Debian. So
be careful with names in require.

Gábor


#7

Adam B. wrote:

require_gem 'rmagick'
require 'RMagick'

which is not what the docs say you need to do.

And that does work on my FC5 machine. But a capitalized require
‘RMagick’ makes the Mac barf and it isn’t worth hassling with anymore. I
worked around it by catching any LoadError on the lowercase require and
then loading the capitalized one if necessary. Not nice, but I’ve wasted
enough time on this.

Re: your last three suggestions, done, done, and done. Really it all
installs and compiles beautifully. I’m starting to think the Mac is the
odd one out but I guess we’ll never know. :slight_smile:

Thanks again for your help.

/afb

All these replies and nobody has mentioned that “require_gem” doesn’t do
what you think it does and isn’t necessary anyway?

“require_gem” does not replace “require” for gems. It is used only to
specify a specific version of a gem, which must then be required anyway.
In the current beta version (0.9.0.8) of RubyGems you get a warning
message when you use “require_gem”.

The name of the library is “RMagick” and that is what you must specify
in the require statement. This is what all the RMagick examples and the
RMagick doc use:

require “RMagick”

Since OS X is case-insensitive “rmagick” works as well but Linux is
case-sensitive so you must use “RMagick”.

Since you’ve installed RMagick as a gem you must have RubyGems loaded
before requiring RMagick. If you haven’t already loaded RubyGems you’ll
need that, too.

require “rubygems”
require “RMagick”

But RoR always loads RubyGems so you shouldn’t need to do that. A simple
require “RMagick” is all you need. If that doesn’t work then RMagick is
not installed properly.

You already know this but I’ll say it anyway since so many people are
confused about it: When require fails it raises an exception. When it
returns false it means that whatever you required has already been
loaded.

There are many good tutorials about installing RMagick on OS X on the
web. Google will turn them up quickly. Mine is at
http://rmagick.rubyforge.org/install-osx.html. If you continue to have
problems try reproducing them in a stand-alone script, outside of your
Rails app. Often this reveals diagnostics that help resolve the problem.
You can always report problems with RMagick by emailing rmagick AT
rubyforge DOT org or post to RMagick’s Help forum on RubyForge.

Good luck!


#8

Adam B. wrote:

Gábor Sebestyén wrote:

The trick is that HFS+ filesystem of Mac OS X is case-insensitive…

Interesting. But then why oh why does this happen:

$ script/console

require ‘rmagick’
=> false

Magick
=> Magick

$ script/console

require ‘RMagick’
[DELETED: Many error of the type:
/usr/local/lib/ruby/site_ruby/1.8/RMagick.rb:32: warning: already
initialized constant PercentGeometry]
NameError: undefined method assoc' for classMagick::ImageList’

Obviously the two different case versions are loading different
RMagicks, yeah?

Thanks!

/afb

In the first case, “rmagick” has already been required using the
lowercase name, so require knows that it’s already been loaded and
returns false. However, require doesn’t know that “rmagick” and
“RMagick” are the same thing, so it loads “RMagick” again. The messages
you see are the result of loading RMagick.rb twice. There’s no evidence
here that you’ve loaded two different versions of RMagick.


#9

Gábor Sebestyén wrote:

The trick is that HFS+ filesystem of Mac OS X is case-insensitive…

Interesting. But then why oh why does this happen:

$ script/console

require ‘rmagick’
=> false

Magick
=> Magick

$ script/console

require ‘RMagick’
[DELETED: Many error of the type:
/usr/local/lib/ruby/site_ruby/1.8/RMagick.rb:32: warning: already
initialized constant PercentGeometry]
NameError: undefined method assoc' for classMagick::ImageList’

Obviously the two different case versions are loading different
RMagicks, yeah?

Thanks!

/afb


#10

On Dec 27, 2006, at 7:12 PM, Adam B. wrote:

Thanks!

/afb

Or the simple way that require tracks the already-loaded files makes
the names ‘rmagick’ and ‘RMagick’ different (since Ruby Strings are
case-sensitive regardless of the file system). See the footnote on p.
124 of the pickaxe (Programming Ruby, 2nd ed.) for more including the
bit “Some consider this a bug, and this behavior may well change in
later releases.”

-Rob

Rob B. http://agileconsultingllc.com
removed_email_address@domain.invalid


#11

On Dec 27, 2006, at 5:05 PM, Adam B. wrote:

Thanks,

/afb

p $LOADED_FEATURES

cheers-
– Ezra Z.
– Lead Rails Evangelist
– removed_email_address@domain.invalid
– Engine Y., Serious Rails Hosting
– (866) 518-YARD (9273)


#12

Rob B. wrote:

“Some consider this a bug, and this behavior may well change in
later releases.”

Yes, I remember that footnote. :slight_smile: But it does reraise a question that I
thought of a few nights ago: Is there a way to determine the full paths
of the loaded libraries, or is $: all I get?

Thanks,

/afb