libiconv Problems with MAMP, PHP, and Ruby

This past week I was having some trouble with a PHP application that tries to shell out to an external Ruby script. The problem was only occuring on my development MacBook Pro, where I had installed MAMP 2.0.5 to run the application. I was able to hit the straight PHP pages without a problem. But whenever I tried to hit the page that shelled out to Ruby, I would see the following error in the log file:

Error: dlopen(/usr/lib/ruby/1.8/universal-darwin10.0/iconv.bundle, 9): Symbol not found: _iconv
  Referenced from: /usr/lib/ruby/1.8/universal-darwin10.0/iconv.bundle
  Expected in: flat namespace

I googled for answers and came up with very little. Eventually I found this thread where someone talks about trying to run Passenger with RVM on a MAMP Apache server. It was determined the problem was the MAMP libiconv was slightly different from the standard OS X libiconv.

Armed with this information I added the following to the Ruby script:

raise ENV['DYLD_LIBRARY_PATH']

And sure enough, the DYLD_LIBRARY_PATH was being set to /Applications/MAMP/Library/lib, which meant the MAMP libiconv was being used by an RVM Ruby that was expecting the system libiconv.

To fix the problem I modified the PHP code so that the command being executed cleared the DYLD_LIBRARY_PATH:

shell_exec("DYLD_LIBRARY_PATH='' && rvm 1.8.7 exec bundle exec ruby script.rb");

The same solution worked again when shelling out to wkhtmltopdf gave me the following error:

dyld: Symbol not found: __cg_jpeg_resync_to_restart
  Referenced from: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources/libTIFF.dylib
  Expected in: /Applications/MAMP/Library/lib/libJPEG.dylib