How to install R on Ubuntu 20.04?

Overview

R has quickly established itself as a high-quality open-source programming language that can run on almost all operating systems. It can be used to analyze data, build statistical models, and make predictions. It also has a solid library of packages for data manipulation, statistical analysis and visualization, graphics, as well as data management.

The following tutorial will demonstrate the steps to install R on Ubuntu 20.04 and to add the most popular R packages from CRAN (Comprehensive R Archive Network).

Prerequisites

There are certain prerequisites that need to be met before you begin.

  • Ubuntu 20.04 LTS configured with non-root sudo user privileges.

  • At least 1GB of RAM.

Get Started

Step 1: Install R

The latest stable version of R is available from CRAN, so we will use that as our starting point. But, before you install it on Ubuntu, be sure to add an external repository maintained by CRAN. This will ensure that you have access to the latest version of R without having to wait for it to be released into Ubuntu's repositories.

  • Add the appropriate GPG key to avoid any impersonation attacks:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

Output:

  • Now, that you have obtained the trusted key, you can add the repository using the following command:

sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'

Note: As Ubuntu is frequently referred to as Focal Fossa, the repository for Ubuntu 20.04 is referred to as focal-cran40 as shown in the command above. But, if you are using an earlier version of Ubuntu, you can refer to the appropriate repository for your OS from R Project Ubuntu Releases.

Output:

  • Once the new repository is added, run the following command to incorporate the package manifest files from it.

sudo apt update

Output:

Note: The repository has been successfully added if you see the line ' https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/ InRelease ' in the output of the update command.

  • Begin with installing R using the following command:

sudo apt install r-base

Note: Enter y if prompted to continue with the installation.

  • You can start using R as root using the following command. As we need to install a sample package for each system user, this command will help users access all the libraries automatically.

sudo -i R

Output:

This verifies that R was successfully installed, and we can access its interactive shell.

Step 2: Install packages of R from CRAN

Now that tools like txtplot enable the creation of clear data-driven graphs of one or more variables in only a few lines of code, conducting statistical research in R has become simple and clear. The txtplot package was designed to display data using ASCII graphs and its package includes line plots, density plots, acf, bar graphs, and scatterplots.

  • To install txtplot, use the following command:

> install.packages('txtplot')

The output displays the location of the package installation.

  • After successful installation, load txtplot using the library command.

> library('txtplot')

Step 3: Demonstration

  • If the library is successful in loading, no error messages will be displayed, and you can try txtplot with the below example that illustrates a plotting function.

Note: The sample dataset, provided by R's package known as datasets includes the count of telephones for multiple world regions (in 1000s).

> txtplot(WorldPhones, xlab="Year", ylab="Number of telephones(1000's)")

Output:

  • You can also use the following command within the R interpreter to learn more about txtplot functions.

> help('txtplot')

Conclusion

The method of installing R that we have explored here takes only a few minutes to have the language ready for use on your Ubuntu system and seems to work best for most programmers. You will now be able to perform statistical analysis of data using R.

Last updated