cat /etc/os-release22 R on Ubuntu
Installing R and RStudio with the Ubuntu default packages will leave you with strongly outdated versions. In practice, this often leads to version conflicts when installing additional R packages.
To avoid such conflicts, it makes sense to work with an up-to-date but not brand-new R version. As of now (summer 2020) I would recommend using v3.6, since most actively developed packages work with it, whereas support for v4 is often not yet provided.
Installing the right version of R requires that you know the Ubuntu code of your system. A lot of information on your system is stored in /etc/os-release:
Returns something like:
NAME="Linux Mint"
VERSION="19.3 (Tricia)"
ID=linuxmint
ID_LIKE=ubuntu
PRETTY_NAME="Linux Mint 19.3"
VERSION_ID="19.3"
HOME_URL="https://www.linuxmint.com/"
SUPPORT_URL="https://forums.ubuntu.com/"
BUG_REPORT_URL="http://linuxmint-troubleshooting-guide.readthedocs.io/en/latest/"
PRIVACY_POLICY_URL="https://www.linuxmint.com/"
VERSION_CODENAME=tricia
UBUNTU_CODENAME=bionicRelevant for the following is only the last line which could be assessed directly using e.g.
grep UBUNTU_CODENAME /etc/os-release
UBUNTU_CODENAME=bionicMore information on Ubuntu releases: https://wiki.ubuntu.com/Releases.
Installing R
In order to make sure that the R base will receive regular updates, it makes sense to install the package from a regularly updated repository. Knowing your Ubuntu code, you can easily find a fitting deb link on RStudio’s Ubuntu README page at https://cran.rstudio.com/bin/linux/ubuntu/README.html.
For example, for the ‘bionic’ example system shown above and R v3.6 the adequate entry is deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/ (cran35 is used for reasons of compatibility). This line must be added to /etc/apt/sources.list to ensure it is visited in system updates. You could use a text editor like nano, Emacs or vi to do so (sudo may be required) or simply append it through the command line:
echo "deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/" >> /etc/apt/sources.listTo make sure the installation works, the correct SSL key has to be added to your system:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9Now you can update the package library and upgrade existing files:
sudo apt-get update
sudo apt-get upgradeIn case no previous R version was installed, you can do it now:
sudo apt-get install r-base-devInstall some Ubuntu packages frequently used by R packages
sudo apt-get install libcurl4-openssl-dev libssl-dev libxml2-dev libudunits2-dev libgdal-dev cargo libfontconfig1-dev libcairo2-devInstall RStudio
RStudio provides its releases as DEB files. To install these, a tool like gdebi must be installed on Ubuntu systems. In case you don’t have it, yet, it can be installed using:
sudo apt-get install gdebi Find the right version for your system on the RStudio download page at https://rstudio.com/products/rstudio/download/#download and install it:
sudo gdebi /path/to/the/file/.debHow to handle version conflicts
From time to time, R packages will not work with the version of R you have installed. If your R version is lower than required by the package, such issues can often be resolved by installing a previous release of the package. This can be achieved with ‘devtools’. To install ‘devtools’, go to the R console and run:
install.packages("devtools")Before getting started, you need to find out which version of R you are running and which version of the package will work with it. R has a built-in variable version for this:
version$version.stringFinding out which version of the package should be used is a bit tougher. Navigate to the RDocumentation website at https://www.rdocumentation.org/ and find the relevant package there. Next to the name of the package, you will see the current version number and a button ‘Other versions’ next to it. At the bottom of the page, there is a section ‘Details’ with an entry ‘Depends’: this is where you can see which R version the package requires. You may find the latest compatible version by selecting older versions from the dropdown at the top of the page until you find a package version that supports your version of R.
Let’s, for example, say you are working on a machine with R version 3.1 and you want to install a compatible version of ggplot2. In this case, you will find that ggplot2 v.3.1.1 is the last version that supported R 3.1.
Using devtools, you can now install this version of ggplot2:
require(devtools)
install_version("ggplot2", version = "3.1.1")For more information and a guide on how to install packages from source, please refer to this post on “Installing older versions of packages” on the RStudio Support website at https://support.rstudio.com/hc/en-us/articles/219949047-Installing-older-versions-of-packages.