I have tried to install rails multiple times, but I keep getting this error. Also, whenever I update ruby to version 3.1 and set the global install to 3.1, I receive this message stating I am still on 2.6. I use MacOS and homebrew too.
ebirch@Ejs-MacBook-Pro ~ % sudo gem install rails
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/gems/2.6.0 directory.