Installing PHP Extensions on macOS Catalina 10.15

macOS 10.15 already comes with php located at /usr/bin/php but it is not possible to install extensions. One way to tackle this is to use homebrew which will help with not only php extensions but a whole lot more. This post is for those who would like to achieve the said goal without homebrew.

We first need XCode command line tools if they are not already installed.

xcode-select --install 

Next we need to compile and install autoconf. Below commands will install autoconf and all related files to /usr/local and subdirectories.

cd /tmp
curl -OL http://ftpmirror.gnu.org/autoconf/autoconf-2.69.tar.gz
tar xzf autoconf-2.69.tar.gz
cd autoconf-2.69
./configure --prefix=/usr/local
make
sudo make install

macOS default php is located at /usr/bin/php which is a signed binary. If we use this binary as is, we will run into the following error message:

Library Validation: mapped file has no cdhash, completely unsigned? Code has to be at least ad-hoc signed.

To bypass this issue we will create a custom php binary located at /usr/local/bin/php along with copies of phpize and php-config. We will copy the preinstalled php binary and remove its signature.

sudo cp /usr/bin/php /usr/local/bin/php
sudo codesign --remove-signature /usr/local/bin/php
sudo cp /usr/bin/phpize /usr/local/bin/phpize
sudo cp /usr/bin/php-config /usr/local/bin/php-config 

Next we need to edit /usr/local/bin/phpize and point it to correct locations.

--- /usr/bin/phpize	2020-03-01 05:36:45.000000000 +0300
+++ /usr/local/bin/phpize	2020-06-14 22:28:48.000000000 +0300
@@ -1,209 +1,210 @@
 #!/bin/sh

 # Variable declaration
+XCODE_SDK_ROOT=$(/usr/bin/xcrun --show-sdk-path)
 prefix='/usr'
 datarootdir='/usr/php'
 exec_prefix="`eval echo ${prefix}`"
 phpdir="`eval echo ${exec_prefix}/lib/php`/build"
-includedir="`eval echo ${prefix}/include`/php"
+includedir="`eval echo ${XCODE_SDK_ROOT}${prefix}/include`/php"
 builddir="`pwd`"
 SED="/usr/bin/sed"

Next we will do the same for php-config.

--- /usr/bin/php-config	2020-03-01 05:38:19.000000000 +0300
+++ /usr/local/bin/php-config	2020-06-14 22:37:47.000000000 +0300
@@ -1,43 +1,44 @@
 #! /bin/sh

+XCODE_SDK_ROOT=$(/usr/bin/xcrun --show-sdk-path)
 SED="/usr/bin/sed"
 prefix="/usr"
 datarootdir="/usr/php"
 exec_prefix="${prefix}"
 version="7.3.11"
 vernum="70311"
-include_dir="${prefix}/include/php"
+include_dir="${XCODE_SDK_ROOT}${prefix}/include/php"
 includes="-I$include_dir -I$include_dir/main -I$include_dir/TSRM -I$include_dir/Zend -I$include_dir/ext -I$include_dir/ext/date/lib"
 ldflags=" -L$SDKROOT/usr/lib -L$SDKROOT/usr/local/libressl/lib  -L/usr/local/lib"

Now we have everything ready to install pear and pecl.

curl -OL https://pear.php.net/install-pear-nozlib.phar
sudo php install-pear-nozlib.phar -d /usr/local/lib/php -b /usr/local/bin 

Edit /usr/local/bin/pecl and add the following to the top of it to make sure it uses the correct php binary.

#!/bin/sh

PHP_PEAR_PHP_BIN=/usr/local/bin/php

# first find which PHP binary to use

And add necessary paths in /etc/php.ini

include_path = ".:/usr/local/lib/php"
extension_dir = "/usr/local/lib/php/extensions"

We can now start installing php extensions such as redis and mailparse.

sudo pecl install redis
sudo pecl install mailparse

And add turn them on in php.ini

extension=redis.so
extension=mailparse.so

Installing php-zip extension requires libzip, which requires a few other steps.

First install cmake by downloading the binary distribution from https://cmake.org/download/ and installing in Applications folder.

Then make a symbolic link to the binary from /usr/local/bin

sudo ln -s /Applications/CMake.app/Contents/bin/cmake /usr/local/bin/cmake

We can now compile and install libzip

cd /tmp
curl -OL https://libzip.org/download/libzip-1.5.2.tar.gz
tar -xzf libzip-1.5.2.tar.gz
cd libzip-1.5.2
mkdir build
cd build
cmake ..
make
sudo make install

And install php zip extension

sudo pecl install zip

And turn it on in php.ini

extension=zip.so

References:

Leave a comment