Ruby 版本升级

发布时间 2023-12-20 14:47:46作者: 麦豇豆

一、升级原因

在开发shopify app的时候,提示我当前的Ruby版本不支持(如下图),所以需要升级Ruby。
由于Ruby 中的一些 Gem 依赖于 OpenSSL 库,所以更改 Ruby 版本,还需要安装正确版本的 OpenSSL。
下面的升级过程会先安装 OpenSSL 1.1,再安装 Ruby 3.2。

二、升级过程

1、降级 OpenSSL 版本

我当前的 OpenSSL 版本为 3.2,存在不兼容的问题,所以需要降到 1.1
  • 查看已安装的 OpenSSL 版本
brew list | grep openssl
  • 查看当前 OpenSSL 版本
openssl version
  • 使用 Homebrew 安装 OpenSSL 1.1
brew install openssl@3.0
  • 更新链接
brew link --force openssl@3.0
  • 配置环境变量
~/.bashrc~/.zshrc 或其他 shell 配置文件中添加如下行:
export PATH="/usr/local/opt/openssl@3.0/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/openssl@3.0/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@3.0/include"
export PKG_CONFIG_PATH="/usr/local/opt/openssl@3.0/lib/pkgconfig"
  • 使用source命令立即应用配置文件的更改 或 重启终端
source ~/.bashrc
source ~/.zshrc
此时,再次查看 OpenSSL 版本,应为 1.1

2、升级 Ruby 版本

  • 查看当前 Ruby 版本
ruby -v
  • 使用 Homebrew 安装 Ruby
brew install ruby@3.2
  • 配置环境变量
~/.bashrc~/.zshrc 或其他 shell 配置文件中添加如下行:
export PATH="/usr/local/opt/ruby/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"
export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"
  • 使用source命令立即应用配置文件的更改 或 重启终端
source ~/.bashrc
source ~/.zshrc

三、其他

1、查看当前 Ruby 依赖的 OpenSSL 版本

ruby -ropenssl -e 'puts OpenSSL::OPENSSL_VERSION'

2、OpenSSL 版本不对相关报错

  • ERROR: While executing gem ... (Gem::Exception)OpenSSL is not available. Install OpenSSL and rebuild Ruby or use non-HTTPS sources (Gem::Exception)
  • Could not load OpenSSL. You must recompile Ruby with OpenSSL support.
  • cannot load such file -- openssl (LoadError)

3、参考链接

https://stackoverflow.com/questions/14845481/cannot-load-such-file-openssl-loaderror
https://www.jianshu.com/p/36a7a354d10f