Installing Valet

To install the Valet environment, simply follow the Laravel official documentation.

Installing Valet went quite smoothly. Next was installing the extensions required for the project:

  • redis
  • mongodb
  • swoole

Issues Encountered During Extension Installation

Installing the redis Extension

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

As you can see, installing the redis extension requires the three dependencies above. Of course, you can choose ’no’ for all of them, but I chose ‘yes’ for all, and then it failed.

$ pecl install igbinary lzf zstd

After installing the required dependency extensions, I tried installing redis again

$ 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

It failed again. Looking at the error message, it seems it couldn’t find the zstd-related files, but I checked that Homebrew had indeed installed this package.

Then I looked at the config.m4 file in the package’s repository to understand the relevant context:

  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

Based on the commands executed above, it seems the issue was with the pkg-config step. I manually executed this command and found:

pkg-config --exists libzstd

zsh: command not found: pkg-config

After spending several dozen minutes, I finally found the problem. Next was to install this tool:

brew install pkg-config

After installation, running pecl install redis again successfully completed the redis extension installation.

Installing the mongodb Extension

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

Another file not found error. This package was automatically installed by Homebrew when installing PHP 8.1. Why couldn’t it be found? After searching various answers online, I surprisingly found the solution in a 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

# Since I'm on Apple Silicon, the Homebrew installation directory differs from Intel's, as explained in Homebrew's official documentation. I replaced the directories as follows:
$ ln -s /opt/homebrew/Cellar/pcre2/10.39/include/pcre2.h /opt/homebrew/Cellar/php/8.1.3/include/php/ext/pcre/pcre2.h

Then running pecl install mongodb again completed the installation successfully.

Installing the swoole Extension

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

Couldn’t find the Openssl related files. We just need to follow the Openssl instructions and execute the following commands:

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"

This way, the swoole extension can be installed correctly.

I hope this is helpful, Happy hacking…