How To Install R on Ubuntu 20.04

In this tutorial you will learn how to install R on Ubuntu 20.04 Focal Fossa Server/Desktop.

In this tutorial you will learn:

  • How to install R
  • How to install latest R version
  • How to add official R-Project repository
  • How to install R library

GNU R on Ubuntu 20.04 Focal Fossa

GNU R on Ubuntu 20.04 Focal Fossa

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Installed Ubuntu 20.04 or upgraded Ubuntu 20.04 Focal Fossa
Software R
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How To Install R on Ubuntu 20.04 step by step instructions

  1. Update the package index:
    $ sudo apt update

    Ubuntu repository contains stable version R packages. However, optionally if you wish to install the latest GNU R version add the official R-Project repository:

    $ sudo apt-key adv --keyserver --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
    $ sudo add-apt-repository 'deb eoan-cran35/'
  2. Install R:
    $ sudo apt install r-base
  3. confirm the R installation by checking its version:
    $ R --version
  4. Start using R:
    $ R
  5. (optional) Install RStudio, the integrated development environment for R.
    Ubuntu repository contains approximately 860 ready to install and use stable library R packages. This means that in most cases you do not need to install and compile R library from source using R shell. In fact this is not recommended at all! All you need is to install an appropriate r-cran package by using the apt command. See bellow.

    To install additional libraries using apt command, first search the package index for available packages. For example let’s see if the R library ggplot2 is available:

    $ apt-cache search r-cran | grep ggplot2
    r-cran-cowplot - GNU R streamlined plot theme and plot annotations for 'ggplot2'
    r-cran-egg - GNU R extensions for 'ggplot2': custom geom, custom themes, plot
    r-cran-ggally - GNU R extension to r-cran-ggplot2
    r-cran-ggdendro - GNU R create dendrograms and tree diagrams using 'ggplot2'
    r-cran-ggforce - accelerating GNU R ggplot2
    r-cran-ggplot2 - implementation of the Grammar of Graphics
    r-cran-ggridges - Ridgeline Plots in 'ggplot2'
    r-cran-ggthemes - extra themes, scales and geoms for r-cran-ggplot2

    To install the actual R library package enter:

    $ sudo apt install r-cran-ggplot2
  6. Test the library installation by loading it from R shell:

    $ R
    > library(ggplot2)