Compiling on Linux
suggest changeTo compile a PHP extension in a typical Linux environment, there are a few pre-requisites:
- Basic Unix skills (being able to operate “make” and a C compiler)
- An ANSI C compiler
- The source code for the PHP extension you want to compile
Generally there are two ways to compile a PHP extension. You can statically compile the extension into the PHP binary, or compile it as a shared module loaded by your PHP binary at startup. Shared modules are more likely since they allow you to add or remove extensions without rebuilding the entire PHP binary. This example focuses on the shared option.
If you installed PHP via your package manager (apt-get install, yum install, and so on) you will need the matching -dev package, which includes the PHP headers and the phpize script. The name varies by distribution and PHP version (php-dev, php8.3-dev, and, on older releases, php7-dev or php5-dev). Search your distro’s repositories rather than guessing.
If you built PHP from source the header files most likely already exist on your system (usually in /usr/include or /usr/local/include).
Steps to compile
After you check to make sure you have all the prerequisites, necessary to compile, in place you can head over to pecl.php.net, select an extension you wish to compile, and download the tar ball.
- Unpack the tar ball (e.g.
tar xfvz yaml-2.0.0RC8.tgz) - Enter the directory where the archive was unpacked and run
phpize - You should now see a newly created
configurescript if all went well. Run./configure - Now you will need to run
make, which will compile the extension - Finally,
make installwill copy the compiled extension binary to your extension directory
The make install step will typically provide the installation path for you where the extension was copied. This is usually in /usr/lib/, for example it might be something like /usr/lib/php5/20131226/yaml.so. But this depends on your configuration of PHP (i.e. --with-prefix) and specific API version. The API number is included in the path to keep extensions built for different API versions in separate locations.
Loading the Extension in PHP
To load the extension in PHP, find your loaded php.ini file for the appropriate SAPI, and add the line extension=yaml.so then restart PHP. Change yaml.so to the name of the actual extension you installed, of course.
A Zend extension (zend_extension=) usually needs the full path to the shared object. A normal PHP extension (extension=yaml.so) is loaded from the extension_dir directory. That directory is a PHP build setting, not the shell $PATH.