[RAILS] rbenv versions and ruby -v versions do not match

problem

asatokensei@MacBook-Air memopy % rbenv versions     
  system
  2.6.6
  2.7.0
* 2.7.1 (set by /Users/asatokensei/.rbenv/version)

asatokensei@MacBook-Air memopy % ruby -v            
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin19]

2.7.1 for rbenv versions 2.6.3 with ruby -v It has become.

I want to use 2.7.1 for joint development.

solution

This seems to be a mistake in the environment variable (PATH). From here, knowledge of UNIX commands is required, so it is recommended to study with dot installation. ↓ https://dotinstall.com/lessons/basic_unix_v2

Let's see from which path ruby is called.

asatokensei@MacBook-Air memopy % which ruby
/usr/bin/ruby

It seems that this path uses ruby, which is included by default in MacOS, instead of ruby entered in rbenv.

Path when ruby of rbenv is called ↓

$ which ruby
/Users/username/.rbenv/shims/ruby

It seems that it should be like the above.

When I check the path, ...

asatokensei@MacBook-Air memopy % echo $PATH
/usr/local/opt/[email protected]/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

You can see that /Users/username/.rbenv/shims/ruby is not included. → In other words, ruby of rbenv is not called.

Since ruby in /Users/username/.rbenv/shims/ruby should be called preferentially, ~ / .bashrc, ~ / .zshrc so that this path comes to the beginning of the environment variable (PATH) Set to etc.

asatokensei@MacBook-Air memopy % rbenv init
# Load rbenv automatically by appending
# the following to ~/.zshrc:

eval "$(rbenv init -)"

Edit ~ / .zshrc.

asatokensei@MacBook-Air memopy % vi ~/.zshrc

When this command is depressed,

export PATH="/usr/local/opt/[email protected]/bin:$PATH"

~                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                                                             
~                                                                                                                                                                                      
~                                                                                                                                                                                      
~                                                                                                                                                                                      
~                                                                                                                                                                                      
~                                                                                                                                                                                      
~                                                                                                                                                                                                                                                                                                                                                                           
~                                                                                                                                                                                      
"~/.zshrc" 3L, 73C

The screen looks like this.

Edit by pressing the i key of insert.

eval "$(rbenv init -)"

Enter a sentence of ↑. You can save it with: wq and exit.

Command that reflects the settings you just made ↓

asatokensei@MacBook-Air memopy % source ~/.zshrc 

This should have been reflected, so check it.

asatokensei@MacBook-Air memopy % ruby -v        
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
asatokensei@MacBook-Air memopy % which ruby
/Users/asatokensei/.rbenv/shims/ruby
asatokensei@MacBook-Air memopy % echo $PATH
/Users/asatokensei/.rbenv/shims:/usr/local/opt/[email protected]/bin:/usr/local/opt/[email protected]/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

It is 2.7.1 properly, and the path is correct.

asatokensei@MacBook-Air memopy % rbenv versions
  system
  2.6.6
  2.7.0
* 2.7.1 (set by /Users/asatokensei/.rbenv/version)
asatokensei@MacBook-Air memopy % ruby -v       
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]

This is OK! !! !!

reference

https://easyramble.com/rbenv-ruby-version-trouble.html

https://qiita.com/sea_ship/items/195466a666cfd94f0225

Recommended Posts

rbenv versions and ruby -v versions do not match
How to deal with different versions of rbenv and Ruby
Upgrade and switch Ruby versions on Windows 10
Do not return when memoizing in Ruby
Ruby and Gem
Install rbenv with apt on ubuntu and put ruby
Correspondence when Ruby version does not switch in rbenv
Manage multiple versions of Ruby on Mac [macOS + rbenv]
In Ruby, methods with? Do not always return true/false.
What to do if you installed Ruby with rbenv but the version does not change