Home > Tutorials > PHP > Installing ImageMagick & Imagick PHP Extension On Dreamhost
Permalink to Installing ImageMagick & Imagick PHP Extension On Dreamhost

Installing ImageMagick & Imagick PHP Extension On Dreamhost

by on 02.27.2009 | 23 comments

I have recently been installing ImageMagick on my server for use on my other site Celeb O Rama, but because I am on Dreamhost I [...]

I have recently been installing ImageMagick on my server for use on my other site Celeb O Rama, but because I am on Dreamhost I can’t just install what I like, like you would on a server that you own. So by using the Dreamhost Wiki I installed ImageMagick, but there are no instructions on how to install Imagick. The following is how to install them both on Dreamhost.

First though it should be noted that if you are using PHP 5.3 there is a much easier way to install IMagick, check out the Dreamhost Wiki for more info. Also if you are using Nginx and PHP 5.2 on Dreamhost you only need to create a file called .php-ini in your home folder with the extra PHP.ini instructions in, again more on the Dreamhost Wiki.

First to install ImageMagick:

wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz
tar zxvf ImageMagick.tar.gz
cd ImageMagick-<version>
./configure --prefix=$HOME/local -with-gslib --with-gs-font-dir=/usr/share/fonts/type1/gsfonts/ --without-perl
make
make install

Usually you would use --with-perl-options but for some reason versions of ImageMagick after 6.4.5 don’t seem to install perlmagick correctly on Dreamhost’s servers. You don’t really need it when using ImageMagick with PHP anyway.

Onto Installing Custom PHP5

Ok, this is the tricky bit. To install Imagick you need to have a custom PHP installation. If you don’t you won’t be able to install Imagick. The main reason that I come across for this (since you could just make a copy of the PHP.ini instead) is that Dreamhost’s version of php-config, which Imagick uses to find PHP’s version, still says PHP 4 instead of 5.2.5. This means Imagick won’t install since it requires 5.1.2 and up, I think. I know it has to be higher than PHP 5.

So here is how to do it. This is also available from the Dreamhost wiki. Copy the following script which was written by Skwerl. Credits are at the top of the shell script. Oh, and don’t forget to change the domain to the domain you want to use the custom PHP install with:

#!/bin/sh

# Script updated 2008-09-23 by Skwerl (antiquiet.com) to work with dreamhost's debian etch
# Script updated 2007-11-24 by Andrew (ajmconsulting.net) to allow 3rd wget line to pass
# Script updated 2006-12-25 by Carl McDade (hiveminds.co.uk) to allow memory limit and freetype
#
# LIBMCRYPT version information (was set as static download file name previously.)
#
# Save the code to a file as *.sh
# Abort on any errors
#
set -e

# The domain in which to install the PHP CGI script.
export DOMAIN="DOMAIN.TLD"

# Where do you want all this stuff built? I'd recommend picking a local
# filesystem.
# ***Don't pick a directory that already exists!***  We clean up after
# ourselves at the end!
SRCDIR=${HOME}/source

# And where should it be installed?
INSTALLDIR=${HOME}/php5

# Set DISTDIR to somewhere persistent, if you plan to muck around with this
# script and run it several times!
DISTDIR=${HOME}/dist

# Pre-download clean up!!!!
rm -rf $SRCDIR $DISTDIR

# Update version information here.
PHP5="php-5.2.17"
LIBICONV="libiconv-1.11"
LIBMCRYPT="libmcrypt-2.5.7"
LIBXML2="libxml2-2.6.32"
LIBXSLT="libxslt-1.1.24"
MHASH="mhash-0.9.7.1"
ZLIB="zlib-1.2.5"
CURL="curl-7.14.0"
LIBIDN="libidn-0.6.8"
CCLIENT="imap-2004g"
CCLIENT_DIR="imap-2004g" # Another pest!
FREETYPE="freetype-2.2.1"
OPENSSL="openssl-0.9.8i"

