the correct Ruby version when opening a terminal

2019-08-10 02:12发布

问题:

Each time I open a terminal my Ruby version is wrong. I am obliged to execute by hand the following command:

source /usr/local/rvm/scripts/rvm

I read that RVM doesn't modify .bashrc or .bash_profile but I note that my .bashrc has at the end:

PATH=$PATH:$HOME/.rvm/bin

Not only I didn't insert this myself but the path to rvm is wrong ! I changed by:

PATH=$PATH:/usr/local/rvm/bin

I reopened a terminal but the Ruby version is always wrong !

回答1:

rvm does modify your .bash_profile / .bashrc -- that's one of its weaknesses. In particular, it adds (is supposed to add) this line to load rvm as a shell function:

# Load RVM into a shell session as a function
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

I had a similar issue with the wrong ruby version running even after I changed the global default in rvm--ensuring this line was at the end of my .bash_profile fixed it. In any case, that line's supposed to be there for rvm to work properly.



回答2:

You can make an .rvmrc file in the root directory of your application.

It runs whatever is inside on opening the root direct, so I think you can just copy this

PATH=$PATH:/usr/local/rvm/bin

and things should be good. You'll get a warning, but it's safe to ignore (or has been in my experience). I think you might be better off going with this, which I've found helpful to keep rubies/gemsets straight across applications.

#.rvmrc
rvm [ruby]@[gemset] #for example rvm 2.0.0-p247@my_gemset

Why gemsets/rubies get switched/reset in the first place, I really don't know and it is annoying, but this seems to fix it.

read more here



回答3:

Depending on your setup it could not be loading for several reasons.

For instance, when you open a shell (depending on the OS distribution) bash files are read in various orders, although here is what the standard looks like: ( I believe Ubuntu is slightly different so you might want to check if thats your distro).

# for interactive login shells (when you login to a terminal)
/etc/profile
/home/<user>/.bash_profile
/home/<user>/.bash_login # if there is no .bash_profile
/home/<user>/.profile # if there is no .bash_login
# for interactive non-login shells (i.e. opening up a new tab)
/etc/bash.bashrc
/home/<user>/.bashrc

What happens when you directly source your .bashrc? Does it then find rvm? If so, then its just a matter of making sure your .bash_profile sources .bashrc aka:

# in .bash_profile
if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi

If this isn't the cause you could dive into the rvm documentation or switch to rbenv which IMHO is a much simpler solution.



回答4:

RVM should handle this automatically but this code is wrong.

PATH=$PATH:/usr/local/rvm/bin

RVM's path should be first. Your bash is acting as you wish actually. It uses the 'system wide' ruby first because you tell your shell to do so. Change this to:

PATH=/usr/local/rvm/bin:$PATH

then source ~/.bashrc and it will work as you wish, calling rvm ruby when you login.