'I am unable to install rails and have tried installing ruby version 3.7 multiple times

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.


Solution 1:[1]

Based on your brief context I am gonna assume you're not using a tool such as rbenv or RVM and am gonna recommend these.

I would recommend to go with a guide like this (instead of reading the above tools docs): https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-with-rbenv-on-macos Basically until step 3 there is good enough and you just change the versions of ruby and rails to whatever you need.

Note (since I saw another answer mentioning conda): conda is also a more general (not specific to ruby) environment manager and if that works for you great too.

Solution 2:[2]

You might have to re-download ruby in order to get the newest version. Ruby just came out with a new update about a couple of months ago and it is not compatible with the older version of rails

Solution 3:[3]

How about using conda?

$ brew install --cask miniconda
$ conda init bash            # from then on your bash shell will be `(base) ... $`
# in `base` you are in normal global environment
# indicating that you are in base conda environment
$ conda activate --name ruby # create environment for ruby
$ conda activate ruby        # enter the 'ruby' environment
$ conda install -c conda-forge ruby=3.7  # install ruby 3.7 into this environment 'ruby'

# you can any time get out of 'ruby' environment by:
$ conda deactivate

# you can 'see' your ruby 3.7 only if you enter the 'ruby' environment by
$ conda activate ruby
# as soon as you deactivate this environment, you will see your normal global ruby, when calling ruby.

Learn about usage of conda in https://www.youtube.com/watch?v=YJC6ldI3hWk .

Solution 4:[4]

Follow the next steps to set up rbenv, ruby 3.1.2, and rails.

Step 1

brew install rbenv

Step 2 Add the following lines to your .zshrc file

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init - zsh)"

Step 3

brew update && brew upgrade ruby-build

Step 4

rbenv install 3.1.2
rbenv global 3.1.2

Step 5

gem install rails

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 draganstankovic
Solution 2 juliavolpe
Solution 3 Gwang-Jin Kim
Solution 4 Christos-Angelos Vasilopoulos