Compiling PHP Extensions

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

  • The extension isn’t available on pecl (e.g. uprofiler)
  • The extension is on pecl, but you need the newest version or a branch with a particular feature or fix in it, perhaps for testing
  • You are fixing an extension yourself (yay, we need more people like you!)

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:
phpize
./configure

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
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 uprofiler.so.

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 -dextension=uprofiler.so -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!

One thought on “Compiling PHP Extensions

  1. Pingback: Compiling PHP Extensions | Advanced PHP | Scoo...

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.