I have a rails application that I have been working on for a few days after I successfully installed rails.
I closed my terminal window to open up a new window and unfortunately when I try to restart the local rails server, it says rails is no longer able to be found.
Even after I run
rbenv install 3.1.2
the ruby version does not reflect the latest.
Installing ruby-3.1.2...ruby-build: using readline from homebrewInstalled ruby-3.1.2 to /Users/sharatakasapu/.rbenv/versions/3.1.2
Sharats-MBP:~ sharatakasapu$ ruby -vruby 2.6.8p205 (2021-07-07 revision 67951) [universal.x86_64-darwin21]Sharats-MBP:~ sharatakasapu$
When I try to install rails
gem install rails -v 7.0.2.4
I get this error
ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.
CodePudding user response:
echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.zshrc source ~/.zshrc
CodePudding user response:
This doesn't exactly seem like a ruby question so much as a macOS question. You used rbenv to upgrade your ruby which is fine, but macOs currently comes with ruby 2.6 pre-installed. Your system probably has it installed for a reason so don't remove it.
Try running which ruby
in your shell to confirm your shell is picking up /usr/bin/ruby which is the built in version. To pick up a different version of ruby you'll need to modify your system path so that the location of the ruby version you want to pick up gets scanned before the version you don't want.
You will first need to find where rbenv installed the ruby version you are looking for on your machine. The output you sent says rbenv to ~/.rbenv/versions/3.1.2. You can modify the path variable of a single shell session with PATH=$HOME/.rbenv/versions/3.1.2:$PATH
but this is imperfect because if you close the shell and reopen it your path will have its prior value. I've certainly made that mistake before.
Persistently changing your path depends on your shell version but the default shell on macOS is zsh which can be configured by appending export PATH=$HOME/.rbenv/versions/3.1.2:$PATH"
to your .zshenv you may have to research your specific shell. You'll know you did it right when which ruby
brings up the location of the new ruby and ruby --version
returns the expected version. Cheers.
CodePudding user response:
You are using the ruby version of your system, not the rbenv
one. Follow the next steps to switch to rbenv
.
Step 1 Add the following lines to your .zshrc
file if they are not exist
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init - zsh)"
Step 2
rbenv install 3.1.2
rbenv global 3.1.2
Step 3
gem install rails -v 7.0.2.4