Multiple R Versions

Installing multiple versions of R on the same machine is useful if you want to take advantage of the latest R features while also maintaining reproducibility for older projects. RStudio provides pre-compiled deb and rpm packages of many versions of R. This is convenient for installing multiple versions side-by-side or if your Linux distribution does not have the version you want in their repositories.

This is great, but how do you toggle between versions?

If you have an RStudio Desktop Pro license then you’ll have an option in the RStudio IDE to toggle between R versions. But if you don’t, or if you want to invoke R from the terminal, keep reading!

A Shim to Toggle Versions

Most guides for installing multiple R versions side-by-side will install them in /opt/R by default. I currently have three versions installed:

ls /opt/R
# 3.6.3  4.0.5  4.1.2

A specific version could be invoked from a terminal like this:


But this is annoying and won’t work if you want to use the RStudio IDE.

So, I wrote a little shim to make things easier for me. I’ve made improvements to it over the years and figured it was time to share. With the shim, I can set an environment variable R_VERSION to the version that I want to run.

export R_VERSION=4.0.5
R --version
# R version 4.0.5 (2021-03-31) -- "Shake and Throw"
# Copyright (C) 2021 The R Foundation for Statistical Computing
# Platform: x86_64-pc-linux-gnu (64-bit)
# R is free software and comes with ABSOLUTELY NO WARRANTY.
# You are welcome to redistribute it under the terms of the
# GNU General Public License versions 2 or 3.
# For more information about these matters see

It also works with Rscript:

export R_VERSION=3.6.3
Rscript --version
# R scripting front-end version 3.6.3 (2020-02-29)

If R_VERSION is not set then the shim defaults to the most recent version installed on the system.

Rscript --version
# R scripting front-end version 4.1.2 (2021-11-01)

Finally, to launch a specific version of R with RStudio:

export R_VERSION=4.0.5
rstudio &

Since R_VERSION is just an environment variable, it can be set in many ways. My favorite is to do it all in one line:

R_VERSION=4.0.5 Rscript
R_VERSION=4.0.5 rstudio &

Installing the Shim

The shim works by replacing the R and Rscript binaries found on your $PATH. This is usually /bin/R/ and /bin/Rscript, which is where the following commands will write the shim (you’ll need to be root).

Feel free to edit the shim before or after you install it. For example, to set the default R version to a fixed version rather than to the latest installed version, or to change the R installation directory (if it’s something other than /opt/R).

cat << 'EOF' > /bin/R

# Uncomment this to set a fixed default version of R. Otherwise, the
# most recent version will be used.

# The default installation directory is /opt/R (
# Change this as is necessary for your environment.

R_VERSIONS_AVAIL=($(basename --multiple $(dirname $(dirname $(ls "${R_INSTALL_DIR}"/*/bin/R))) | sort --version-sort))

# If no default R version is set then set the most recent version as the default
if [ -z ${R_VERSION_DEFAULT} ]; then

# If a specific R version was not requested then use the default
if [ -z ${R_VERSION} ]; then

R_BINARY_BASE=$(basename $0)

if [ ! -f $R_BINARY ]; then
  echo "${R_BINARY_BASE} ${R_VERSION} not found. Available versions: ${R_VERSIONS_AVAIL[*]}"
  exit 1

# Unset the R_HOME env var if something else set it
unset R_HOME
"${R_BINARY}" "$@"
chmod +x /bin/R
ln -s /bin/R /bin/Rscript