In some cases, it seems that your ruby version can still on 2.6 (which is old, latest is 3.2.2 right now) even if you installed the lasted version with brew or rbenv. You can see also that you have rights problems and need sudo rights to install some gems.
# ruby -v
ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.arm64e-darwin22]
# rbenv install 3.2.2
# rbenv global 3.2.2
# ruby -v # WE CAN SEE THAT WE STILL HAVE OLD VERSION
ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.arm64e-darwin22]
This is because MacOS comes with a “system Ruby” pre-installed. If we remove all we can see the pre-installed version is located in this folder:
$ which ruby
/usr/bin/ruby
What should we do to not use system Ruby
There are two methods that I recommend, the method using brew or the method using rbenv which both allows you to manage Ruby version.
Method 1 : Updating Ruby version with Brew
First you need to install the Ruby version with brew:
brew install ruby ruby-build
Then open a terminal and go on your personal folder (/Users/YOUR_USERNAME) and use the following command to switch from the Ruby system version to the brew version with the .zshrc file in your user folder:
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc
Method 2 : Updating Ruby version with RBEnv
If you still see the old version by checking the version doing ruby -v
First you need to install rbenv, which is a ruby version manager, we can install it with brew.
brew install rbenv ruby-build
Then open a terminal and go on your personal folder (/Users/YOUR_USERNAME) and use the following command to switch from the Ruby system version to the rbenv version with the .zshrc file in your user folder:
echo 'eval "$(rbenv init -)"' >> ~/.zshrc
Check the result
Then, open a new terminal (close the old one), if you updated correctly the zshrc file and have installed the correct ruby version, you should be able to see the good version:
% ruby -v
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin22]
Hope it will work for you. Do not hesitate if any question.