Compiling PHP Extensions

Written by Lorna Jane / Original link on Aug. 29, 2014

There are lots of reasons why you might like to compile your own PHP extensions. For me those reasons are usually:

Related: If you followed my previous post on compiling PHP, be aware that in the php/bin/ folder there is a pecl binary that will install extensions correctly for whichever version of PHP it belongs to, so you may not need to read the rest of this post. However if you do, the paths follow on from the examples in that post.

I haven't seen a really approachable guide anywhere, we tend to speak of extensions in hushed tones, and actually it isn't particularly tricky so here is my quick how-to guide.

Start by grabbing the code you need. First we'll need to prepare it for compiling by explaining to it which version of PHP it is intended for.

For your default PHP install you can just run:

However for a version that isn't the default on your system, we can be a bit more specific with the paths to use:
phpize /path/to/toy/php/bin/phpize
./configure --with-php-config=/path/to/toy/php/bin/php-config

Either way, we're now all set to build and install our extension:
make install

This compiles the extension and places the binaries in the correct location (you may need to sudo make install if your user doesn't have write permission to the desired location). This process will tell you which extension file was created, such as

Check that PHP can load the extension by adding it on the command line and then viewing the modules list (beware that errors often appear at the top of this output and may scroll off screen!):

php -m

(remember to include the /path/to/toy/php for non-default installs)

You should see the extension listed in the output - you can now add it to your php.ini file or however you usually enable extensions and carry on with whatever it was you were doing!

Lorna is an independent web development consultant, author and trainer, available for work (interesting projects only). This post was originally published at LornaJane


pecl calevans lornajane krakjoe lornajane

« Including Code Samples With rst2pdf - Running Multiple Versions of PHP »