rubyhomebrewrbenvapple-m1asdf-vm

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?


Solution

  • 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/openssl@1.1
    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 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"
    

    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 RUBY_CONFIGURE_OPTS=openssl@1.0
    

    And do a

    asdf reshim ruby
    

    Thanks @xjlin0 for this update