Getting MAMP 1.9 to work with Image Magick, imagick.so and other flora
It was a full eight hours of hair pulling. For some reason, all the tutorials that can be found on getting MAMP to work with Image Magick in Snow Leopard are incomplete, miss out information, or dated. Or all of them. They are excellent posts, but I could not get imagick.so to be loaded as a PHP module by following any of them. I won’t go into explaining what MAMP or Image Magick are, if you are reading this, you already know, and most likely are having the same problems I was having.
Here is a short list of the resources I used to write this procedure:
Getting Imagemagick (and more) to work with MAMP on OS X – misses info on compiling for Snow Leopard.
Installing Image Magick and Imagick for PHP for MAMP – misses change needed in ports conf file to enable Universal mode.
MAMP & Imagick on Snow Leopard – goes through the pitfalls, which makes the tutorial confusing, but goes into the Universal mode switch.
There are others which I may miss, such as forum posts or other blogs, if so, my apologies. In all, none of them go into the use of older libraries by MAMP in its sandboxed model, which breaks imagick.so when trying to compile it from source rather than using pecl.
1. Install MacPorts
I won’t go into details as you most likely have already done it if you’re reading this. Don’t update your ports yet!
2. Make MacPorts build Universal binaries
Simply edit /opt/local/etc/macports/variants.conf and add +universal at the end of the file. Now, update your ports collection by running:
sudo port -v selfupdate
3. Install Image Magick using MacPorts
Simple:
sudo port install ImageMagick
This takes a while, so go grab a coffee.
4. Update your path
Add
export PATH="$PATH:/opt/local/bin"
to ~/.profile
5. Download and install the imagick PHP extension
Grab it from here, I used 3.0.1 RC2. Extract the archive, and then execute the following in Terminal:
MACOSX_DEPLOYMENT_TARGET=10.6 CFLAGS="-arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp" CCFLAGS="-arch i386 -arch x86_64 -g -Os -pipe" CXXFLAGS="-arch i386 -arch x86_64 -g -Os -pipe" LDFLAGS="-arch i386 -arch x86_64 -bind_at_load" export CFLAGS CXXFLAGS LDFLAGS CCFLAGS MACOSX_DEPLOYMENT_TARGET
This sets up compilation as a Universal binary. Once the exports are done, execute:
cd /imagick/ phpize ./configure --with-imagick=/opt/local make make install
When this completes, you will have a directory called modules, inside which you will find imagick.so, the PHP extension. You can check compilation worked by executing:
file imagick.so
which should return:
imagick.so: Mach-O universal binary with 2 architectures imagick.so (for architecture i386): Mach-O bundle i386 imagick.so (for architecture x86_64): Mach-O 64-bit bundle x86_64
If it doesn’t, then your exports didn’t happen correctly, and only one version was built.
6. Copy the module to the MAMP modules folder
You now need to copy imagick.so to the folder where MAMP stores PHP extensions, which in my install is:
/Applications/MAMP/bin/php5.3/lib/php/extensions/no-debug-non-zts-20090626/
for PHP 5.3, and
/Applications/MAMP/bin/php5.2/lib/php/extensions/no-debug-non-zts-20060613/
for PHP 5.2.
7. Add the extension to php.ini
Here you can choose to edit the MAMP template (File -> Edit Template -> [your PHP version]) or simply edit php.ini found in either /Applications/MAMP/conf/php5.3 or /Applications/MAMP/conf/php5.2. Add the following line:
extension=imagick.so
8. The Test
The easiest way to see what PHP is being run with is to create a simple php page with:
<?php
phpinfo();
?>
This will show you all the extensions being loaded, and you should see an entry for imagick. If you don’t, check the PHP error log, where you may find crap like this:
dyld: Symbol not found: __cg_jpeg_resync_to_restart Referenced from: /System/Library/Frameworks/ApplicationServices... Expected in: /Applications/MAMP/Library/lib/libjpeg.62.dylib
which is Not A Good Thing™. You may also hit library version incompatibilities, such as imagick.so requiring newer libraries than those provided by MAMP. If this is the case, you need to edit MAMP’s environment variables, thus:
sudo vi /Applications/MAMP/Library/bin/envvars
Comment the two lines that set and export the path, which look like:
#DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH" #export DYLD_LIBRARY_PATH
and add:
export PATH="$PATH:/opt/local/bin"
This has the effect of breaking out of the MAMP sandbox, and using the newer libraries found at /opt/local/bin. You could also delete the originals in MAMP and add dynamic links to the newer versions at /opt/local/bin, but I have not tried this nor recommend it.
After doing this change, restart MAMP, and try to reload the PHP info page – imagick should now be listed and working!
Let me know if I missed anything or you have any improvements to this tutorial, I am by no means an expert at this, just managed to get it working after a bout of severe hair loss.
Thanks for your post. It saved my day!
However, using the same imagick.so for both version of PHP doesn’t work for me. I mean I had to make 2 seperated imagick.so, one for PHP5.3 and one for PHP5.2.
Hope this helps
Nice – can you tell me what you did to compile versions for 5.2? I will update the post as needed.
Thanks for this tutorial! It is working great!
Just a note:
this command:
./configure –with-imagick=/opt/local
should be:
./configure -–with-imagick=/opt/local
Emil,
Thanks – I updated the post to add the missing dash!
Hi Mike
I am on about day 14 trying to get this to work. I was happy to see this article because I had already slogged through the other sources you referenced, thinking that I would blog a simple tutorial once I got things to work. But I am not there yet…
I just built a clean Snow Leopard install on my MBP to ensure I was actually starting from scratch. Following the process in this tutorial, I still get
imagick.so: Mach-O 64-bit bundle x86_64
I triple checked the .profile, variants.conf and export flags. Still no joy.
Feeling kind of stoopid about now…
Joe
Just to be sure, you do have Xcode installed? Make sure that all files you edit are under root (sudo xyz), and always check that the changes have been saved to file.
Make sure the flags from step #5 are exported correctly, eg. ‘echo $CFLAGS’ etc. for all of them. Other than this, not sure what could be the problem…
Hi Mike
Finally got it to work. I am not sure why this made a difference, in fact it bothers me that I had to do this, but when I used the OSX native phpize, it worked fine. So, after running the exports and ./configure-ing the PHP 5.2 sources, then downloading and expanding imagick-3.0.1RC2 into ~Downloads, this is what I did:
$ cd /Applications/MAMP/bin/php5.2/bin
$ chmod u+x *
$ cd ~/Downloads/imagick-3.0.1RC2
$ /usr/bin/phpize
$ ./configure –with-php-config=/Applications/MAMP/bin/php5.2/bin/php-config –with-imagick=/opt/local
$ make
…and then fire up MAMP. I did not have to make install.
Kudos to a colleague (Victor Yanagida) for the tips. Hope this helps someone else.
Joe
Hi Papa Joe,
I was experiencing the same problem here.
After some hours fighting with the terminal, I managed to compile imagick.so with support for i386. Here is what I did:
- Install libxslt with universal support (it seems it was compiled just for x86_64):
port install libxslt +universal
- Manually editing Makefile to make the flags work (export CCFLAGS… etc wasn´t working for me).
Hope it helps. Good luck!
Mines,
What OS X version do you have installed, and what version of Xcode? I’m starting to think that this has an influence on the procedure. In any case, in my procedure I mention that the +universal flag must be added BEFORE you start doing anything with Macports, otherwise you won’t get Universal versions of anything it builds, including support libs like libxslt. Thanks for your input!
This didn’t work for me unfortunately. Since we’re all using the same MAMP, OS, and Imagick couldn’t you just post the imagick.so file? I don’t know enough about it to say if it’d work, but seems like it should.
Aaron,
It’s not just imagick.so – this is just the PHP module, you need to have the actual Image Magick suite installed too. In any event it’s bad juju to use pre-compiled binaries, for a number of reasons, such as:
- Security, who knows what went into the binary.
- Library compatibility, if it’s compiled against a different version of xyz it won’t work.
You should only use binaries from trusted sources, and even then, it is better to compile from source if you can.
As I said, make sure you add the +universal flag -before- you do ‘sudo port selfupdate’ and the rest of the procedure.
Good to know that you have to add the +universal flag before you update MacPorts. It might be helpful to add that step between 2 and 3.
I have the universal build of imagick.so now but when PHP tries to load the extension I’m getting this in my error log:
Library not loaded: /opt/local/lib/libfreetype.6.dylib
Referenced from: /Applications/MAMP/bin/php5.3/lib/php/extensions/no-debug-non-zts-20090626/imagick.so
Reason: Incompatible library version: imagick.so requires version 13.0.0 or later, but libfreetype.6.dylib provides version 10.0.0 in Unknown on line 0
Any ideas?
Yes, it makes sense to have it better explained than what it is now, I’ll update the post. As for your new issue, look at step #8, that is the fix too (need to reference /opt/local/bin).
For anyone else who’s a MacPorts noob, to update run “sudo port selfupdate”.
Man, you’re the best, thank you for sharing this guide.
Step five you say “extract the archive” but you don’t to say where to extract it?
Anywhere! In my case, the archive ended up in /Downloads, so I just went there in Terminal, then tar -zxf, and cd into the directory.
man thank you so much… what a waiste of a day, I was pulling my hair out until I found this posting!!!!
Hi,
Thanks for the guide. I have followed the guide and done everything without errors.
BUT, I don’t find the imagick in phpinfo. When I check the log I get this:
[31-Oct-2010 13:20:49] PHP Warning: PHP Startup: Unable to load dynamic library ‘/Applications/MAMP/bin/php5.3/lib/php/extensions/no-debug-non-zts-20090626/imagick.so’ – dlopen(/Applications/MAMP/bin/php5.3/lib/php/extensions/no-debug-non-zts-20090626/imagick.so, 9): 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.8.dylib
in /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/Resources/libTIFF.dylib in Unknown on line 0
Steve,
Make sure you follow the extra instructions in Step 8, this looks like library compatibility issues.
Hi Mike,
Thanks for the fast reply. I had forgot to add comments to the two lines listed in step 8, but now apache wont start?
When I comment these two lines:
#DYLD_LIBRARY_PATH=”/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH”
#export DYLD_LIBRARY_PATH
and add the export PATH=”$PATH:/opt/local/bin”. It causes apache to not start. MySQL server is starting, but not apache.
Any idea what I can try?
Thanks.
Do you have any error messages in the logs? You should see the reason why Apache is not starting there.
dyld: lazy symbol binding failed: Symbol not found: _MagickWandGenesis
Referenced from: /Applications/MAMP/bin/php5.3/lib/php/extensions/no-debug-non-zts-20090626/imagick.so
Expected in: flat namespace
dyld: Symbol not found: _MagickWandGenesis
Referenced from: /Applications/MAMP/bin/php5.3/lib/php/extensions/no-debug-non-zts-20090626/imagick.so
Expected in: flat namespace
I am kind of new on this server management. Would be very thankful if you could give me a reply =)
I cannot think of anything other than installing a clean MAMP and repeating the steps – it could be that the extension did not compile as a Universal Binary, did you check that?
Thanks for the feedback. How can I delete everything(macports, imagemagick etc.)?
Thanks a lot for your work. I write and practice on my personal blog this ‘how to’ in spanish (not change the commands
)
http://bit.ly/cbxl9B
Hello Mike,
I got the same problem: ‘dyld: Symbol not found: _MagickWandGenesis’.
But it’s all clean. Maybe it’s a known 64bit problem.
hi, thanks so much for this guide! it runs now more or less, but when i try to read a png file with these commands:
$im = new Imagick();
$im->readImage( “generator/_drawings/test.png” );
i get this error:
Uncaught exception ‘ImagickException’ with message ‘no decode delegate for this image format
any idea? in the terminal i can use “convert xyz.png xyz.jpg” .
Andre,
The explanation (at least the most likely one) is here:
http://www.phwinfo.com/forum/php-general/343228-imagick-setimageformat.html
Hope it helps.
Dear Mike,
thanks so much for taking time for this. I also came along that link while researching the problem, didn’t solve it unfortunately, but after that i figured out that it didn’t read the png because i previously forced a httpd process (the process imagick initiated) to quit, because the CPU spiked so much for a long time. when i restarted the computer, the code that i pasted works. means, imagick works.
but then i ran again the code that would cause the spike, and just let it run till it finished. it takes 74 (!) seconds on my new macbook pro to execute this simple code. online, on the hosting, its done in no time. maybe an os x thing?
here’s the code, maybe somebody wants to try & tell, how long it takes.
many greetings!
andre
newImage( 2480, 3508, ‘white’, ‘png’ );
$draw = new imagickDraw();
$draw-> setFont(‘Helvetica’);
$draw-> setFontSize(40);
$draw-> setFillColor(“black”);
$canvas-> annotateImage( $draw, 0, 3505, 0, “test test 1 2 3 4″);
$canvas-> writeImage( ‘text.png’ );
echo time()-$starttime;
?>
ps: strange, it cuts the code.. it’s missing this at the beginning:
and then the rest of the prior post
sorry, i don’t know why, but one part of the code always gets filtered out. here’s an image of the code:
http://primeclub.org/stuff/slow_imagick_code.png
Andre,
You’re creating almost 9MB of image in memory, and then you are processing it, this can take time – not sure if 74 seconds, but don’t expect it to be immediate. I’ll try the code and see what happens.
Regards,
Mike
Thanks Mike !
I was expecting to take some time because it’s a hi-res A4 image, but on the hosting server the whole script takes just 1 second. If my new Macbook Pro is 74x slower, then i need to get another computer
So i guess it’s something with my Imagemagick configuration (memory? swap file? )… or a Linux/OS X thing.
Was just curious if somebody else has the same issue.
Many Greetings,
André
Having the same “MagickWandGenesis” errors here than Mike and Steve..
Any idea’s?
Hi again, I hope I don’t bother..
When I do
$ file imagick.so
imagick.so: Mach-O universal binary with 2 architectures
imagick.so (for architecture i386): Mach-O bundle i386
imagick.so (for architecture x86_64): Mach-O 64-bit bundle x86_64
So that looks good.
The Terminal log, after downloading and building imagick says: Build process completed successfully
However when I looked in more detial there are also these lines:
ld: warning: in /opt/local/lib/libMagickWand.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
ld: warning: in /opt/local/lib/libbz2.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
ld: warning: in /opt/local/lib/libz.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
ld: warning: in /opt/local/lib/libMagickCore.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
ld: warning: in /opt/local/lib/liblcms.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
ld: warning: in /opt/local/lib/libtiff.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
ld: warning: in /opt/local/lib/libjpeg.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
ld: warning: in /opt/local/lib/libfontconfig.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
ld: warning: in /opt/local/lib/libexpat.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
ld: warning: in /opt/local/lib/libfreetype.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
ld: warning: in /opt/local/lib/libiconv.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
ld: warning: in /opt/local/lib/libltdl.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
dsymutil .libs/imagick.so || :
warning: no debug symbols in executable (-arch i386)
warning: no debug symbols in executable (-arch x86_64)
Does this mean the libraries needed to install ImageMagick are not Snow Leopard 64bit compatible?
I did
$ sudo port -v selfupdate
from other tutorials before I added the
+universal in variants.conf
If this is the problem how can I re-build the necessary libraries 64bit?
Thanks so much for this page!
Day 3 on this now…
Thank you sooooo much for this post!!!
I actually tried so many things that I had to get a clean copy of macport and MAMP to start from scratch following this guide – and it’s the only one that worked.
However, one suggestion: you might want to mention in step 5 where the files should be extracted to. I’m not sure, but it only worked for me when it’s inside MAMP/bin/php5.3/bin
i am getting:
PHP Startup: imagick: Unable to initialize module
in the error log – what gives?
This typically happens when you build IM with PHP headers that don’t match the PHP version you have installed. Make sure you have the IM .so in the right PHP folder, according to the PHP version you have selected in MAMP. Without further info, I’d recommend you re-install MAMP and start the procedure I’ve listed from scratch.
Doh procedure does not work, I am running into the same issues as other people in the comments have run into…
Specifically my php error logs are telling me…
PHP Warning: PHP Startup: imagick: Unable to initialize module
Module compiled with module API=20090626, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
So is there anyway I can get the module to compiled with the correct API?
Okay, so I guess that means my module was compiled to work with php5.3 not php5.2, and when i switch to php5.3 it now shows up in phpinfo, I wonder if i can also make a build to work for php5.2 too?
Hi,
thank you so much for this instruction. It was the only one for me that worked. I tried to install IM myself, but MAMP couldn’t load it.
The clue is really to make imagick.so “+universal”.
Maybe people want to know on which Versions this instruction works (at least I wondered about it beacause there are so many instructions out there in the web and some of them are really old so that it’s maybe not clear if it still works for newer versions). Whatever, for me, this instruction worked for:
- MAMP Version 1.9.4
- MAC OS X 10.6.7
- At step 5 I downloaded the latest Version (right now its 3.1.0b1)
- The ImageMagick Version installed by MacPorts was 6.6.9-9
I don’t know if it’s important, but after “sudo port -v selfupdate” there was a recommendation to upgrade “outdated” ports: “sudo port upgrade outdated”. I did that before installing ImageMagick. It took a while, but it was OK.
Thank you again!
Michael
Thank you for this super helpful article.
I couldn’t find any basic information on installing modules with MAMP, which is pretty bad considering it’s actually a fairly specific process. I imagine this will be a shared grievance amongst many users. I’ve used this model as my basic route to installing modules and now have successfully got up and running with a fairly common setup (memcache, gmagick etc).
I now think surely the GUI could be extended to have single click download/install if they’re going to hijack your environment to the level of having a proprietary ‘envvars’ dir.
Again, this is an excellent guide. Cheers!
I remember struggling with this problem last year when I wasn’t familiar with any command line at all – I found this article and, although hardly understanding what I was doing, I managed to get Imagick to work on my MAMP. Today I just installed a new copy of MAMP and again… this article helped me out. The difference is this time I had no trouble following it at all
YOU RULE. Just so everyone knows, this is still applicable with MAMP 2.0.5
The only issue I ran into was trying to get phpize to work. Make sure you are in the second folder of `imagick-3.1.0RC1/imagick-3.1.0RC1` – also, if you run into and permission issues when running `make install`, just use `sudo make install` and you’ll be golden.
Thanks a ton!!! This needs to be at the top of Google.
After spending a long time get the exact solution great and good