Andrew Redd
Andrew Redd

Reputation: 4692

Installing multiple versions of R

When I was in school the system administrator had multiple versions of R installed. I'm running into a bug with R-3.1.0 and need to install reinstall some past versions of R for development while this bug is investigated, but I don't see any documentation about how to have multiple concurrent versions of R on the same system ti should look something like

$ ls -l /usr/lib | grep R-
lrwxrwxrwx  1 root  root         8 Jun  3 09:41 R -> R-3.1.0/
drwxr-xr-x  9 root  root      4096 May 15 11:56 R-3.1.0
drwxr-xr-x  9 root  root      4096 May 15 11:56 R-3.0.3


$ ls -l /usr/bin | grep R-
lrwxr-xr-x  1 root  root         8 Jun  3 09:41 R -> R-3.1.0
-rwxr-xr-x  9 root  root      4096 May 15 11:56 R-3.1.0
-rwxr-xr-x  9 root  root      4096 May 15 11:56 R-3.0.3

I see no documentation about how to achieve this either in the R Installation and Administration guide or in the configure --help. I'm running a Gentoo system and the ebuild removes or overwrites the previous version of R on install, so I'll likely be installing from source.

Upvotes: 9

Views: 8021

Answers (3)

aattp
aattp

Reputation: 33

Not sure if this answer fits 100% to the original question, I just hope it is useful for somebody looking for multiple R installations in a linux system and how to switch between them


In linux, it is possible to use update-alternatives to switch between multiple installations of R (or other software). I share below an approach that works for me. Other approaches based for instance on mamba or docker might be more suitable depending on the case.

In the code below, R is installed using the available builds from rstudio. Alternatively, you can compile R from source like in the accepted answer (by Dirk) or as shown here. The update-alternatives part should work regardless of the R installation approach. According to my experience, installations using apt (as in the official documentation) removes previous R installations, or at least I could not manage to install multiple versions this way.

# I have previously cleaned previous R installations in my system, probably it also works with no cleaning, though.


### Install multiple R versions (from rstudio builds)
# -----------------------------------------------------
# install R 4.4.1 (Ubuntu 22.04)  <https://docs.posit.co/resources/install-r.html#download-and-install-r> 
export R_VERSION=4.4.1
export R_update_alternative_prio=$(echo ${R_VERSION} | tr -d '.') # e.g. 4.4.1 -> 441
curl -O https://cdn.rstudio.com/r/ubuntu-2204/pkgs/r-${R_VERSION}_1_amd64.deb  # choose link based on linux distro:  <https://docs.posit.co/resources/install-r.html#download-and-install-r> 
sudo apt-get update 
sudo apt-get install ./r-${R_VERSION}_1_amd64.deb

# verify installation
/opt/R/${R_VERSION}/bin/R --version


# Create a symlink to R (and Rscript)
sudo ln -s /opt/R/${R_VERSION}/bin/R /usr/local/bin/R
sudo ln -s /opt/R/${R_VERSION}/bin/Rscript /usr/local/bin/Rscript # Think carefully if you plan to invoke Rscript from R code, then rather use full path to Rscript

# # clear the shell's cache of executable locations, might be needed in interactive sessions
# hash -r # when 'which R' returns correct path but 'R' fails to find the executable.

# Configure update-alternatives
sudo update-alternatives --install /usr/local/bin/R R /opt/R/${R_VERSION}/bin/R ${R_update_alternative_prio}
sudo update-alternatives --install /usr/local/bin/Rscript Rscript /opt/R/${R_VERSION}/bin/Rscript ${R_update_alternative_prio} # Think carefully if you plan to invoke Rscript from R code, then rather use full path to Rscript

# # final checks
# which R  # check 'ls -l <path-to-R>', check symlinks until you arrive to R installation at /opt/R
# whereis R
# R
# rstudio

Then, you need to repeat the previous step for installing additional R versions as desired.

Finally, you can choose which is the default R or Rscript version as follows:



# # 'update-alternatives' usage
# sudo update-alternatives --display R
# sudo update-alternatives --display Rscript

# sudo update-alternatives --config R # then choose version
# sudo update-alternatives --config Rscript # then choose version


# rstudio # in rstudio desktop, you need to run 'sudo update-alternatives --configure R' before open rstudio, no possible to choose In Global Options as in windows or rstudio-server  

If you are using rstudio desktop from linux, it seems like only the dafault R version is available (not possible to choose R version from Global options). Then, you need to run sudo update-alternatives --display R before launching rstudio.

Upvotes: 0

Dirk is no longer here
Dirk is no longer here

Reputation: 368599

Yes, this is pretty easy as Josh Ulrich's comment already says.

People usually have this question regarding r-devel installation alongside the basic R you will get from your distro. I use a simple shell script to build / update R-devel, and two matching ones to run R-devel and Rscript-devel. I can't recall if I blogged about it, but folks seem to point to an this older post on the r-sig-debian list which contains it. Here is a nice post by Michael detailing the same process.

And by creating new subdirectories, you can add as many R version as you want. You should keep the local builds in /usr/local, though, and could create softlinks for the binaries in /usr/local/bin. The script R itself will have R_HOME_DIR hard-coded and "just work".

Fancier approaches involve virtualization, and on Linux your best bet is docker which I am playing with now, and which I hope to detail at some point for this very purpose of testing R packages against multiple compilers etc.

Upvotes: 8

earizon
earizon

Reputation: 2238

I'm not an expert on R but on Linux you can always use schroot and debootstrap to create a new isolated environement with different versions of R, libraries, ... Actually I use it to compile different versions of code for RedHat, Ubuntu or Debian.

See for example: https://wiki.ubuntu.com/DebootstrapChroot

(Once you get use to it, it will take no more than 20 minutes to create a new chrooted system)

Upvotes: 0

Related Questions