How To Set up R programming language on Ubuntu 20.04

In this guide, we want to teach you How To Set up the R programming language on Ubuntu 20.04.

R is a programming language and environment commonly used in statistical computing, data analytics, and scientific research.

It is one of the most popular languages used by statisticians, data analysts, researchers, and marketers to retrieve, clean, analyze, visualize and present data.

Due to its expressive syntax and easy-to-use interface, it has grown in popularity in recent years.

The R programming language is used for:

  • Statistical inference
  • Data analysis
  • Machine learning algorithm

How To Set up R programming language on Ubuntu 20.04

Before you start to set up the R programming language, you need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article the Initial Server Setup with Ubuntu 20.04.

Now follow the steps below to complete this guide.

Install R programming language on Ubuntu 20.04

To install the latest version of the R on Ubuntu 20.04, you need to add the external repository maintained by CRAN.

CRAN is a network of FTP and web servers around the world that store identical, up-to-date, versions of code and documentation for R.

Note: CRAN maintains the repositories within their network, but not all external repositories are reliable. Be sure to install only from trusted sources.

First, you need to add the GPG key with the following command:

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

You will get the following output:

Output
Executing: /tmp/apt-key-gpghome.FBKeq1wx73/gpg.1.sh --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
gpg: key 51716619E084DAB9: public key "Michael Rutter <[email protected]>" imported
gpg: Total number processed: 1
gpg: imported: 1

Then, you can use the command below to add the repository on Ubuntu 20.04:

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

Now update your local package index with the following command:

sudo apt update

Next, you can use the following command to install the R programming language on Ubuntu 20.04:

sudo apt install r-base

As we’re planning to install an example package for every user on the system, we’ll start R as root so that the libraries will be available to all users automatically. Alternatively, if you run the R command without sudo, a personal library can be set up for your user.

Start your R with the command below:

sudo -i R
Output
R version 4.2.0 (2022-04-22) -- "Vigorous Calisthenics"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
...
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

This confirms that you have successfully installed the R programming language on Ubuntu 20.04 and entered its interactive shell.

Install R Packages

At this point, we want to show you install the R packages from CRAN.

For example, we will install textplot, a library that outputs ASCII graphs that include scatterplot, line plot, density plot, acf, and bar charts:

> install.packages('txtplot')

When your installation is completed, you can load your library with the command below:

> library('txtplot')

If there are no error messages, the library has successfully loaded.

If you are interested to learn more about txtplot, use help(txtplot) from within the R interpreter.

Any precompiled package can be installed from CRAN with install.packages(). To learn more about what’s available, you can find a listing of official packages organized by name via the Available CRAN Packages By Name list.

To exit R, you can type q(). You can press n when prompted unless you want to save the workspace image.

Conclusion

At this point, you learn to Set up R programming language on Ubuntu 20.04.

Hope you enjoy it.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!