So, you’ve got an M1 Mac and you’re ready to roll with Ruby. You might have hit a few bumps along the way, but worry not - we’ve all been there. This guide is all about getting you through the rough patches and straight to coding bliss on MacOS Sonoma.
Running Ruby on an M1 Mac can sometimes be a tricky affair, especially with the various dependencies and configurations needed for a successful installation. This guide is tailored to help you navigate through the installation process on MacOS Sonoma, highlighting common issues and providing straightforward solutions to get Ruby up and running on your machine.
You might have encountered errors similar to the following during your installation attempts:
ruby-build: using gmp from homebrew
BUILD FAILED (macOS 14.0 using ruby-build 20231009)
compiling ossl_x509name.c
compiling ossl_x509req.c
compiling ossl_x509revoked.c
compiling ossl_x509store.c
linking shared-object openssl.bundle
ld: warning: ignoring duplicate libraries: '-lruby.3.2'
1 warning generated.
linking shared-object ripper.bundle
ld: warning: ignoring duplicate libraries: '-lruby.3.2'
make: *** [build-ext] Error 2 ```
Seeing this on your terminal? You’re not alone. Let’s smash those errors together.
## Prerequisites
I’m guessing you’ve got Homebrew somewhere on your machine. If not, no biggie - we’ll get to that. If it's already there, run the following command with an expected error:
```brew upgrade```
Error: Cannot install in Homebrew on ARM processor in Intel default prefix (/usr/local)! ...```
This error is a common pitfall and a major reason for installation failures.
First things first, let's make sure XCode is up-to-date
xcode-select --install
- Navigate to Homebrew's website
- Click on the highlighted option for M1 Macs, which should direct you to their GitHub page.
- Under assets, choose your preferred installer. The Homebrew-****.pkg option is recommended for its simplicity.
- Run the installer and disregard the pop-up command about the Homebrew path for now. We will address this in the following steps.
- Run
brew doctor
and get rid of the files it warns about.
Step 3: Install rbenv and ruby-build
Now, install rbenv and other necessary tools. rbenv
is a Ruby version manager that allows us to run different versions of Ruby locally, globally, and at the application specific level. ruby-build
is a tool that downloads and compiles various versions of Ruby and helps us with dependency management
brew install rbenv ruby-build rbenv-gemset rbenv-vars
brew install openssl@1.1 readline libyaml gmp
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.1)"
brew install openssl@3 readline libyaml gmp
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@3)"
brew install rust
Open your ~/.zshrc
file and add:
export PATH=/opt/homebrew/bin:$PATH
eval "$(rbenv init -)"
We’re doing this because Homebrew’s got a new home in /opt/homebrew, and we need to let rbenv
know where to look. Your ~/.zshrc
should look something like this:
export PATH=/opt/homebrew/bin:$PATH
# For Ruby 2.x - 3.0
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.1)"
# ************* OR **************
# For Ruby 3.1 and above
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@3)"
eval "$(rbenv init -zsh)"
After updating, run source ~/.zshrc
to reload your file and ensure the changes take place. You can also run brew config
and verify homebrew is using the freshly installed version on the HOMEBREW_PREFIX
line.
Run:
rbenv init
You should follow the instructions that are logged. I've only seen to add eval "$(rbenv init -)"
to your ~/.zshrc
file (which we did in the step above) but if you see other instructions, please comment below. Then close and reopen your terminal window. Run the following to check your setup:
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-doctor | bash
If rbenv
points to an old version of Homebrew like the image below, clean it up:
rm -rf /usr/local/bin/rbenv*
Run the rbenv-doctor script again to ensure everything is set up properly.
Install Ruby 3.1.2:
rbenv install 3.1.2
You should see an output like below:
For Ruby 3.2.2:
rbenv install 3.2.2
Create a new directory and navigate to it in the terminal. Set the local Ruby version:
rbenv local 3.1.2
There is no output for the command above but you should see a .ruby-version file in the directory and can verify the Ruby version with ruby -v
.
And as you can see, I have set different local Ruby versions on each directory
To set a global Ruby version:
rbenv global <version>
I mainly wrote it for myself in case I run into this again but hopefully someone gets something out of this.