在 M1 MacBook 上安装旧的 Ruby 版本?

问题描述 投票:0回答:6

使用

rbenv
asdf
在 M1 MacBooks 上安装 Ruby 3.0.x 工作正常。但是 2.7.x 和 2.6.x 等旧版本存在各种问题。我如何修复它们,而不同时安装 x86 和 ARM 版本的
homebrew

ruby homebrew rbenv apple-m1 asdf-vm
6个回答
59
投票

为了使用

rbenv
asdf
(本例中使用 asdf) 在 M1 MacBook 上成功安装 Ruby 版本 2.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/[email protected]
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 [email protected])"
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
。请注意,对于像
fish
这样的其他一些 shell,这些变量的导出会有些不同。

现在开始一个新的终端会话,你可以尝试安装旧的 ruby 版本:

asdf install ruby 2.7.2
asdf install ruby 2.6.5

请注意,低于 2.5 的真正旧版本可能仍然存在问题。大部分学分归于this Github issue.

更新

对于 Ruby 2.2,请更改以下变量:

export [email protected]

然后做一个

asdf reshim ruby

感谢@xjlin0 的更新


9
投票

这个简单的命令帮助了我

RUBY_CFLAGS="-w" rbenv install 2.5.5

0
投票

我在 Ruby 2.2.2 中遇到了同样的问题,许多 gems 都依赖于此。所以我为 ubuntu 18.04 创建了一个 docker 容器,然后在上面安装了 ruby。它的工作。


0
投票

对于 M1 Macbook Pro 2021 上带有 frum 的 OSX v12.6.1,我在使用 brew 安装 ruby-build 成功构建 ruby 2.6.5 后,在 ~/.bash_profile 中使用了以下导出

export optflags="-Wno-error=implicit-function-declaration"
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@3)"
export LDFLAGS="-L/opt/homebrew/opt/readline/lib -L/opt/homebrew/opt/openssl@3/lib"
export CPPFLAGS="-I/opt/homebrew/opt/readline/include -I/opt/homebrew/opt/openssl@3/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/readline/lib/pkgconfig /opt/homebrew/opt/openssl@3/pkgconfig"

0
投票

此问题已在 ruby 2.7.10 及更高版本中解决。如果您使用的是版本管理器,例如 asdf,则应针对完整的 2.7.x 版本范围(而不仅仅是 2.7.10)解决此问题。

潜在的问题是 Xcode 14 中引入的不兼容性。一种解决方案是降级到 Xcode 13,以便您可以构建 ruby。对于许多用户来说,这不是一条可行的途径,因此另一种选择是为您的架构获取一个已经构建的 ruby 版本,这是从

homebrew
.

下载的

Homebrew 有一个适用于 [email protected] 的公式,它将在 MacOS M1(arm)和旧的基于英特尔的模型(x86)上运行。你可以安装这个

brew install [email protected]
将安装 ruby 2.6.10。如果这就是你所需要的,你可以在这里停下来。

如果你想使用 ruby 2.6 和

asdf
这样的版本管理器,你可以执行以下操作:

brew install [email protected]
ln -s $(brew --prefix [email protected]) ~/.asdf/installs/ruby/2.6.10
asdf reshim ruby 2.6.10

-2
投票

感谢@orthodox

使用 rosetta,我在 arm64 下进行了 brew。所以我卸载它,然后用 x86 重新安装并工作

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
echo 'eval "$(/usr/local/bin/brew shellenv)"' >> /Users/mberrueta/.zprofile
eval "$(/usr/local/bin/brew shellenv)"

arch -x86_64 brew uninstall --ignore-dependencies   asdf
arch -x86_64 brew install asdf
arch -x86_64 brew upgrade asdf
asdf plugin add ruby https://github.com/asdf-vm/asdf-ruby.git
asdf plugin update ruby


arch -x86_64 brew uninstall --ignore-dependencies --force openssl
arch -x86_64 brew uninstall --ignore-dependencies  --force ruby-build
rm -rf /opt/homebrew/etc/[email protected]
arch -x86_64 brew install -s readline
arch -x86_64 brew install -s openssl
arch -x86_64 brew install -s ruby-build



export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix [email protected])"
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"

asdf install ruby 2.6.9
© www.soinside.com 2019 - 2024. All rights reserved.