Building PHP on Mac OS X is pretty straightforward, though you might encounter a hiccup or two along the way.
For any number of reasons you may find yourself needing to build PHP from source. Perhaps you need to match a client’s version for a particular project. Or you maybe want to test against different versions before releasing some code. Or maybe you’re missing some extensions in your current build.
Whatever the reason, fortunately it’s pretty easy, though there are a few potential pitfalls along the way.
Here we’re building PHP 5.4.11 on Mac OS X 10.7.5.
Prerequisites
To build PHP you’ll need command-line C development tools installed on your Mac. If you’ve done any Mac development, or built open source code before you may already have Apple’s XCode installed. If you don’t, go to https://developer.apple.com/xcode/index.php to install the free download.
You also, rather obviously, need the PHP source bundle. You can download the sources for the version you need from http://php.net/downloads.php. Extract these to a working directory on your Mac.
Configuring
Building PHP follows the familiar configure – make – make install chain used by many open source projects.
From the directory where you extracted the PHP sources, run the following command:-
./configure --help
This will show you the many options you can tweak to tailor your build. If you’re building in order to enable a specific feature, search through the list for the options you need, or check out http://www.php.net/manual/en/configure.about.php for details about commonly required options.
For example, to build PHP for Apache HTTPD with both libmysql and mysqlnd support, you would need to set at least the following options:-
–with-apxs2 | Set to the full path of the bin/apxs file in your Apache HTTPD installation. |
–prefix | Set to the directory you want to install your PHP build into. |
–with-mysql | Set to the location of your MySQL installation. |
–with-mysqli | Enables the MySQL native driver. |
For example:-
./configure --with-apxs2=/Users/devsumo/tools/httpd-2.4.3/bin/apxs --prefix /Users/devsumo/tools/php-5.4.11 --with-mysql=/Users/devsumo/tools/mysql-5.5.28 --with-mysqli
The configure command checks all required prerequisites are present on your system and configures the build for the OS X platform. If it fails you’ll need to locate the particular prerequisites missing from your system and install them.
Building
Once you’ve successfully run the configure command, next up run the following:-
make
This will build all the PHP code for you.
If you see an error like the following:-
Undefined symbols for architecture x86_64: "_res_9_init", referenced from: _zif_dns_get_mx in dns.o _zif_dns_get_record in dns.o _zif_dns_check_record in dns.o "_res_9_search", referenced from: _zif_dns_get_mx in dns.o _zif_dns_get_record in dns.o _zif_dns_check_record in dns.o "_res_9_dn_skipname", referenced from: _zif_dns_get_mx in dns.o _zif_dns_get_record in dns.o "_res_9_dn_expand", referenced from: _zif_dns_get_mx in dns.o _php_parserr in dns.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status make: *** [libs/libphp5.bundle] Error 1
Run the following command and repeat the make step:-
export LDFLAGS=-lresolv make
If you see an error like the following:-
dyld: Library not loaded: libmysqlclient.18.dylib
This means your MySQL dynamic libraries can’t be located. These are typically in the lib sub-folder of your MySQL installation – make sure the file is there and then include that folder in the DYLD_LIBRARY_PATH environment variable and run the make step again.:-
export DYLD_LIBRARY_PATH=/Users/devsumo/tools/mysql-5.5.28/lib:$DYLD_LIBRARY_PATH make
Installing
Once make completes with no errors, you can run the following command to install PHP:-
make install
Note that this command will copy libphp5.so into your Apache HTTPD installation’s modules folder for you, so if you want to keep a copy of the existing one rename it before running this step.
If make install give you an error like this:-
cp: ext/phar/phar.phar: No such file or directory make: *** [install-pharcmd] Error 1
You may need to run the following command and then repeat the build, as per this article (https://bugs.php.net/bug.php?id=49890)
export LC_ALL=en_US.UTF-8 make clean make make install
Testing
The PHP build comes with a suite of over 9000 regression tests that you can run to get some confidence that your build is in good shape. Run the following command and go for a coffee, it’ll take a good quarter of an hour or so to run:-
make test
If this throws up one or two isolated issues it’s probably nothing to worry about for development use but you might want to investigate further before using it in production.
Running
All you need to do now is create yourself a php.ini for your new build in the lib folder of your installation directory. You can base this on php.ini-production or php.ini-development which come with the source bundle.
Since make install copied the PHP library into your Apache HTTPD instance, all you need to do now is restart it and you should be working with your new PHP build. A quick phpinfo() check should show you that all’s well
<?php phpinfo(); ?>
A little over 11 years later, finding myself building PHP 8.3.3 on macOS Ventura 13.2, it was a little surprising to find the “Building” steps above still necessary to get to a clean build.
One new curiosity though, making the Zend packages threw up this error:-
Zend/../main/php_config.h:2310:10: fatal error: 'stdlib.h' file not found
Not dug into the root cause of this, but I found that setting the following environment variable got me past this issue:-
export INCLUDE=/Library/Developer/CommandLineTools/usr/include/c++/v1