Mac silicon M1 compilation and installation of php8 & swoole4.6 arm64

Time:2021-9-15

Recently, I built an M1 mac mini and was ready to use it for developmentmixphpThe V3 version has been tried several times before and has not been compiled successfully. Today, the installation attempt is successful again. There are problems with many people’s articles on the network (maybe the system environment is different). I hereby share it to let the follow-up people close the pit.

Problems faced

Because the latest version of MacOS Big Sur cannot write files even if the security mode / usr / lib is turned off, make install cannot install any PHP extensions. Therefore, if you want to install swoole, you can only compile and install PHP in the / usr / local directory

% csrutil status
System Integrity Protection status: disabled.
% mkdir /usr/lib/php/extensions/test
mkdir: /usr/lib/php/extensions/test: Read-only file system

PHP Build

Since the brew arm64 version cannot be used, only the x64 version can be used to install some dependencies, which leads to many problems later. I wanted to compile an x64 PHP + swarm, but the exception did not continue when compiling x64 swarm. Instead, I studied arm64 PHP + swarm

arch -x86_64 brew install openssl zlib curl libjpeg libpng libxml2 gettext freetype pcre libiconv libzip

The path in the parameter needs to be replaced according to the actual path installed on your computer

./configure --prefix=/usr/local/php8.0.7 --with-config-file-path=/usr/local/php8.0.7/etc --with-config-file-scan-dir=/usr/local/php8.0.7/etc/php.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --with-mhash --enable-tokenizer --enable-pcntl --enable-sockets --enable-soap --enable-simplexml --without-pear --with-pear --with-zlib=/usr/local/opt/zlib --with-curl=/usr/local/Cellar/curl/7.77.0 --with-openssl=/usr/local/Cellar/[email protected]/1.1.1k --with-iconv=/usr/local/Cellar/libiconv/1.16
make

PHP Build ERROR: No package 'openssl' found

Arm64: OpenSSL needs to be compiled and installed (libressl from the system is not allowed)

OpenSSL compilation based on MAC silicon M1:https://segmentfault.com/a/11…

sudo cp /usr/local/openssl/lib/pkgconfig/* /usr/local/lib/pkgconfig/

Continue compiling: PHP--with-opensslReplace with / usr / local / OpenSSL)

PHP Build ERROR: configure: error: Please reinstall the iconv library

Arm64: iconv needs to be compiled and installed

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar xvzf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv
make && sudo make install

Continue compiling: PHP--with-iconvReplace with / usr / local / libiconv)

PHP Build ERROR: ld: warning: ignoring file /usr/local/Cellar/oniguruma/6.9.7.1/lib/libonig.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64

Arm64: oniguruma needs to be compiled and installed

wget https://github.com/kkos/oniguruma/archive/v6.9.5_rev1.tar.gz
tar xvzf v6.9.5_rev1.tar.gz
cd oniguruma-6.9.5_rev1
./autogen.sh

Oniguruma Build ERROR: autoreconf: error: aclocal failed with exit status: 2

Need to install: automake, which can also be used if an x64 is installed

arch -x86_64 brew install automake

Continue compiling: oniguruma

./autogen.sh
./configure --prefix=/usr/local/oniguruma
make && sudo make install

Remove x64 oniguruma and associate arm64 oniguruma to the system

arch -x86_64 brew uninstall oniguruma
sudo cp /usr/local/oniguruma/lib/pkgconfig/* /usr/local/lib/pkgconfig/
sudo cp /usr/local/oniguruma/include/* /usr/local/include/
export LDFLAGS="-L/usr/local/oniguruma/lib"
export CPPFLAGS="-I/usr/local/oniguruma/include"
export PKG_CONFIG_PATH="/usr/local/oniguruma/lib/pkgconfig"
make clean
./configure --prefix=/usr/local/php8.0.7 --with-config-file-path=/usr/local/php8.0.7/etc --with-config-file-scan-dir=/usr/local/php8.0.7/etc/php.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --with-mhash --enable-tokenizer --enable-pcntl --enable-sockets --enable-soap --enable-simplexml --without-pear --with-pear --with-zlib=/usr/local/opt/zlib --with-curl=/usr/local/Cellar/curl/7.77.0 --with-openssl=/usr/local/openssl --with-iconv=/usr/local/libiconv
make

PHP Build ERROR: ld: can't write output file: sapi/phpdbg/phpdbg for architecture arm64

sudo make && sudo make install

PHP Build ERROR: PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.

Ignore this exception

PHP Build ERROR: Allocation of JIT memory failed, PCRE JIT will be disabled. This is likely caused by security restrictions. Either grant PHP permission to allocate executable memory, or set pcre.jit=0

sudo vim /Users/liujian/Downloads/php-8.0.7/ext/phar/phar.php

newly addedini_set("pcre.jit", "0");

sudo make install

Swoole Build

Add one--enable-thread-context, and specify--with-openssl-dirPath to the compiled version

wget https://github.com/swoole/swoole-src/archive/refs/tags/v4.6.7.tar.gz
tar xvzf v4.6.7.tar.gz
cd swoole-src-4.6.7
./configure --with-php-config=/usr/local/php8.0.7/bin/php-config --enable-openssl --enable-http2 --enable-thread-context --with-openssl-dir=/usr/local/openssl
make && sudo make install