在M1 MacBook上使用rbenv
或asdf
安装Ruby 3.0.x没有问题。但是旧版本(如2.7.x和2.6.x)存在各种问题。我该如何修复它们,而无需同时安装homebrew
的x86和ARM版本?
rbenv
或asdf
(本例中使用asdf)的情况下成功安装 Ruby 版本2.6.x或2.7.x 在 M1 MacBook 上,按照以下步骤进行操作:
通过您喜欢的安装方法升级到最新版本的rbenv
或asdf-ruby
插件。在我的情况下,我使用Homebrew安装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
中添加以下环境变量: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 进行此次更新。
RUBY_CFLAGS="-w" rbenv install 2.5.5
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
针对在 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"
我曾经遇到过Ruby 2.2.2的同样问题,而且很多gem都依赖于它。因此,我创建了一个Ubuntu 18.04的Docker容器,然后在其中安装了Ruby。现在它可以正常工作了。
这个问题已经在 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
感谢 @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