# What PHP features do you want enabled?
PHPFEATURES="--prefix=${INSTALLDIR} \
 --with-config-file-path=${INSTALLDIR}/etc/php5/${DOMAIN} \
 --enable-fastcgi \
 --enable-force-cgi-redirect \
 --with-xml \
 --with-libxml-dir=${INSTALLDIR} \
 --with-freetype-dir=${INSTALLDIR} \
 --enable-soap \
 --with-openssl=${INSTALLDIR} \
 --with-mhash=${INSTALLDIR} \
 --with-mcrypt=${INSTALLDIR} \
 --with-zlib-dir=${INSTALLDIR} \
 --with-jpeg-dir=/usr \
 --with-png-dir=/usr \
 --with-gd \
 --enable-gd-native-ttf \
 --enable-memory-limit \
 --enable-ftp \
 --enable-exif \
 --enable-sockets \
 --enable-wddx \
 --with-iconv=${INSTALLDIR} \
 --enable-sqlite-utf8 \
 --enable-calendar \
 --with-curl=${INSTALLDIR} \
 --enable-mbstring \
 --enable-mbregex \
 --enable-bcmath \
 --with-mysql=/usr \
 --with-mysqli \
 --without-pear \
 --with-gettext \
 --with-imap=${INSTALLDIR} \
 --without-imap-ssl"

# ---- end of user-editable bits. Hopefully! ----

# Push the install dir's bin directory into the path
export PATH=${INSTALLDIR}/bin:$PATH

# set up directories
mkdir -p ${SRCDIR}
mkdir -p ${INSTALLDIR}
mkdir -p ${DISTDIR}
cd ${DISTDIR}

# Get all the required packages
wget -c http://us.php.net/get/${PHP5}.tar.gz/from/this/mirror
mv mirror ${PHP5}.tar.gz
wget -c http://mirrors.usc.edu/pub/gnu/libiconv/${LIBICONV}.tar.gz
wget -c ftp://ftp.uni-weimar.de/pub/unix/${LIBMCRYPT}.tar.gz
wget -c http://xmlsoft.org/sources/old/${LIBXML2}.tar.gz
wget -c http://xmlsoft.org/sources/${LIBXSLT}.tar.gz
wget -c http://iweb.dl.sourceforge.net/project/mhash/mhash/0.9.7.1/${MHASH}.tar.gz
wget -c http://www.zlib.net/${ZLIB}.tar.gz
wget -c http://curl.haxx.se/download/${CURL}.tar.gz
wget -c http://kent.dl.sourceforge.net/sourceforge/freetype/${FREETYPE}.tar.gz
wget -c ftp://alpha.gnu.org/pub/gnu/libidn/${LIBIDN}.tar.gz
wget -c ftp://ftp.cac.washington.edu/imap/old/${CCLIENT}.tar.Z
wget -c http://www.openssl.org/source/${OPENSSL}.tar.gz

echo ---------- Unpacking downloaded archives. This process may take several minutes! ----------

cd ${SRCDIR}
# Unpack them all
echo Extracting ${PHP5}...
tar xzf ${DISTDIR}/${PHP5}.tar.gz
echo Done.
echo Extracting ${LIBICONV}...
tar xzf ${DISTDIR}/${LIBICONV}.tar.gz
echo Done.
echo Extracting ${LIBMCRYPT}...
tar xzf ${DISTDIR}/${LIBMCRYPT}.tar.gz
echo Done.
echo Extracting ${LIBXML2}...
tar xzf ${DISTDIR}/${LIBXML2}.tar.gz
echo Done.
echo Extracting ${LIBXSLT}...
tar xzf ${DISTDIR}/${LIBXSLT}.tar.gz
echo Done.
echo Extracting ${MHASH}...
tar xzf ${DISTDIR}/${MHASH}.tar.gz
echo Done.
echo Extracting ${ZLIB}...
tar xzf ${DISTDIR}/${ZLIB}.tar.gz
echo Done.
echo Extracting ${CURL}...
tar xzf ${DISTDIR}/${CURL}.tar.gz
echo Done.
echo Extracting ${LIBIDN}...
tar xzf ${DISTDIR}/${LIBIDN}.tar.gz
echo Done.
echo Extracting ${CCLIENT}...
uncompress -cd ${DISTDIR}/${CCLIENT}.tar.Z |tar x
echo Done.
echo Extracting ${FREETYPE}...
tar xzf ${DISTDIR}/${FREETYPE}.tar.gz
echo Done.
echo Extracting ${OPENSSL}...
tar xzf ${DISTDIR}/${OPENSSL}.tar.gz
echo Done.

