Installing Software on Ubuntu »How to Install R on Ubuntu 20.04

R is an open source programming language and free environment that specializes in statistical computing and graphing. It is mainly used by statisticians and miners to develop statistical software and data analysis.

This article describes the steps required to install R on Ubuntu 20.04.

Prerequisites

Make sure you have met the following prerequisites before continuing with this article:

  • Your system has at least 1 GB of RAM. Otherwise, create a swap file.
  • You are logged in as a user with sudo privileges.

Installing R on Ubuntu

The R packages included in the standard Ubuntu repositories often contain older packages. We will install R from the CRAN repository.

To install R on Ubuntu 20.04, follow these steps:

  1. Install the dependencies required to add new storage over HTTPS:
    sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common
  2. Add the CRAN repository to your system’s source list:
    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'
  3. Install R by typing:
    sudo apt install r-base
  4. Installation may take several minutes. When finished, check this by printing the R version:
    R --version
    R version 4.0.1 (2020-06-06) -- "See Things Now"
    Copyright (C) 2020 The R Foundation for Statistical Computing
    Platform: x86_64-pc-linux-gnu (64-bit)
    ...

That’s it, R has been installed on your Ubuntu machine and you can start using it.

Compiling R packages

One of the main reasons for R’s popularity is the wide range of packages available through the R Comprehensive Archive Network (CRAN).

To be able to compile R packages, you need to install the build-essential package:

sudo apt install build-essential

For demonstration purposes, we will install a package named stringr, which provides quick and correct implementations of common string manipulations.

When run as root, the packages will be installed globally and available to all users on the system. If you start R without sudo, a personal library is created for your user.

Open the R console:

R
>

Install the stringr package by typing:

install.packages("stringr")

The installation will take some time, and after it completes download the library:

library(stringr)

Create a simple symbolic vector named tutorial:

tutorial <- c("How", "to", "Install", "R", "on", "Ubuntu", "20.04")

Run the following function, which prints the length of the string:

str_length(tutorial)
[1] 3 2 7 1 2 6 5

You can find more R packages in the available CRAN packages section by name and install them using install.packages ().

Output

We showed you how to install R on Ubuntu 20.04 and compile the R packages.

If you run into an issue or have feedback, please leave a comment below.

Sidebar