Install older Ruby versions on a M1 MacBook?
Asked Answered
G

7

38

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?

Gesticulatory answered 1/9, 2021 at 11:14 Comment(0)
G
64

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

Gesticulatory answered 1/9, 2021 at 11:14 Comment(19)
I've a legacy project still creeping along with 2.4, and your recommendations worked in my testing. Thanks! – Cutback
I should have mentioned it at the time as it could be helpful to others - I was using rbenv and it's ruby-build for my testing. – Cutback
i confirm it works with asdf but not rbenv did same steps – Writer
Yeah, as I mentioned I did try it with asdf, I just assumed it should work with rbenv since asdf is using rbenv under the hood. And for @Cutback it did work with rbenv πŸ€·πŸ»β€β™‚οΈ – Gesticulatory
Uninstalling and reinstalling OpenSSL using these instructions jacked up my rubygems and npm registry fuctionality, and I had to manually install CA certs. – Adeliaadelice
Actually, still having issues in various places. For example: Warning UNABLE_TO_GET_ISSUER_CERT_LOCALLY: request to https://registry.npmjs.org/yarn failed, reason: unable to get local issuer certificate – Adeliaadelice
for 2.2 github.com/rbenv/ruby-build/issues/1742#issuecomment-835790760 plus set RUBY_CONFIGURE_OPTS to [email protected] and asdf reshim ruby – Pellicle
@Pellicle I've updated the post with your info. Thanks! – Gesticulatory
What is libffi? As far as I know you need to export /opt/homebrew/opt/readline and opt/homebrew/opt/openssl@3 eg.: 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" [code] – Rangel
@MarceloXavier libffi is a C foreign function interface library. As far as I remember without this setting my installers were not passing successfully. – Gesticulatory
@Gesticulatory asdf does not use rbenv under the hood, it uses ruby-build. Both projects are hosted by the rbenv org on Github, but they are not the same. rbenv is a ruby version manager, the same function as asdf. rbenv also uses ruby-build under the hood. – Vaporetto
I have rosetta installed, so with arm64 doesn't worked. ``` /bin/bash -c "$(curl -fsSL 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 install asdf asdf plugin add ruby github.com/asdf-vm/asdf-ruby.git asdf plugin update ruby arch -x86_64 brew install -s readline arch -x86_64 brew install -s openssl arch -x86_64 brew install -s ruby-build asdf install ruby 2.6.9 ``` – Inexertion
Please be careful suggesting this type of forced install with "ignore" flags, this is dangerous and may screw up another tool that requires this dependencies; KEEP THIS IN MIND if you use this answer!!! – Seminar
This does not work on BUILD FAILED (macOS 12.6 using ruby-build 20220910.1) – Lewes
This doesn't work anymore on macOS 12.6 – Cleave
not working for me on Mac OS 12.5.1 – Antitoxin
A similar comment than the one for ruby <= 2.2.x should appear for ruby versions above 3.0.0, the recommended openssl version in that case is openssl@3 – Sovereignty
This answer should not be the recommended one: mess too much with some important libs of your system for a not guaranted result. – Tripping
LDFLAGS should be separated with ' ', not ':' 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" – Backwoodsman
H
15

This simple command helped me

RUBY_CFLAGS="-w" rbenv install 2.5.5
Hearty answered 16/12, 2022 at 8:37 Comment(2)
wow this really works as of january 23 unlike accepted version which is also much more work – Desmarais
This worked for me, thank you !! – Salter
B
2

Faced with an error on 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.

Solved with:

brew install rbenv
RUBY_CFLAGS="-w" rbenv install 2.7.4
Boggess answered 4/7, 2023 at 11:30 Comment(0)
J
1

For OSX v12.6.1 with frum on M1 Macbook Pro 2021 I used the following exports in ~/.bash_profile after installing ruby-build with brew to successfully build 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"
Jambeau answered 15/11, 2022 at 20:7 Comment(1)
this works for Ruby 3, too – Niels
H
0

I have faced the same issue for Ruby 2.2.2 and many gems were dependent on that. So I have created a docker container for ubuntu 18.04 and then installed ruby on it. It's working.

Herren answered 30/1, 2022 at 9:48 Comment(1)
Yes, I prefer to have my development setup dockerized for all the projects as well. But I do have the languages I work with installed locally for the sake of linting and formatting used by the editors. It's doable to use lint/format from within the containers themselves but the experience is still not that great and easy to set up for a lot of editors. That's why I keep just Ruby/Elixir versions installed locally, but all other services I use strictly from Docker, like Posgres, Redis, etc. – Gesticulatory
E
0

This issue has been resolved for ruby 2.7.10 and onward. If you're using a version manager, such as asdf, this should be resolved for the full 2.7.x version range (not just 2.7.10).

The underlying issue is an incompatibility introduced in Xcode 14. One solution is to downgrade to Xcode 13, so that you can build ruby. This isn't a viable path for many users, so another option is to get an already built version of ruby for your architecture, which occurs with downloads from homebrew.

Homebrew has a formulae for [email protected] that will run on MacOS M1 (arm) and older intel based models (x86). You can install this with brew install [email protected] which will install ruby 2.6.10. If that's all you need, you can stop here.

If you want to use ruby 2.6 with a version manager such as asdf, you can do the following:

brew install [email protected]
ln -s $(brew --prefix [email protected]) ~/.asdf/installs/ruby/2.6.10
asdf reshim ruby 2.6.10
Electrokinetics answered 5/5, 2023 at 15:56 Comment(0)
I
-2

thanks to @orthodox

with rosetta I has brew under arm64. so I uninstall it, and reinstall with x86 and worked

/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/[email protected]
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 [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"

asdf install ruby 2.6.9
Inexertion answered 28/6, 2022 at 1:12 Comment(1)
so no solution without using brew with x86 – Lewes

© 2022 - 2024 β€” McMap. All rights reserved.