# Build them in the required order to satisfy dependencies.

#libiconv
cd ${SRCDIR}/${LIBICONV}
./configure --enable-extra-encodings --prefix=${INSTALLDIR}
# make clean
make
make install

#libxml2
cd ${SRCDIR}/${LIBXML2}
./configure --with-iconv=${INSTALLDIR} --prefix=${INSTALLDIR}
# make clean
make
make install

#libxslt
cd ${SRCDIR}/${LIBXSLT}
./configure --prefix=${INSTALLDIR} \
 --with-libxml-prefix=${INSTALLDIR} \
 --with-libxml-include-prefix=${INSTALLDIR}/include/ \
 --with-libxml-libs-prefix=${INSTALLDIR}/lib/
# make clean
make
make install

#zlib
cd ${SRCDIR}/${ZLIB}
./configure --shared --prefix=${INSTALLDIR}
# make clean
make
make install

#libmcrypt
cd ${SRCDIR}/${LIBMCRYPT}
./configure --disable-posix-threads --prefix=${INSTALLDIR}
# make clean
make
make install

#libmcrypt lltdl issue!!
cd  ${SRCDIR}/${LIBMCRYPT}/libltdl
./configure --prefix=${INSTALLDIR} --enable-ltdl-install
# make clean
make
make install

#mhash
cd ${SRCDIR}/${MHASH}
./configure --prefix=${INSTALLDIR}
# make clean
make
make install

#freetype
cd ${SRCDIR}/${FREETYPE}
./configure --prefix=${INSTALLDIR}
# make clean
make
make install

#libidn
cd ${SRCDIR}/${LIBIDN}
./configure --with-iconv-prefix=${INSTALLDIR} --prefix=${INSTALLDIR}
# make clean
make
make install

#cURL
cd ${SRCDIR}/${CURL}
./configure --with-ssl=${INSTALLDIR} --with-zlib=${INSTALLDIR} \
  --with-libidn=${INSTALLDIR} --enable-ipv6 --enable-cookies \
  --enable-crypto-auth --prefix=${INSTALLDIR}
# make clean
make
make install

