Hi all,
I noticed there are many questions and no dedicated tutorials here for installing multiple versions of R and bioconductor so I thought I''d share how I install and switch between versions of R and bioconductor on Ubuntu. This is for both beginners on first installation of R/Rstudio/Bioconductor, and intermediates looking to use multiple versions. I find it useful to have everything in one place. I've also included the most useful links for each step that helped me piece this together for whenever I do a complete re-install.
Hope it helps someone out!
Bioinfguru
Edits:
- 19/4/24 - The scope of this tutorial is for those installing on personal machines (sudo privilage is required). Next time I install, I'll be trying to install as guest, without sudo to see what fixes are required. For those who want to use this on a cluster, that is beyond the scope of this tutorial.
INSTALL R
Source: Posit - Install R
Remember: All versions of R are always installed in /opt/R, while a symlink is placed in /usr/local/bin/R. So the command "which R" always returns the symlink, not the installation location or version
First Installation: View the list of R versions and choose the latest (currently 4.3.3)
sudo snap install curl # step 1
sudo apt-get update # step 2
sudo apt-get install gdebi-core # step 3
export R_VERSION=4.3.3 # step 4 - notice version number is latest version always for first installation
curl -O https://cdn.rstudio.com/r/ubuntu-2204/pkgs/r-${R_VERSION}_1_amd64.deb # step 5
sudo gdebi r-${R_VERSION}_1_amd64.deb # step 6
/opt/R/${R_VERSION}/bin/R --version # step 7 - verify installation
sudo ln -s /opt/R/${R_VERSION}/bin/R /usr/local/bin/R # step 8 - create R symlink (first installation only)
sudo ln -s /opt/R/${R_VERSION}/bin/Rscript /usr/local/bin/Rscript # step 9 - create Rscript symlink (first installation only)
R --version # step 10 - verify R symlink
Rscript --version # step 11 - verify Rscript symlink
Now to install additional versions of R (older or newer) is simple:
- Edit step 4 (e.g. export R_VERSION=4.2.3)
- Repeat steps 4-7 only (the default version is always the last one you install)
And to change the default R version (always used by Rstudio) is also simple (just edit the symlinks):
export R_VERSION=X.X.X # select the version you wish to be the default
sudo ln -sf /opt/R/${R_VERSION}/bin/R /usr/local/bin/R # edits R symlink
sudo ln -sf /opt/R/${R_VERSION}/bin/Rscript /usr/local/bin/Rscript # edits Rscript symlink
readlink -f /usr/local/bin/R && readlink -f /usr/local/bin/Rscript # verify symlinks
R --version # verify R version
Rscript --version # verify Rscript version
And to temporarily use a different R version is simple:
/opt/R/4.2.3/bin/R # runs version 4.2.3
INSTALL RSTUDIO
Sources:
This may be specific to my machine, or operating system (Ubuntu 22.04), but it works without error for me so I'm posting this as some people also have trouble installing Rstudio and get stuck in a tree of errors due to not having some of the dev packages listed below installed on there machine.
gpg --keyserver keys.openpgp.org --search-keys 51C0B5BB19F92D60 # enter number 1 when prompted
sudo apt-get install libxml2-dev libfontconfig1-dev libharfbuzz-dev libfribidi-dev libcurl4-openssl-dev libfreetype6-dev libpng-dev libtiff5-dev libjpeg-dev
Finally, I can download ubuntu 22 .deb file from https://posit.co/download/rstudio-desktop/ and install with 'software install'
And now, to change the version of R that RStudio uses is simple:
Opening Rstudio with the app icon always uses the default version of R. So to have Rstudio run a different R version you have 2 options:
- Change the default version of R (above) or ...
- Open Rstudio from the terminal after explicitly telling it which R version to use. This is switching of versions is temporary and ends as soon as the terminal is closed. Don't worry, you will still be looking at the Rstudio GUI with full functionality, all that has changed is how you open it, and the R version.
Here is how to do the second option:
export RSTUDIO_WHICH_R=/opt/R/4.2.3/bin/R # tell Rstudio which version to use from terminal
rstudio # now starting RStudio from the terminal runs R version 4.2.3 (clicking on the desktop icon will still use the default R version
INSTALL BIOCONDUCTOR
Sources:
Important:
- Each Bioconductor release is designed to work with a specific version of R
- R releases have 2 digits (e.g. 4.3), R updates have 3 digits (4.3.2)
- R has 1 release per year, but Bioconductor has 2 releases
- So for every R release (4.3), there are 2 bioconductor releases (3.17, 3.18)
So remember: If you require a specific bioconductor release, you must first install and switch to the R version it matches to!
### Open Rstudio and Install the latest release of bioconductor for the R version running:
install.packages("BiocManager", repos = "https://cloud.r-project.org")
BiocManager::version() # Verify version
BiocManager::install("DESeq2") # install a bioconductor package
### Switch to the other of the 2 releases for the R version currently running:
BiocManager::install(version="3.15") # Updates all packages to the specified Bioconductor version
BiocManager::version() # view bioconductor version
BiocManager::valid() # identify packages that are out-of-date or from unexpected version
Your tutorial required
sudo
and thus is not doable for a majority of people. Any alternative tosudo snap
?Thanks @Ram I'll add that to the intro. I'm the admin on my machine so I haven't found it necessary to install any other way. I'll try a fresh install as guest next time without sudo and see where the problems arise.
Perhaps add the word linux/ubuntu to the title? You have noted that the instructions are for Ubuntu in text.
Done. Thank you.
To use different versions of R and bioconductor, you don't really need RStudio. One can simply create new conda environments with the required versions of various bioC packages and the corresponding versions of R will automatically be installed. It is definitely a pain managing libraries for different versions of R but conda is the way to go.
Certainly true. Switching versions is way simpler that way, at the cost of the familiarity of the Rstudio IDE - especially if the code is written and only needs execution. While writing the code though, I def prefer Rstudio.
I prefer RStudio for new projects as well. The moment I need versions though, RStudio becomes a burden. I wish one could launch RStudio from the command line using something like
RStudio --R /path/to/R/executable --libpath /path/to/lib
You can, if you use RStudio via Docker, for example
bioconductor/bioconductor_docker
. I currently manage many different projects with this strategy, with different R and Bioconductor versions independent of any permanent host machine. Oldest project goes back to R 4.0.3 and Bioc 3.12. Together with Docker Hub this is conventient for software version control.Or better yet. To have an R package that allows access to conda environments via "conda list env" so that environments can be switched within Rstudio without a restart so a single Rscript could utilise multiple environments and versions. Much like in bash with
Way beyond my knowledge.
How is that sane, let alone "better"? Also, why use
snap
for curl andapt-get
for other stuff? Why not simplyapt-get
for everything?Some of us are still learning and fall foul to knowledge limitation fairly regularly. Luckily we have people to put us in our place.
Sorry, I see how harsh I come across. It's not good practice to use multiple versions of R in the same project. I can understand legacy project dependencies and why-fix-something-that-isn't-broken scenarios, but designing around these dependencies is asking for unmanageable headaches down the line.
That makes a lot more sense. Thank you.