Getting Ruby GD2 working on OSX 7
I installed the Ruby GD2 gem on my Mac last night and found myself getting the following error:
ruby/gems/1.8/gems/gd2-1.1.1/lib/gd2.rb:59:in `initialize’:RuntimeError: dlopen(libgd.2.dylib, 9): image not found
It didn’t take very long to figure out that OSX doesn’t seem to come with any version of libgd. After bouncing around with trying to get RMagick installed instead, and finding very little documentation on setting up the Ruby GD2 library, I decided to install the gd2 package via ports and reinstalled the gem with the same results. It still wasn’t finding libgd because the ports library path (/opt/local/lib) is not set in my path. I don’t set this because I want anything self-compiled to only use the Mac libraries. After some digging, I found that I could specify the path in the Ruby GD2 source.
To fix it I changed line 33 in “ruby/gems/1.8/gems/gd2-1.1.1/lib/gd2.rb” from:
case Config::CONFIG[‘arch’]
when /darwin/
‘libgd.2.dylib’
to:
case Config::CONFIG[‘arch’]
when /darwin/
‘/opt/local/lib/libgd.2.dylib’
It’s not the cleanest fix, but it’s good enough for development purposes. Hopefully this saves a few people from some of the circles I went through figuring this out. It does seem that this is a least a more simple fix than what’s required to get the RMagick library running on OSX.
ruby/gems/1.8/gems/gd2-1.1.1/lib/gd2.rb:59:in `initialize’:RuntimeError: dlopen(libgd.2.dylib, 9): image not found
It didn’t take very long to figure out that OSX doesn’t seem to come with any version of libgd. After bouncing around with trying to get RMagick installed instead, and finding very little documentation on setting up the Ruby GD2 library, I decided to install the gd2 package via ports and reinstalled the gem with the same results. It still wasn’t finding libgd because the ports library path (/opt/local/lib) is not set in my path. I don’t set this because I want anything self-compiled to only use the Mac libraries. After some digging, I found that I could specify the path in the Ruby GD2 source.
To fix it I changed line 33 in “ruby/gems/1.8/gems/gd2-1.1.1/lib/gd2.rb” from:
case Config::CONFIG[‘arch’]
when /darwin/
‘libgd.2.dylib’
to:
case Config::CONFIG[‘arch’]
when /darwin/
‘/opt/local/lib/libgd.2.dylib’
It’s not the cleanest fix, but it’s good enough for development purposes. Hopefully this saves a few people from some of the circles I went through figuring this out. It does seem that this is a least a more simple fix than what’s required to get the RMagick library running on OSX.
Rather than having to edit the gem, I find it cleaner to just symlink the library from /usr/lib:
flake:/usr/lib morten$ sudo ln -s /opt/local/lib/libgd.2.dylib
I think the better solution would be for the library to check LDPATH, and then you could add /opt/local/lib to LDPATH.
I found it quick and useful, thanks.
got rid of my pain - thanks for the article and the comments - I used a symlink. didn’t fully understand the LDPATH solution but interested to learn more. thanks…scott
I was saved! This article was the only one I found about my trouble. Thank you very match.
I added a different hack that looks like this:
#FIXME: I changed this so that if the lib isn’t found, then we use the # hardcoded path as defined in the blog post # http://blog.chrispcritter.com/articles/2006/11/26/getting-ruby-gd2-working-on-osx begin LIB = DL.dlopen(gdlibraryname) rescue RuntimeError LIB = DL.dlopen(‘/opt/local/lib/libgd.2.dylib’) end
That way if the regular one works, great!
I added a different hack that looks like this:
#FIXME: I changed this so that if the lib isn’t found, then we use the # hardcoded path as defined in the blog post # http://blog.chrispcritter.com/articles/2006/11/26/getting-ruby-gd2-working-on-osx begin LIB = DL.dlopen(gdlibraryname) rescue RuntimeError LIB = DL.dlopen(‘/opt/local/lib/libgd.2.dylib’) end
That way if the regular one works, great!