Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install older Ruby versions on a M1 MacBook?

Installing Ruby 3.0.x works fine on M1 MacBooks using rbenv or asdf. But older versions like 2.7.x and 2.6.x are having various issues. How do I fix them, without installing both x86 and ARM versions of homebrew at the same time?

like image 278
orthodoX Avatar asked Sep 01 '21 11:09

orthodoX


People also ask

How do I downgrade Ruby version on Mac?

Set Ruby version with rvm on Mac 0 on the command line. To switch to the system ruby, enter rvm use system . To switch back to the default, rvm default . The command rvm list will show all installed Rubies, including the current and default versions.

How do I uninstall Ruby on Mac m1?

Uninstall Ruby on Mac with rbenv For rbenv, use rbenv versions to see which versions you have installed. Use the uninstall command to remove a version. This will remove any gems associated with the version as well. If you want to reinstall Ruby, see Install Ruby on Mac for recommendations of newer version managers.


1 Answers

In order to make installing of Ruby versions 2.6.x or 2.7.x successful on M1 MacBook using either rbenv or asdf (asdf is used in this example) follow these steps:

Upgrade to the latest version of rbenv or asdf-ruby plugin using your prefered installation method. In my case it's asdf-ruby installed over homebrew:

brew upgrade asdf
asdf plugin update ruby

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/[email protected]
brew install -s readline
brew install -s openssl
brew install -s ruby-build

In your shell config .bashrc or .zshrc add the following ENV variables:

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"

This will ensure that the proper libraries and headers are used during the installations and it will ignore the implicit-function-declaration that is preventing some versions to continue installation. Note that for some other shells like fish the exporting of these variables will be a bit different.

Now start a new terminal session and you can try installing the older ruby versions:

asdf install ruby 2.7.2
asdf install ruby 2.6.5

Note that really old versions below 2.5 might still have issues. Most of the credits go to this Github issue.

UPDATE

For Ruby 2.2 please change the following variable:

export [email protected]

And do a

asdf reshim ruby

Thanks @xjlin0 for this update

like image 86
orthodoX Avatar answered Sep 22 '22 12:09

orthodoX