skip to content

With RVM, what do `rvm install 4.0.7`, `rvm use 4.0.7` and `rvm use 4.0.7 --default` each do, and which one survives a new terminal?

level: juniorimportance: should knowfreq 32%

answer

  1. install builds, use switches
  2. use changes this shell only
  3. --default writes the default alias
  4. not installed: error plus rvm install hint
  5. Ruby 4 needs rvm get master

basics

~20 s

rvm install downloads a prebuilt Ruby or compiles one into ~/.rvm/rubies; rvm use switches only the current shell by rewriting PATH and the gem variables; adding --default also records it as the default alias that every new shell loads.

solid answer

~40 s

`rvm install 4.0.7` puts a Ruby under `~/.rvm/rubies/ruby-4.0.7`: RVM tries a prebuilt binary first, falls back to compiling, and runs its requirements step to get the OS libraries the build needs. `rvm use 4.0.7` is a shell function that rewrites `PATH`, `GEM_HOME`, `GEM_PATH`, `MY_RUBY_HOME` and `RUBY_VERSION` in the current shell only, so a new terminal starts on the default again. `rvm use 4.0.7 --default` does the same and also re-points the `default` alias, which RVM loads when a new shell sources it. If 4.0.7 is not installed, `rvm use` prints `Required ruby-4.0.7 is not installed.` with the `rvm install` command and returns 1, unless you passed `--install`. On an RVM at the 1.29.12 release, run `rvm get master` before installing Ruby 4.

code

bash · 6 lines
bash
rvm get master              # RVM at the 1.29.12 release predates Ruby 4 support
rvm install 4.0.7           # binary if available, else compile
rvm use 4.0.7               # this shell only
rvm use 4.0.7 --default     # this shell + the default alias
rvm list                    # installed rubies, current and default marked
rvm current                 # e.g. ruby-4.0.7

go deeper

for a junior

Recall the three scopes: install is on disk, use is this shell, --default is every future shell. Know rvm list and rvm current to check where you are.

for a middle

Explain that rvm use works by rewriting PATH and the gem variables in the calling shell, and that --default re-points an alias sourced at shell start.

for a senior

Connect the per-shell model to production: servers, cron and CI never type rvm use, so they need the default alias, a project file or a wrapper.

for a principal

Weigh keeping a tool whose last release is 1.29.12 against migrating: installing Ruby 4 already requires tracking RVM master.

