R on Ubuntu featured image

Installing R on Ubuntu 21.04: A Tutorial

R is a programming language that specializes in working with data. R is free software that supports an extensive catalog of statistical and graphical methods. The list includes various machine learning algorithms, time series, linear regression, and more. It’s used by industry giants like Google, Facebook, Airbnb, Uber, etc.

As the description suggests, R is the go-to option when big data is involved. It’s used for data analysis, machine learning, and statistical inference, to name a few. In this guide, we will go through the steps of installing R on Ubuntu 21.04.

Prerequisites

Here are a couple of prerequisites required to perform the steps demonstrated in this guide:

R on Ubuntu

There are two ways of installing R on Ubuntu:

  • Ubuntu package repo: Because it’s a popular language, R is directly available from the official Ubuntu package repositories. However, because of the way Ubuntu releases package updates, it’s often not the latest version of R.

  • CRAN (The Comprehensive R Archive Network) project: CRAN is a network of FTP and web servers that serve the up-to-date versions of code and documentation of R for various platforms. It offers a dedicated repo for Ubuntu and it comes with the latest version of R.

We will demonstrate both methods of installing R. However, it’s strongly recommended to install R from the CRAN repo as it offers the latest packages. Whatever method you choose, R will be installed and available for all users of the system.

  • Method 1 – Installing R from Ubuntu Default Repo

First, open up the terminal and update the APT cache:

R on Ubuntu screenshot 1

Next, install R from the Ubuntu package repo:

R on Ubuntu screenshot 2
  • Method 2 – Installing R from CRAN Repo

The CRAN repo supports Ubuntu LTS releases for the most part. Check the CRAN documentation on Ubuntu repo to see if your version is supported.

Step 1- Adding the CRAN Repo

To add the CRAN repo, open up a terminal, and run the following commands. First, update the APT cache:

R on Ubuntu screenshot 3

There are two helper packages needed to work with CRAN and R. Install them right away:

R on Ubuntu screenshot 4

Next, we need to add the signing key for the CRAN repo. Download and add the key:

12 2 1

We can also verify the key if it was the right one. The fingerprint should be E298A3A825C0D65DFD57CBB651716619E084DAB9:

R on Ubuntu screenshot 5

Finally, add the CRAN repo for Ubuntu:

R on Ubuntu screenshot 6

Update the APT cache:

R on Ubuntu screenshot 7
Step 2 – Installing R from CRAN

Now, APT will download and install R from the CRAN repo. Install R:

R on Ubuntu screenshot 8

Working with R Shell

In this section, we will have a simple demonstration of using the R shell. We will install an R package stringr. It comes with a set of cohesive functions designed to transform working with strings as simple as possible. First, check out stringr here.

Launch the R shell:

12 7 1

Then, you can install the package:

12 8 1

Once the installation is complete, load stringr into the current R session:

12 9 1

One function of stringr is str_length that will print the length of strings. Create a string:

12 10

Now, check the length of each of the strings:

12 11

Like Linux, R also comes with a neat documentation feature for its packages. Check out the documentation of string:

12 12

To exit the R shell, use the following code:

12 14

Final Thoughts

This guide successfully demonstrates configuring and installing R on Ubuntu 21.04 server. The entire procedure is simple and quick. It also features a quick demonstration of using the R shell and working with R packages. For more information on how to take control of your R code, check out R Studio – an open-source IDE for R.

Happy Computing!