最近在 macOS Catalina 下使用 RVM 安装 ruby 2.7.2 时,在安装阶段遇到了报错:

Error running '__rvm_make install',
please read ~/.rvm/log/1604332862_ruby-2.7.2/install.log
There has been an error while running make install. Halting the installation.

打开日志看了下,发现有句这样的关键错误消息:

~/.rvm/src/ruby-2.7.2/lib/rubygems/core_ext/kernel_require.rb:92:in `require': cannot load such file -- openssl (LoadError)

看起来是 OpenSSL 的依赖问题,搜索了一通,多数人提出的解决方法是安装时加上 --with-openssl-dir 参数,指向当前 OpenSSL 安装的路径: brew --prefix openssl ,试了下也还是一样报错。于是 brew info openssl 看了下,发现有这么段说明:

If you need to have openssl@1.1 first in your PATH run:
echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.bash_profile

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

For pkg-config to find openssl@1.1 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"

难道是少了环境变量?理论上开箱即用才是最好的体验啊🤔,于是补上环境变量重试了下:

$ LDFLAGS="-L/usr/local/opt/openssl@1.1/lib" \
CPPFLAGS="-I/usr/local/opt/openssl@1.1/include" \
PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig" \
rvm install ruby-2.7.2
Searching for binary rubies, this might take some time.
No binary rubies available for: osx/10.15/x86_64/ruby-2.7.2.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for osx_brew.
Updating certificates bundle '/usr/local/etc/openssl@1.1/cert.pem'
Requirements installation successful.
Installing Ruby from source to: ~/.rvm/rubies/ruby-2.7.2, this may take a while depending on your cpu(s)...
ruby-2.7.2 - #downloading ruby-2.7.2, this may take a while depending on your connection...
ruby-2.7.2 - #extracting ruby-2.7.2 to ~/.rvm/src/ruby-2.7.2.....
ruby-2.7.2 - #configuring.........................................................................
ruby-2.7.2 - #post-configuration.
ruby-2.7.2 - #compiling........................................................................
ruby-2.7.2 - #installing............
ruby-2.7.2 - #making binaries executable...

顺利编译通过👌。