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

36

在M1 MacBook上使用rbenvasdf安装Ruby 3.0.x没有问题。但是旧版本(如2.7.x和2.6.x)存在各种问题。我该如何修复它们,而无需同时安装homebrew的x86和ARM版本?

7个回答

62
为了在使用rbenvasdf(本例中使用asdf)的情况下成功安装 Ruby 版本2.6.x或2.7.x 在 M1 MacBook 上,按照以下步骤进行操作: 通过您喜欢的安装方法升级到最新版本的rbenvasdf-ruby插件。在我的情况下,我使用Homebrew安装asdf-ruby
brew upgrade asdf
asdf plugin update ruby

重新安装当前版本的opensslreadlineruby-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中添加以下环境变量:
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”。请注意,对于其他一些类似fish的shell,导出这些变量的方式会有所不同。现在开始一个新的终端会话,然后您可以尝试安装旧的ruby版本:
asdf install ruby 2.7.2
asdf install ruby 2.6.5

请注意,版本在2.5以下的非常老的版本可能仍然存在问题。大部分功劳归功于这个Github问题

更新

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

export RUBY_CONFIGURE_OPTS=openssl@1.0

并且做一件事

asdf reshim ruby

感谢 @xjlin0 进行此次更新。


5
我有一个旧项目仍在使用2.4版本,你的建议在我的测试中很有效。谢谢! - Yoopergeek
3
我确认它可以在asdf上工作,但在rbenv上不行,我执行了相同的步骤。 - Almokhtar
5
按照这些指示卸载并重新安装OpenSSL后,我的rubygems和npm注册功能出现问题,我必须手动安装CA证书。 - yuяi
2
针对2.2版本,参考https://github.com/rbenv/ruby-build/issues/1742#issuecomment-835790760进行操作,并将RUBY_CONFIGURE_OPTS设置为openssl@1.0,最后执行`asdf reshim ruby`。 - xjlin0
4
在构建失败时,这并不起作用(使用ruby-build 20220910.1在macOS 12.6上)。 - julio
显示剩余12条评论

14
这个简单的命令帮助了我。
RUBY_CFLAGS="-w" rbenv install 2.5.5

2
哇,截至1月23日它真的起作用了,而已被接受的版本则需要更多的工作。 - Joe Half Face
这对我有用,谢谢!! - undefined

2
面对M2 Pro,Ventura 13.4.1上的错误。
Error running '__rvm_make -j12',
please read /Users/mkrasikov/.rvm/log/1688468004_ruby-2.7.4/make.log
There has been an error while running make. Halting the installation.

解决方案:

brew install rbenv
RUBY_CFLAGS="-w" rbenv install 2.7.4

0

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

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.2.2的同样问题,而且很多gem都依赖于它。因此,我创建了一个Ubuntu 18.04的Docker容器,然后在其中安装了Ruby。现在它可以正常工作了。


是的,我更喜欢将我的开发环境设置为Docker化,适用于所有项目。但是,出于编辑器使用的linting和格式化的考虑,我确实在本地安装了我使用的语言。从容器内部使用lint/format是可行的,但体验仍然不太好,并且对于许多编辑器来说很难设置。这就是为什么我只在本地安装Ruby/Elixir版本,而所有其他服务都严格使用Docker,例如Posgres、Redis等。 - orthodoX

0

这个问题已经在 ruby 2.7.10 及以上版本 中得到解决。如果您使用版本管理器,例如 asdf,则应该为完整的 2.7.x 版本范围(而不仅仅是 2.7.10)解决此问题。

根本问题是 Xcode 14 引入的不兼容性。一种解决方法是降级到 Xcode 13,以便您可以构建 ruby。这对许多用户来说并不是可行的路径,因此另一个选择是获取已经构建好的适用于您架构的 ruby 版本,这可以通过从 homebrew 下载实现。

Homebrew有一个ruby@2.6的公式,可以在MacOS M1(arm)和旧的基于英特尔的模型(x86)上运行。您可以使用以下命令安装它:brew install ruby@2.6,这将安装ruby 2.6.10。如果这就是您所需的全部内容,您可以在此停止。

如果您想使用版本管理器(如asdf)使用ruby 2.6,您可以执行以下操作:

brew install ruby@2.6
ln -s $(brew --prefix ruby@2.6) ~/.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/openssl@1.1
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 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"

asdf install ruby 2.6.9

不使用brew和x86,无解决方案。 - julio

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接