## Three commands, three scopes RVM (Ruby Version Manager) keeps several Ruby interpreters side by side in your home directory and lets you pick one per shell. The three commands in the question act at three different scopes, and mixing them up is the most common beginner confusion. | Command | What it changes | Lifetime | |---|---|---| | `rvm install 4.0.7` | adds `~/.rvm/rubies/ruby-4.0.7` and its gem directory | permanent, on disk | | `rvm use 4.0.7` | the environment of the **current shell** | until that shell exits | | `rvm use 4.0.7 --default` | the current shell **and** the `default` alias | every future shell | ## What `rvm install` does `rvm install` takes a **ruby string** such as `4.0.7`, `ruby-4.0.7` or `ruby-head`; `rvm list known` shows the strings your RVM checkout knows about. The install runs in steps: 1. **Requirements.** RVM checks for the operating-system packages a Ruby build needs (a compiler, OpenSSL, libyaml, readline and so on). This is the `rvm requirements` step, run automatically; its behaviour is controlled by the **autolibs** mode, whose default is `enabled`, meaning RVM may install missing packages through the system package manager and ask for `sudo`. 2. **Binary or source.** By default RVM first tries to download a prebuilt binary for your platform; if none exists it downloads the source and compiles it. `--binary` forces the download path and `--disable-binary` forces a compile. 3. **Default gemsets.** RVM creates the Ruby's gem directories, including the `@global` gemset, and installs the gems listed in its `global.gems` file. If no default Ruby exists yet, the first Ruby you install also becomes the default and is selected in the current shell. ## What `rvm use` does `rvm use` is not a program that runs and exits: when RVM is loaded properly, `rvm` is a **shell function** sourced into your bash or zsh session. That is what lets it change the calling shell's environment: - it removes the previous RVM Ruby's directories from `PATH` and prepends the new Ruby's `bin` directory and its gem `bin` directories; - it exports `GEM_HOME`, `GEM_PATH`, `MY_RUBY_HOME`, `RUBY_VERSION` and `IRBRC` for the chosen Ruby (and gemset, if one is named); - it runs `hash -r` so the shell forgets cached command locations. The change is inherited by child processes started from that shell, but a new terminal window starts from scratch. If the Ruby is missing, RVM prints `Required ruby-4.0.7 is not installed.`, suggests `rvm install "ruby-4.0.7"`, stores that command in `$rvm_recommended_ruby` and returns a non-zero status. Passing `--install` (install-on-use) makes it install instead. `rvm use system` switches back to the operating system's Ruby for the current shell, and `rvm use default` returns to the default. ## What `--default` adds With `--default`, after switching the current shell RVM deletes and recreates an **alias named `default`** pointing at the chosen environment (for example `ruby-4.0.7` or `ruby-4.0.7@billing`). The alias is a set of symlinks under `~/.rvm/rubies`, `~/.rvm/gems`, `~/.rvm/environments` and `~/.rvm/wrappers`, plus an entry in `~/.rvm/config/alias`. When a new login shell sources `~/.rvm/scripts/rvm` and finds no RVM Ruby on `PATH`, it sources `~/.rvm/environments/default`, so the new shell starts on that Ruby. `rvm list default` shows it; `rvm use system --default` removes it. A project file (such as `.ruby-version` or `.rvmrc`) found when you `cd` into a directory still wins over the default for that directory. ## Checking what you are on - `rvm list` shows installed Rubies and marks the current and default ones. - `rvm current` prints the active ruby string and gemset. - `which ruby` should point inside `~/.rvm`; `ruby -v` confirms the version. ## Common mistakes - **Expecting an install to change the default.** Installing `4.0.7` on a machine whose default is an older Ruby leaves the default alone; new shells stay on the old Ruby until you run `rvm use 4.0.7 --default`. - **Forgetting the gemset is part of the default.** `rvm use 4.0.7@billing --default` records `ruby-4.0.7@billing`, so every new shell installs gems into that gemset until you change it. - **Reading `rvm use` output as proof for other processes.** An editor, a terminal multiplexer started earlier, or a service keeps the environment it was started with; only processes started from the switched shell inherit the new Ruby. - **Mixing up `rvm use system` and `rvm reset`.** The first switches the current shell to the operating system Ruby; `rvm reset` also removes the default alias. ## Ruby 4 and RVM's release state RVM's last tagged release is **1.29.12** (15 January 2021); later work lives on the master branch, whose `VERSION` reads `1.29.12-next`. Master's list of known Rubies includes `[ruby-]4[.0.7]`, and its changelog lists "Recognize \"4\" as valid MRI Ruby version number" among the unreleased changes after 1.29.12. On a machine that installed RVM from the stable release, run `rvm get master` first, then `rvm install 4.0.7`.

  • What does `rvm requirements` do, and why might it prompt for sudo?
    It installs the operating-system packages a Ruby build depends on, such as a compiler, OpenSSL and libyaml, through the system package manager. `rvm install` normally runs it for you. Because installing system packages needs root, RVM calls `sudo` for that step; `rvm autolibs read-fail` makes it only check and fail instead, which suits users without sudo access.
  • A colleague says `rvm use 4.0.7` 'did nothing' in a new tab. What happened?
    `rvm use` changes only the shell it runs in, by rewriting that shell's `PATH` and gem variables. A new tab starts from the default alias, so unless they ran `rvm use 4.0.7 --default` or the directory carries a project file such as `.ruby-version`, the new tab is back on the previous default.

saying these in an interview costs you the question

  • Thinks rvm use changes the Ruby for every open terminal
  • Believes rvm install always compiles Ruby from source
  • Expects rvm use on a missing Ruby to install it without --install
  • Treats rvm as an ordinary executable rather than a sourced shell function
  • Assumes the RVM 1.29.12 release can install Ruby 4 without rvm get master