How To Compile And Install PHP Extensions From Source

Want to help support this blog? Try out Oh Dear, the best all-in-one monitoring tool for your entire website, co-founded by me (the guy that wrote this blogpost). Start with a 10-day trial, no strings attached.

We offer uptime monitoring, SSL checks, broken links checking, performance & cronjob monitoring, branded status pages & so much more. Try us out today!

Profile image of Mattias Geniar

Mattias Geniar, September 14, 2008

Follow me on Twitter as @mattiasgeniar

It’s not as difficult as it might sound, so here’s the quick-n-dirty way of installing and compiling your very own PHP extensions/libraries, from the PHP source code. With a normal install, not every library is compiled and installed – so it might leave you with several functions that aren’t working.

This guide is building a module for PHP 5.2, but the steps are identical for PHP 5.3, 5.4, 5.5, or 5.6.

In this case, I’ll assume you’re trying to compile the dBase-extension. It’ll probably start with the following error, shown on screen:

Fatal error: Call to undefined function dbase_create() in ... on line ...

It means the extension wasn’t loaded – and in most cases isn’t present on the server at all. So we’ll have to create it ourselves.

Install the PHP development package

On Ubuntu/debian, you can use apt-get, it’s a piece of cake.

$ apt-get install php5-dev

On CentOS / Red Hat, use yum for your PHP packages.

$ yum install php-devel

(alternatively: check which repositories you’re using and which PHP package you have installed, the package may also be named php55-devel, or similar).

Download & unzip the PHP5 source code

Go the the PHP-download page, and select the complete PHP5 source code.

$ cd /usr/local/src
$ mkdir php_source
$ cd php_source
$ wget

Next, unzip the file you just downloaded (the “.gz”-file extensions means it has been gzipped).

$ gunzip php-5.2.6.tar.gz

And untar it.

$ tar xvf php-5.2.6.tar

Prepare the extension (phpize)

Go to the proper directory. You just untarred the files, so we’ll browse to that specific subdirectory (note; directory-names may vary, depending on the version you just downloaded). In this case, we go to the “dbase” subfolder, but it could be any of the extensions you want to configure.

$ cd php-5.2.6/ext/dbase/

And use phpizein that directory.

$ cd /usr/local/src/php_source/php-5.2.6/ext/dbase/
$ phpize

Configuring for:
PHP Api Version:         20041225
Zend Module Api No:      20060613
Zend Extension Api No:   220060519

Configure & Make the extension

Configure it

$ ./configure

And make it

$ make

The make command, will render a lot of text (compilation-info), and most likely end with the following snippet.


Build complete.
(It is safe to ignore warnings about tempnam and tmpnam).

Extension is now created in ./.libs/

Move it to the extensions directory

Woohoo, we compiled our dbase-extension! Now all we need to do, is activate it.

Move the extension

If you already have an extensions-directory on your server, use that one – if not, you can create a new directory to place the extensions. To find out, check the extension_dir directive.

$ php -i | grep extension_dir
extension_dir => /usr/lib/php/extensions/no-debug-non-zts-20121212

Copy the extension to that directory.

$ cp libs/ /usr/lib/php/extensions/no-debug-non-zts-20121212/

Edit your PHP.INI

The extension is ready to be included – now we need to change the php.ini file to load that particular extension. First, find out which php.ini your system is using (note: the CLI may load a different config than the php-fpm or the Apache module one, a phpinfo() in your application would tell you for certain).

$ php -i | grep 'Configuration File'
Configuration File (php.ini) Path => /etc
Loaded Configuration File => /etc/php.ini

In the example above, we use /etc/php.ini as our base php.ini file.

$ vi /etc/php.ini

Add the module you want to load. I usually add these at the very bottom, where my changes are. This is relative to the extension_dir directive you found above.

Restart your php

In case of Apache

$ /etc/init.d/httpd restart
$ /etc/init.d/apache2 restart

or php-fpm:

$ /etc/init.d/php-fpm restart

And it’s as easy as that!

Want to subscribe to the cron.weekly newsletter?

I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.

It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.

No spam. Just some good, practical Linux & open source content.