在 Apple M1 (arm处理器) 上安装 APISIX 开发环境

2,703 阅读2分钟

0x00 背景

由于换了电脑进行开发,我不得不重新安装开发环境。这是个痛苦的过程……

0x01 安装环境

我按照官网文档,先开始安装依赖

# 安装 OpenResty, etcd 和 编译工具
brew install openresty/brew/openresty luarocks lua@5.1 etcd curl git

brew 默认会安装 lua-5.4 版本,这一步没什么问题。虽然 APISIX 运行时使用的是 LuaJIT,不过这部操作在 APISIX 启动的时候会通过环境变量找到 OpenResty 的安装目录,进而找到 LuaJIT 的位置。

然后 clone 源码,我把它放在 brew 的安装程序的默认存放目录下,想跟 brew 安装的其他文件夹放在一起。

brew 的安装程序的默认存放目录是 /opt/homebrew/Cellar,如下

image-20210514015913054.png

然后 cd 到 /apisix 目录下,执行 make deps,出现了第一个错误

mkdir -p ~/.luarocks
luarocks --lua-dir=/usr/local/opt/lua@5.1 config --local variables.OPENSSL_LIBDIR /opt/homebrew/Cellar/openresty/1.19.3.1_1/openssl/lib

Error: Lua interpreter not found at /usr/local/opt/lua@5.1

You may want to specify a different Lua version with --lua-version

make: *** [deps] Error 1

这个报错一开始看的懵逼了,分析了一下,应该是在 /usr/local/opt/lua@5.1 找不到 lua 的命令文件。

然后在 Makefile 里面搜了一下关键词:/usr/local/opt/lua@5.1,果然有

LUAROCKS=luarocks --lua-dir=/usr/local/opt/lua@5.1

--lua-dir 参数告诉 luarocks 使用哪种Lua安装 lua 的包。Makefile 指定了 /usr/local/opt/lua@5.1 这个位置,但是我电脑上这个位置是没有这个文件夹了。

搜了半天发现,原来在 M1 上,homebrew 的 HOMEBREW_PREFIX/opt/homebrew,而不是 /usr/local 了。来源于这个 PR:github.com/Homebrew/in…

所以通过 luarocks 安装的 lua@5.1 的位置变成了 /opt/homebrew/opt/lua@5.1,我改了 Makefile 中这行的代码,变成

LUAROCKS=luarocks --lua-dir=/opt/homebrew/opt/lua@5.1

继续执行 make deps,继续报错,此时心态濒临崩溃。

报错如下

Error: Failed installing dependency: https://luarocks.org/luasec-0.9-1.src.rock - Could not find header file for OPENSSL
  No file openssl/ssl.h in /opt/homebrew/Cellar/openresty/1.19.3.1_1/openssl/include
You may have to install OPENSSL in your system and/or pass OPENSSL_DIR or OPENSSL_INCDIR to the luarocks command.
Example: luarocks install luasec OPENSSL_DIR=/usr/local
make: *** [deps] Error 1

这说明 /opt/homebrew/Cellar/openresty/1.19.3.1_1/openssl/include/openssl/ssl.h 这个位置的文件不存在。

全局搜了一下 ssl.h 文件

$find . / -name "ssl.h" 2>/dev/null | grep -v "Permission denied"

/System/Volumes/Data/opt/homebrew/Cellar/openresty-openssl111/1.1.1k_1/include/openssl/ssl.h
/System/Volumes/Data/opt/homebrew/Cellar/openssl@1.1/1.1.1k/include/openssl/ssl.h
/opt/homebrew/Cellar/openresty-openssl111/1.1.1k_1/include/openssl/ssl.h
/opt/homebrew/Cellar/openssl@1.1/1.1.1k/include/openssl/ssl.h

发现在安装依赖的时候,自动安装的 openresty-openssl111 下,其实 openssl@1.1 下也有,但是我选择 openresty-openssl111 ,我记得之前版本的 openresty 携带的 openssl 就在自己的安装目录下,看 Makefile 也是去 openresty 的安装目录下寻找,可能是最新的 brew 或者 openresty 修改了 openssl 的安装位置。

我选择最简单的办法解决这个问题,软连接把 opt/homebrew/Cellar/openresty/1.19.3.1_1/openssl 指向 opt/homebrew/Cellar/openresty-openssl111/1.1.1k_1,这样就能搜到了,命令如下

ln -s /opt/homebrew/Cellar/openresty-openssl111/1.1.1k_1 /opt/homebrew/Cellar/openresty/1.19.3.1_1/openssl

继续执行 make deps,终于没有报错,完整地安装了依赖,并且成功启动。