How to Install R on Ubuntu 20.04

Choose a different version or distribution

Introduction

Before we begin talking about how to install R on Ubuntu 20.04, let's briefly understand – What is R?

R is a versatile programming language widely used for statistical analysis, data visualization, and machine learning. It provides a vast collection of libraries and tools that make it easy to explore, manipulate, and visualize data. With its user-friendly syntax and powerful features, R is favored by researchers, data scientists, and analysts.

Whether you're working with large datasets or conducting complex statistical modeling, R offers a comprehensive ecosystem that enables efficient data analysis. Embracing R can enhance your data-driven decision-making and unlock valuable insights from your data.

This tutorial describes how to install R on Ubuntu 20.04.

Advantages of R

  1. Versatile: R is a versatile language for statistical analysis, data manipulation, and machine learning.
  2. Extensive Libraries: R offers a vast collection of libraries for various data analysis tasks, enabling efficient and comprehensive data exploration.
  3. Data Visualization: R provides powerful tools for creating stunning and informative visualizations to communicate insights effectively.
  4. Community Support: R boasts a vibrant community of users who contribute packages, tutorials, and resources, fostering collaboration and learning.
  5. Integration: R seamlessly integrates with other programming languages and tools, making it easy to incorporate into existing workflows and systems.

Prerequisites to Install R on Ubuntu 20.04

Before proceeding with this tutorial, please ensure that you have met the following prerequisites:

  • Your computer has at least 1 GB of RAM. Or create a swap file instead.
  • You're logged in as a user with sudo access.

Installing R on Ubuntu

The R packages in the default Ubuntu repositories are frequently out of date. R will be downloaded from the CRAN repository.

Follow these steps to install R on Ubuntu 20.04:

Install the dependencies required to add a new HTTPS repository:

sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common

Add the CRAN repository to your list of system sources:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'

Install R by typing:

sudo apt install r-base

It may take a few minutes to complete the installation. When finished, print the R version to check it:

R --version
Output
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 is now installed on your Ubuntu machine and ready for use.

Compiling R Packages

The vast array of packages available through the Comprehensive R Archive Network is one of the main reasons R is so popular (CRAN).

To compile R packages, you must first install the build-essential package:

sudo apt install build-essential

To demonstrate, we'll install stringr, a package that provides fast, correct implementations of common string manipulations.

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

Open the R console:

sudo -i R
Output
>

Install the stringr package by typing:

install.packages("stringr")

It will take some time to install, but once done, load the library with:

library(stringr)

Make a basic character vector called tutorial:

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

Execute the following function to determine the length of a string:

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

More R packages can be found at Available CRAN Packages By Name and installed with install.packages().

ℹ️
You can install the R packages as Debian packages from the cran2deb4ubuntu repository instead of compiling them.

FAQs to Install R on Ubuntu 20.04

How can I install additional R packages on Ubuntu 20.04?

You can install additional R packages by running the command sudo apt-get install r-cran-PACKAGE in the terminal, replacing PACKAGE with the name of the package you want to install.

Can I install RStudio on Ubuntu 20.04?

Yes, you can install RStudio on Ubuntu 20.04. Visit the RStudio website, download the appropriate version for Ubuntu, and follow the installation instructions.

How do I update R on Ubuntu 20.04?

To update R on Ubuntu 20.04, open a terminal and run the command sudo apt-get update to update the package lists, and then run sudo apt-get upgrade r-base to upgrade the R package.

How can I uninstall R from Ubuntu 20.04?

To uninstall R from Ubuntu 20.04, open a terminal and run the command sudo apt-get remove r-base to remove the R package.

Can I use RStudio without installing R separately on Ubuntu 20.04?

No, RStudio is an integrated development environment (IDE) for R and requires R to be installed on your system before you can use it.

How can I check the installed version of R on Ubuntu 20.04?

Open a terminal and type R --version to display the installed version of R on your Ubuntu 20.04 system.

Conclusion

We've shown you how to install R and compile R packages on Ubuntu 20.04.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them.