0

With a default installation of RVM and from a non-login shell, executing rvm use produces:

RVM is not a function, selecting rubies with 'rvm use ...' will not work.

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for an example.

Where the reason for this warning is that the following sourcing line is added only in files like ~/.bash_profile during installation:

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

And the previous can even be confirmed as expected behavior from https://rvm.io/support/faq#shell_login:

RVM by default adds itself currently to ~/.bash_profile file, and the recommended way is to enable login shell in gnome-terminal (and screen).

But, why isn't the RVM installer simply adding the previous line in files like ~/.bashrc so commands like rvm use work both for login and non-login shells?.

Similar questions without a proper/official answer:

Jaime Hablutzel
  • 6,117
  • 5
  • 40
  • 57

1 Answers1

1

The main reason here is that rvm must be defined as a function of your shell and not as a rvm-use script.

If defined as a script, rvm-use would operate in a separate subprocess and only had an access to a copy of your shell environment, not to the original env. Because rvm use needs to actually modify your local PATH environment (to prepend rvm ruby shims for correct version) it needs full access to your shell environment - hence use of function is required.

This means, you need to load this function somewhere - it is (most likely, didn't fully check it) done via /etc/profile, which loads /etc/profile.d/rvm.sh file. This file needs to be either manually sourced or is loaded automatically when terminal opens as a login shell.

Now, why does rvm needs to modify local terminal environment instead of using single global state? It is to allow us to have few terminals open with different ruby versions active at the same time.

BroiSatse
  • 44,031
  • 8
  • 61
  • 86
  • As you've pointed out correctly, the RVM installer adds its _function definition_ sourcing line to profile initialization files like `~/.bash_profile`, but my question is why doesn't it add this line to files like `~/.bashrc` so a login shell isn't required to be able to use commands like `rvm use`. – Jaime Hablutzel May 02 '20 at 20:25