Installing old version Ruby version 2.7.1 and 2.7.2 always raises an error on my Apple M1 Macbook. These are my steps on how to install Ruby 2.7.1 and 2.7.2 on my M1 Macbook with Homebrew and asdf. But because under the hood asdf use rbenv, I assume it should work too with rbenv.

My Macbook specifications:

  • MacBook Pro (13-inch, M1, 2020)
  • Chip Apple M1
  • macOS Monterey version 12.1
  • Memory 16GB

1. Install or Upgrade asdf and asdf-ruby

Install asdf and add Ruby plugin to asdf.

brew install asdf
asdf plugin add ruby

Or upgrade existing asdf and asdf-ruby plugin.

brew upgrade asdf
asdf plugin update ruby

2. Install Ruby

Version 2.7.1

Add the following ENV variable to .zshrc.

export RUBY_CFLAGS="-Wno-error=implicit-function-declaration"

And install Ruby 2.7.1 with the following command.

asdf install ruby 2.7.1

If the installation is successful, run asdf list, it will shows Ruby 2.7.1.

Version 2.7.2

If Ruby is already installed, reinstall the current versions of openssl, readline and ruby-build in order to have the latest versions and configs.

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

add the following ENV variables to .zshrc.

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

Install Ruby 2.7.2 with the following command.

asdf install ruby 2.7.2

If the installation is successful, run asdf list, it will shows Ruby 2.7.2.

Based on Stack Overflow, above ENV variables should work too on Ruby 2.6.x, but I’ve never tried to install Ruby 2.6.x.