# c-client
cd ${SRCDIR}/${CCLIENT_DIR}
make -i ldb SSLTYPE=none
# Install targets are for wusses!
cp c-client/c-client.a ${INSTALLDIR}/lib/libc-client.a
cp c-client/*.h ${INSTALLDIR}/include

#OpenSSL
cd ${SRCDIR}/${OPENSSL}
./config --prefix=${INSTALLDIR} --openssldir=${INSTALLDIR}
make
make install

echo - about to build php5...
echo
read -p  "(Press any key to continue)" temp;
echo

#PHP 5
cd ${SRCDIR}/${PHP5}
./configure ${PHPFEATURES}
# make clean
make
make install

#copy config file
mkdir -p ${INSTALLDIR}/etc/php5/${DOMAIN}
cp ${SRCDIR}/${PHP5}/php.ini-dist ${INSTALLDIR}/etc/php5/${DOMAIN}/php.ini

#copy PHP CGI
mkdir -p ${HOME}/${DOMAIN}/cgi-bin
chmod 0755 ${HOME}/${DOMAIN}/cgi-bin
cp ${INSTALLDIR}/bin/php ${HOME}/${DOMAIN}/cgi-bin/php.cgi
rm -rf $SRCDIR $DISTDIR
echo ---------- INSTALL COMPLETE! ----------

Copy that into clipboard open an SSH connection & do vi php5-skwerl.sh and paste in the code (press ‘i’ then right click to paste). Then save the file by pressing ESC then :wq (that’s ‘Colon’, ‘w’ then ‘q’) then press enter. Use chmod +x php5-skwerl.sh to make the script executable. Finally run the script using ./php5-skwerl.sh. DO NOT copy the code into notepad & save it then upload it, it’s alot easier to use vi.

During the installation you may get a little message warning about unencrpted passwords, I can’t remember exactly but as long as you don’t want to use openSSL you can either say yes or no. Since I have no use for SSL or openSSL I just said no.

Once the code has finished, it took around 30-40 minutes for me, then you will need to tell your server to use your PHP installation. To do this add this to your .htaccess file.

Options +ExecCGI
AddHandler php-cgi .php
Action php-cgi /cgi-bin/php.cgi

<FilesMatch "^php5?\.(ini|cgi)$">
Order Deny,Allow
Deny from All
Allow from env=REDIRECT_STATUS
</FilesMatch>

Then give a PHP page a refresh & see what happens, chances are you’ll get a 500 or Internal Server Error. If you do, don’t panic, just run this from the SSH command line (bash).

cp ~/php5/bin/php-cgi ~/DOMAIN.TLD/cgi-bin/php.cgi

Change DOMAIN.TLD to the domain you chose to install the custom PHP installation too. All should be fine after that.

Installing Imagick

Now for the bit which isn’t covered in the Dreamhost wiki. Installing Imagick. It took a little bit of time but I managed to get it installed. You first need to download, compile & install autoconf & then Imagick.

mkdir ~/source
cd ~/source
wget ftp://ftp.gnu.org/gnu/autoconf/autoconf-2.63.tar.gz
tar xzf autoconf-2.63.tar.gz
cd autoconf-2.63
./configure --prefix=$HOME/source
nice -n 19 make
make install
export PATH=/home/{username}/source/bin:$PATH
cd ~/
wget http://pecl.php.net/get/imagick-2.2.2.tgz
tar zxvf imagick-2.2.2.tgz
cd imagick-2.2.2
phpize
./configure -prefix=$HOME/imagick --with-imagick=$HOME/local --with-php-config=$HOME/php5/bin/php-config
make
make install

Don’t forget to change {username} on line 9 to your username or you’ll get a error.

Phew! After that it should tell you the path to imagick.so which you need to instuct your PHP.ini to find. So let’s do that. First either write down the path given or copy it to clipboard. In Putty you do that just by highlighting the text in the window.

cd ~/
vi ~/php5/etc/php5/DOMAIN.TLD/php.ini

scroll all the way down until you find extension_dir='./' and change it to the FULL path to the folder given by the build of imagick before. If you have it in clipboard press i then delete ./ and right click. Now find the list of extensions which look like ‘;extension=thing_some.dll’ and after the last one add ‘extension=imagick.so’. Now press ESC then colon ‘w’ ‘q’ and enter to quit and save.

Once you’ve done all that, make a php file with phpinfo() in it and go down and see if you can find this

And Relax

Yup, that’s it. It takes about 1 hour maybe 1 hour 30. It’s worth it if you don’t like to use exec() in your scripts, also Imagick supports PHP’s try, catch exception system so you can easily output nice errors instead of guessing what’s happened when exec fails. For a full list of imagick, imagickDraw, imagickPixel and all the other imagick commands look at php.net.

Hope that helps someone, the instructions can be applied to any server really, but you will need to change certain options since these ones are specifically for Dreamhost since write access to /usr/ is denied for obvious reasons. Any problems or questions feel free to ask.

The shell script & instructions for installing php5 & the info for installing imagemagick have been used from the Dreamhost wiki since they are correct & no modifications need to be made for them to work. Credit for those parts go to the authors from the Wiki. The imagick installation was written by me since no documentation for installing imagick exists in the Dreamhost wiki.

Written by Paul Robinson

A Web coder in languages such as CSS, X/HTML, jQuery, but mostly PHP. Addicted to Girls Aloud, Jennifer Morrison, Carah Faye Charnow, TV Show Chuck, and completely in love with Yvonne Strahovski's smile.

Give something back!

If you LOVED this tutorial and would like to show your appreciation, please consider or a little something from our Amazon Wishlist.

Discussion: 23 Comments

  1. Aug 31st, 2009 @ 18:59:28

    Thanks for the tut! This worked like a charm to get Imagick support working on Dreamhost!

    Just a tip for fellow dreamhosters – this method doesn’t not appear to enable TIFF support, if you need it, I’d recommend installing libtiff from source in your home directory and linking the ImageMagick build against it during the first step.

    A few other uncommon formats that might require this step: RSVG, JBIG, JPEG-2000, GhostScript – basically a ./configure on your ImageMagick source should tell you what libs were available.


  2. Aug 31st, 2009 @ 19:03:04

    Thanks for that. I didn’t need TIFF support so I never realised that. Hopefully you’ve just helped anyone who needs it. :D


  3. Oct 14th, 2009 @ 16:58:56

    hi guys, thanks you very much for this post.. i am really thank full to you because. i have visited 50 sites but not get any solution for installing imagick library on shared server. but i have done with installed by your help……thank you very much…
    Prince Thomas


    • Oct 14th, 2009 @ 17:40:59

      No problem, glad we were able to help you. :)


  4. Jul 28th, 2010 @ 22:30:45

    Thanks to your instructions I was successfully able to get imagick working on my Dreamhost account. I am, however, experiencing a problem converting .pdf files into images. I’m assuming this has to do with Ghostscript. I have checked the version on DH and they are using 6.2, which should at least work with the current build, however when I try to load a pdf for conversion I get errors. Is there something I need to do to point imagick to the gs? Does imagick require a more recent version of GS?


    • Jul 28th, 2010 @ 23:19:23

      I’ll answer my own question here – it seems the particular function I was using is only available in Image Magick 6.3.8 and the version install on DH is 6.3.7. This leads me to ask another question, I did install a current version as per your instructions above, but it seems my install is not getting picked up, how do I point imagick to my local version?


    • Jul 29th, 2010 @ 00:03:04

      I actually don’t use imagick any more as I tend to use the cli version instead so I can’t test this out, but the –with-imagick parameter tells imagick where your imagemagick install is. You will need to adjust it if your path is different.

      It’s kind of a misnomer, but according to the documentation that is the config option you need to change if imagemagick is installed at a path other than the default system path.

      If that doesn’t work or the install paths are the same, I’m not sure? I’ve always found it works okay if you keep the -with–imagick option the same as the prefix used when installing imagemagick.


  5. Aug 3rd, 2010 @ 13:34:17

    Hi I need your help. I followed the script and run it from my server and I got this error :


    [ps17015]$ ./php5-skwerl.sh
    –05:25:29– http://us.php.net/get/php-5.2.5.tar.gz/from/this/mirror
    => `mirror’
    Resolving us.php.net… 208.69.120.58
    Connecting to us.php.net|208.69.120.58|:80… connected.
    HTTP request sent, awaiting response… 404 Not Found
    05:25:30 ERROR 404: Not Found.

    Hoping for your favorable response.


    • Aug 3rd, 2010 @ 17:18:19

      Hi,

      The error is exactly what it looks like, php.net no longer host 5.2.5 on their servers. The lowest binary I can find is 5.2.12 so all you need to do is change:

      PHP5="php-5.2.5"
      

      to

      PHP5="php-5.2.12"
      

      and you should be okay.

      Hope that helps.


    • Aug 4th, 2010 @ 12:03:12

      while installing Imagick running the ff lines, I got each result below :

      ./configure -prefix=$HOME/imagick –with-imagick=$HOME/local –with-php-config=$HOME/php5/bin/php-config

      checking ImageMagick MagickWand API configuration program… configure: error: not found. Please provide a path to MagickWand-config or Wand-config program.

      make

      make: *** No targets specified and no makefile found. Stop.

      make install

      make: *** No rule to make target `install’. Stop.

      Hoping for your soonest and favorable response


    • Aug 4th, 2010 @ 12:16:20

      That error would say to me that imagemagick isn’t in the location you have set. Is your MagickWand-config & Wand-config files inside ~/local/bin?

      If they are I’m not sure what is going on. I’m no expert on Unix systems, these scripts/commands are just the ones that worked for me on Dreamhost’s systems.


    • Aug 4th, 2010 @ 14:32:54

      Thank you for this. I have installed it successfully now.

      Just one thing, when installing custom php under a domain.tld, will it also be applicable on it’s subdomain like subdomain.domain.tld? Or do I need to install a custom php under the subdomain and how to do it?

      Thank You


    • Aug 4th, 2010 @ 14:44:05

      No problem.

      Re the subdomains, I believe you may need to symbolic link the php binary to the cgi-bin. There should be info on how to do that on the install PHP5 page of the dreamhost wiki here.

      Hope that helps.


  6. Mar 30th, 2011 @ 23:40:58

    any idea how to install Imagick on dreamhost with php 5.3.5? It is not the same since php 5.3 has much simpler way of using custom php


    • Mar 30th, 2011 @ 23:54:09

      Sorry Milan, I honestly have no idea. I haven’t yet had a chance to try on PHP 5.3.5. Is there nothing on the Dreamhost wiki at http://wiki.dreamhost.com?


  7. Mar 31st, 2011 @ 00:03:45

    there is how to make custom php and it looks easier. But I didnt managed to make it work with imagick. Check it out and let me know if yo uhave an idea. I need that imagick badly because i am running sponsored photocontest and uploaded photos dont look good with GD library. Need Imagick :(

    http://wiki.dreamhost.com/PHP.ini


    • Mar 31st, 2011 @ 01:51:28

      I can’t be sure as I haven’t had a chance to try it but it would probably go something like this.

      First follow the instructions on the PHP.ini page on Dreamhost for setting up a custom ini file for PHP 5.3. Then try this.

      wget http://pecl.php.net/get/imagick-3.0.1.tgz
      tar zxvf imagick-3.0.1.tgz
      cd imagick-3.0.1
      /usr/local/php53/bin/phpize
      ./configure -prefix=$HOME/imagick --with-imagick=$HOME/local --with-php-config=/usr/local/php53/bin/php-config
      make
      

      Make sure you change the path on the --with-imagick to the path to your imagemagick install or it won’t work. If using a normal Dreamhost install of Imagemagick it is /usr/bin I believe. Then you would copy the output .so file into your .php/53 directory that you have created for the custom PHP. Then add:

      extension = /home/username/.php/5.3/mymodule.so
      

      Into the phprc file the Dreamhost wiki told you to make. Obviously replace ‘username’ & ‘mymodule’ with your username & the name of the imagick module respectively.

      Hopefully that will work. Again I can’t be sure as I’m not yet ready to upgrade to 5.3, but let me know how it goes if you can.


    • Mar 31st, 2011 @ 13:15:30

      I did it.
      Combined the two instructions. First installed imagemagick like on this page. Then used this link:
      http://wiki.dreamhost.com/PHP.ini
      to create custom php. Phprc method didnt work. I just copied the original php.ini to .php/5.3/ folder and it automaticaly used that php.ini instead of original. Then I installed imagick using the instructions here and copied imagick.so to .php/5.3/ folder and created a path to imagick.so in my new php.ini and it worked.
      Thats it in short :)


    • Mar 31st, 2011 @ 14:33:40

      Not sure why the phprc didn’t work, but great job on managing to get it working. :)

      I was hoping it would be similar in some ways to the older way.


  8. Jan 5th, 2012 @ 03:39:55

    Here’s a copy of the script with updated links. I had to change a couple things to get everything to work: http://pastebin.com/eL47HENL


    • Jan 5th, 2012 @ 18:28:17

      Thanks Jake,

      If you don’t mind, I’ll update the code in the post with your version. :)


Leave a comment

Please enclose code in [lang] tags. For example [php] echo 'hello world'; [/php]

* Name, Email, Comment are Required