安装 Valet

安装 Valet 环境时,照着 Laravel 官方文档进行安装即可。

安装 Valet 还是比较顺利的,接下来就是安装项目所需的扩展:

  • redis
  • mongodb
  • swoole

安装扩展遇到的问题

安装 redis 扩展

pecl install redis
.....
enable igbinary serializer support? [no] : yes
enable lzf compression support? [no] : yes
enable zstd compression support? [no] : yes

可以看到安装 redis 扩展需要上面三个依赖,当然你也可以都选择 no,我这里都选了 yes,然后失败了。

$ pecl install igbinary lzf zstd

安装完所需的依赖扩展以后,再次安装 redis

$ pecl install redis
......
checking for igbinary includes... /opt/homebrew/Cellar/php/8.1.3/include/php
checking for redis igbinary support... enabled
checking for pkg-config... no
checking for libzstd files in default path... not found
configure: error: Please reinstall the libzstd distribution
ERROR: `/private/tmp/pear/temp/redis/configure --with-php-config=/opt/homebrew/opt/php/bin/php-config --enable-redis-igbinary=yes --enable-redis-lzf=yes --enable-redis-zstd=yes' failed

又抱错了,从错误信息上来看应该是没有找到 zstd 的相关文件,但是我看了 Homebrew 确实是安装了这个包的。

然后我去这个包的项目仓库中查看了 config.m4 的相关上下文:

  if test "$PHP_REDIS_ZSTD" != "no"; then
    AC_DEFINE(HAVE_REDIS_ZSTD, 1, [ ])

    if test "$PHP_LIBZSTD" = "yes" && test -x "$PKG_CONFIG" && $PKG_CONFIG --exists libzstd; then
      AC_MSG_CHECKING(for libzstd using pkg-config)

      LIBZSTD_VER=`$PKG_CONFIG libzstd --modversion`
      if $PKG_CONFIG libzstd --atleast-version 1.3.0; then
        LIBZSTD_INC=`$PKG_CONFIG libzstd --cflags`
        LIBZSTD_LIB=`$PKG_CONFIG libzstd --libs`
        AC_MSG_RESULT(found version $LIBZSTD_VER)
        PHP_EVAL_LIBLINE($LIBZSTD_LIB, REDIS_SHARED_LIBADD)
        PHP_EVAL_INCLINE($LIBZSTD_INC)
      else
        AC_MSG_ERROR([found version $LIBZSTD_VER, version 1.3.0 required])
      fi

    elif test "$PHP_LIBZSTD" != "no"; then
      AC_MSG_CHECKING(for libzstd files in default path)
      for i in $PHP_LIBZSTD /usr/local /usr; do
        if test -r $i/include/zstd.h; then
          AC_MSG_RESULT(found in $i)
          LIBZSTD_DIR=$i
          break
        fi
      done
      if test -z "$LIBZSTD_DIR"; then
        AC_MSG_RESULT([not found])
        AC_MSG_ERROR([Please reinstall the libzstd distribution])
      fi
      PHP_CHECK_LIBRARY(zstd, ZSTD_getFrameContentSize,
      [
        PHP_ADD_LIBRARY_WITH_PATH(zstd, $LIBZSTD_DIR/$PHP_LIBDIR, REDIS_SHARED_LIBADD)
      ], [
        AC_MSG_ERROR([could not find usable libzstd, version 1.3.0 required])
      ], [
        -L$LIBZSTD_DIR/$PHP_LIBDIR
      ])
    else
      AC_MSG_ERROR([only system libzstd is supported])
    fi
  fi

通过上面执行的命令来看,应该是 pkg-config 这个环节出了问题,最后我手动执行这个命令发现:

pkg-config --exists libzstd

zsh: command not found: pkg-config

花了几十分钟总算找到问题了,接下来就是安装这个工具:

brew install pkg-config

安装完以后,再次执行 pecl install redis 便顺利完成 redis 扩展的安装。

安装 mongodb 扩展

pecl install mongodb
.....
In file included from /private/tmp/pear/temp/mongodb/php_phongo.c:29:
In file included from /opt/homebrew/Cellar/php/8.1.3/include/php/ext/spl/spl_iterators.h:22:
/opt/homebrew/Cellar/php/8.1.3/include/php/ext/pcre/php_pcre.h:23:10: fatal error: 'pcre2.h' file not found
#include "pcre2.h"
         ^~~~~~~~~
1 error generated.
make: *** [php_phongo.lo] Error 1
ERROR: `make' failed

哎,又找不到文件了,这个包之前在安装 PHP 8.1 的时候 Homebrew 自动安装过了。为啥找不到呢?在网上搜了各种答案,最后居然是在 swoole 的 issue 中找到了答案。

$ ln -s /usr/local/Cellar/pcre2/10.36/include/pcre2.h /usr/local/Cellar/php/8.0.0_1/include/php/ext/pcre/pcre2.h

# 因为我的是 Apple Silicon 所以 Homebrew 的安装目录和 Intel 的有所区别,这点在 Homebrew 的官方文档里有说明。我这里将目录替换成如下:
$ ln -s /opt/homebrew/Cellar/pcre2/10.39/include/pcre2.h /opt/homebrew/Cellar/php/8.1.3/include/php/ext/pcre/pcre2.h

然后再次执行 pecl install mongodb 即可完成安装。

安装 swoole 扩展

pecl install swoole
.....
In file included from /private/tmp/pear/temp/swoole/ext-src/php_swoole.cc:16:
In file included from /private/tmp/pear/temp/swoole/ext-src/php_swoole_cxx.h:20:
In file included from /private/tmp/pear/temp/swoole/ext-src/php_swoole_coroutine.h:22:
In file included from /private/tmp/pear/temp/swoole/include/swoole_coroutine.h:22:
In file included from /private/tmp/pear/temp/swoole/include/swoole_socket.h:33:
/private/tmp/pear/temp/swoole/include/swoole_ssl.h:27:10: fatal error: 'openssl/ssl.h' file not found
#include <openssl/ssl.h>
         ^~~~~~~~~~~~~~~
1 error generated.
make: *** [ext-src/php_swoole.lo] Error 1
ERROR: `make' failed

找不到 Openssl 的相关文件,那么我们只需要按照 Openssl 的说明执行如下命令:

export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@3/lib/pkgconfig"

这样就可以正确安装 swoole 扩展了。

I hope this is helpful, Happy hacking…