It is an open-source programming language and free environment that specializes in statistical computing and graphical representation. It is mainly used by statistics and data miners to develop statistical software and conduct data analysis.
This article covers the steps needed to install R on Ubuntu 20.04.
Make sure you have fulfilled the following prerequisites before continuing with this tutorial:
- Your system has at least 1G of RAM. If not, create a swap file.
- You are logged in as a user with sudo privileges.
Install R on Ubuntu
The R packages that are included in the Ubuntu default repository often expire. We will install R from the CRAN repository.
To install R on Ubuntu 20.04, follow these steps:
1. Install the dependencies needed to add a new repository via HTTPS:
sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common
2. Add a CRAN repository to your system’s source list:
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/'
3. Install R by typing:
sudo apt install r-base
4. The installation may take several minutes to complete. When finished, verify by printing the 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 all, R has been installed on your Ubuntu machine, and you can start using it.
Arrange Package R
One of the main reasons why R is so popular is the variety of packages available through Comprehensive R 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 called stringr, which provides a fast and correct implementation of general string manipulation.
When started as root, packages will be installed globally and available to all system users. If you start R without sudo, a personal library will be prepared for your users.
Open console R:
Install the stringr package by typing:
The installation will take time and once completed, load the library with:
Create a simple character vector named tutorial:
tutorial <- c("How", "to", "Install", "R", "on", "Ubuntu", "20.04")
Run the following function which prints the length of the string:
 3 2 7 1 2 6 5
You can find more R packages in Available CRAN Packages By Name and install with install.packages ().
We have shown you how to install R on Ubuntu 20.04 and compile the R package.
If you experience problems or get feedback, leave a comment below.