Apple Silicon Mac下pecl安装memcached时ZLIB头文件查找失败如何解决?
问题描述
我在搭载Apple Silicon芯片的Mac上,已通过Homebrew安装了PHP、zlib和memcached。尝试使用命令sudo pecl install memcached安装PHP的memcached扩展,当提示输入zlib directory [no] :时,我指定了路径"/opt/homebrew/opt/zlib/include"。我已确认该目录下存在zlib.h和zconf.h文件,brew info zlib也给出了如下环境变量配置提示:
For compilers to find zlib you may need to set:
export LDFLAGS="-L/opt/homebrew/opt/zlib/lib"
export CPPFLAGS="-I/opt/homebrew/opt/zlib/include"For pkg-config to find zlib you may need to set:
export PKG_CONFIG_PATH="/opt/homebrew/opt/zlib/lib/pkgconfig"
但执行sudo pecl install memcached时仍抛出错误:
checking whether to use system FastLZ library... no checking for ZLIB... yes, shared checking for pkg-config... /opt/homebrew/bin/pkg-config configure: error: Can't find ZLIB headers under "/opt/homebrew/opt/zlib/include" ERROR: `/private/tmp/pear/temp/memcached/configure --with-php-config=/opt/homebrew/opt/php/bin/php-config --with-libmemcached-dir=no --with-zlib-dir=/opt/homebrew/opt/zlib/include --with-system-fastlz=no --enable-memcached-igbinary=no --enable-memcached-msgpack=no --enable-memcached-json=no --enable-memcached-protocol=no --enable-memcached-sasl=no --enable-memcached-session=no' failed
解决步骤
1. 保留环境变量执行安装
sudo会默认重置环境变量,导致brew info zlib提示的配置无法生效。先在当前Shell中导出所需环境变量,再用sudo -E保留这些变量执行安装:
export LDFLAGS="-L/opt/homebrew/opt/zlib/lib" export CPPFLAGS="-I/opt/homebrew/opt/zlib/include" export PKG_CONFIG_PATH="/opt/homebrew/opt/zlib/lib/pkgconfig" sudo -E pecl install memcached
当提示输入zlib directory [no] :时,直接回车使用默认值即可,环境变量会帮编译程序找到zlib路径。
2. 调整zlib路径参数
如果上述方法无效,在安装过程中提示zlib directory [no] :时,输入/opt/homebrew/opt/zlib(zlib的根目录,而非include子目录),编译脚本会自动在该目录下查找include和lib文件夹。
3. 手动编译安装(备选方案)
如果PECL安装仍失败,可手动下载源码编译:
- 下载memcached扩展源码:
pecl download memcached - 解压并进入源码目录:
tar -xzf memcached-*.tgz && cd memcached-* - 初始化编译环境并配置:
phpize ./configure --with-php-config=/opt/homebrew/opt/php/bin/php-config --with-zlib-dir=/opt/homebrew/opt/zlib - 编译并安装:
make && sudo make install - 最后在PHP配置文件(如
/opt/homebrew/etc/php/{版本号}/php.ini)中添加extension=memcached.so,重启PHP服务即可。
内容的提问来源于stack exchange,提问作者tinkerr

