Jun 24, 2023 5 min read

How to Install R on Ubuntu 22.04

Install R on Ubuntu 22.04 with our step-by-step tutorial. It is widely used for statistical analysis, data visualization, and machine learning.

Install R on Ubuntu 22.04
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install R on Ubuntu 22.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.

In this tutorial, you will install R and how to add packages from the official Comprehensive R Archive Network (CRAN).

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 22.04

To follow along with this tutorial, you will need an Ubuntu 22.04 server with the following configuration:

  • A non-root user with sudo privileges and at least 1GB of RAM

You are now ready to begin once you have fulfilled these conditions.

Step 1 - Installing R

Because R is a fast-moving project, the most recent stable version is not always available from Ubuntu's repositories, thus you will begin by adding the external repository managed by Comprehensive R Archive Network (CRAN).

👉
It should be noted that CRAN maintains the repositories within their network, however not all external repositories are secure. Make sure you only install from reliable sources.

You must first configure the package repository before adding the public GPG key for the R project to your server. To begin, use the wget command to obtain the key, then use the gpg --dearmor command to convert it into a format that apt can use to verify downloaded packages.

Download and install the key:

wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo gpg --dearmor -o /usr/share/keyrings/r-project.gpg

Next, add the R source list to the directory sources.list.d, where APT will look for new sources:

echo "deb [signed-by=/usr/share/keyrings/r-project.gpg] https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/" | sudo tee -a /etc/apt/sources.list.d/r-project.list

The [signed-by=/usr/share/keyrings/r-project.gpg] section asks apt to use the key you downloaded to verify R package repository and file information.

After that, make sure APT can read the latest R source by updating your package lists:

sudo apt update

You should search for lines that resemble the following in the output that appears:

Output
...
Get:7 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ InRelease [3622 B]                  
Get:8 https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ Packages [15.6 kB]
...

If the lines mentioned above are present in the output of the update command, the repository has been added successfully. You may be confident that we will not unintentionally install an earlier version now.

You may now execute the following command to install R.

sudo apt install --no-install-recommends r-base

If asked to confirm installation, select y to proceed. The --no-install-recommends argument ensures that no additional packages are installed.

When you start R, the most recent stable version available from CRAN is 4.2.0 as of the time of writing.

Because this guide will go over installing an example package for each user on the system, run R as root so that the libraries are automatically available to all users. A personal library can also be created for your user if you execute the R command without sudo.

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 verifies that R was successfully installed and that you were able to access its interactive shell.

Step 2 – Installing R Packages from CRAN

R has a ton of available add-on packages, which is one of its strengths. Install txtplot, a library that generates ASCII graphs such as scatterplot, line plot, density plot, acf, and bar charts, for demonstration purposes:

install.packages('txtplot')

Note: where the package will be installed is shown in the output that follows.

Output
...
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
. . .

We run R as root so we have access to this site-wide path. This location will make the package accessible to all users.

After the installation is finished, load the txtplot library:

library('txtplot')

If there are no error messages, the library has been loaded successfully. Let us put it into practice by using an example that exemplifies a fundamental plotting function with axis labels. The datasets package's example data comprises the speed of cars and the distance required to stop based on data from the 1920s:

txtplot(cars[,1], cars[,2], xlab = 'speed', ylab = 'distance')
Output
     +----+-----------+------------+-----------+-----------+--+
  120 +                                                   *    +
      |                                                        |
d 100 +                                                   *    +
i     |                                    *                *  |
s  80 +                          *         *                   +
t     |                                       * *    *    *    |
a  60 +                          *  *      *    *      *       +
n     |                        *         * *  * *              |
c  40 +                *       * *    *  *    * *              +
e     |         *      *  * *  * *  *                          |
   20 +           *    *  * *       *                          +
      |  *      *    *                                         |
    0 +----+-----------+------------+-----------+-----------+--+
           5          10           15          20          25   
                                speed       

Use help(txtplot) within the R interpreter to learn more about txtplot if you are interested.

With install.packages(), you can install any precompiled package from CRAN. To learn more about what is available, see the Available CRAN Packages By Name list to see a list of official packages arranged by name.

You can enter q() to exit R. If you do not wish to save the workspace image, you can press n when asked.

FAQs to Install R on Ubuntu 22.04

How can I install additional packages in R on Ubuntu?

To install packages, open the R console and use the command: install.packages('package_name'). Replace package_name with the desired package.

Can I install specific versions of R on Ubuntu 22.04?

Yes, you can install specific R versions. Visit the CRAN website, find the desired version, and follow the installation instructions provided.

How do I update R to the latest version on Ubuntu?

To update R to the latest version, open the terminal and run the command: sudo apt-get update && sudo apt-get upgrade r-base.

How can I uninstall R from Ubuntu?

To uninstall R, open the terminal and execute: sudo apt-get purge r-base followed by rm -rf ~/.R to remove user-specific configurations.

Can I use RStudio with R on Ubuntu 22.04?

Yes, you can install RStudio on Ubuntu 22.04. Download the RStudio package from their website and follow the installation instructions.

How do I switch between multiple R versions on Ubuntu?

Use the update-alternatives command in the terminal to switch between installed R versions. Consult the Ubuntu documentation for detailed instructions.

Is it necessary to install additional dependencies for R on Ubuntu?

The r-base package includes most necessary dependencies. However, some packages may require additional system libraries, which you can install as needed.

Conclusion

Hope this detailed guide helped you understand how to install R on Ubuntu 22.04.

If you have any suggestions or queries, kindly leave them in the comments section.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.