问题描述
使用rbenv
或asdf
在M1 MacBooks上安装Ruby 3.0.x效果良好。但2.7.x和2.6.x等旧版本存在各种问题。如何在不同时安装homebrew
的x86和ARM版本的情况下修复它们?
推荐答案
要使用rbenv
或asdf
(本例中使用的是asdf)在M1 MacBook上成功安装Ruby2.6.x或2.7.x版,请执行以下步骤:
rbenv
或asdf-ruby
插件的最新版本。在我的情况下,它asdf-ruby
安装在自制软件之上:
brew upgrade asdf
asdf plugin update ruby
重新安装openssl
、readline
和ruby-build
的当前版本,以便拥有最新版本和配置:
brew uninstall --ignore-dependencies readline
brew uninstall --ignore-dependencies openssl
brew uninstall --ignore-dependencies ruby-build
rm -rf /opt/homebrew/etc/openssl@1.1
brew install -s readline
brew install -s openssl
brew install -s ruby-build
在shell配置.bashrc
或.zshrc
中添加以下ENV变量:
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.1)"
export LDFLAGS="-L/opt/homebrew/opt/readline/lib:$LDFLAGS"
export CPPFLAGS="-I/opt/homebrew/opt/readline/include:$CPPFLAGS"
export PKG_CONFIG_PATH="/opt/homebrew/opt/readline/lib/pkgconfig:$PKG_CONFIG_PATH"
export optflags="-Wno-error=implicit-function-declaration"
export LDFLAGS="-L/opt/homebrew/opt/libffi/lib:$LDFLAGS"
export CPPFLAGS="-I/opt/homebrew/opt/libffi/include:$CPPFLAGS"
export PKG_CONFIG_PATH="/opt/homebrew/opt/libffi/lib/pkgconfig:$PKG_CONFIG_PATH"
这将确保在安装期间使用正确的库和头文件,并将忽略阻止某些版本继续安装的implicit-function-declaration
。请注意,对于其他一些shell(如fish
),这些变量的导出将略有不同。
现在启动新的终端会话,您可以尝试安装较旧的Ruby版本:
asdf install ruby 2.7.2
asdf install ruby 2.6.5
请注意,2.5以下的真正旧版本可能仍有问题。大部分贷方流向this Github issue。
更新
对于Ruby 2.2,请更改以下变量:
export RUBY_CONFIGURE_OPTS=openssl@1.0
并执行
asdf reshim ruby
感谢@xjlin0进行此更新