Tutorial

How To Install R on Ubuntu 22.04

Published on May 5, 2022
How To Install R on Ubuntu 22.04

A previous version of this tutorial was written by Lisa Tagliaferri.

Introduction

An open-source programming language, R is widely used for performing data analysis and statistical computing. Supported by the R Foundation for Statistical Computing, it is an increasingly popular and extensible language with an active community. R offers many user-generated packages for specific areas of study, which makes it applicable to many fields.

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

Prerequisites

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

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

To learn how to achieve this setup, follow our initial server setup guide.

Once these prerequisites are in place, you’re ready to begin.

Step 1 — Installing R

Because R is a fast-moving project, the latest stable version isn’t always available from Ubuntu’s repositories, so you’ll start by adding the external repository maintained by CRAN.

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

To begin you’ll need to add the R project’s public GPG key to your server, and then configure the package repository. First you’ll download the key with the wget command and convert it to a format that apt can use to verify downloaded packages using the gpg --dearmor command.

Download the key and install it:

  1. 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 sources.list.d directory, where APT will search for new sources:

  1. 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] portion of the file instructs apt to use the key that you downloaded to verify repository and file information for R packages.

Next, update your package lists so APT will read the new R source:

  1. sudo apt update

Among the output that displays, you should identify lines similar to the following:

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 above appear in the output from the update command, you’ve successfully added the repository. Now you can be sure we won’t accidentally install an older version.

At this point, you’re ready to install R with the following command.

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

If prompted to confirm installation, press y to continue. The --no-install-recommends arguments ensures that no extra packages are installed.

As of the time of writing, the latest stable version of R from CRAN is 4.2.0, which is displayed when you start R.

Since this tutorial will explore installing an example package for every user on the system, 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.

  1. 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’ve successfully installed R and entered its interactive shell.

Step 2 — Installing R Packages from CRAN

Part of R’s strength is its available abundance of add-on packages. For demonstration purposes, you’ll install txtplot, a library that outputs ASCII graphs that include scatterplot, line plot, density plot, acf and bar charts:

  1. install.packages('txtplot')

Note: The following output shows where the package will be installed.

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

This site-wide path is available because we ran R as root. This location will make the package available to all users.

When the installation is complete, load the txtplot library:

  1. library('txtplot')

If there are no error messages, the library has successfully loaded. Let’s put it in action now with an example which demonstrates a basic plotting function with axis labels. The example data, supplied by R’s datasets package, contains the speed of cars and the distance required to stop based on data from the 1920s:

  1. 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

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

With R successfully installed on your server, you may be interested in this guide on installing the RStudio Server to bring an IDE to the server-based deployment you just completed. You can also learn how to set up a Shiny server to convert your R code into interactive web pages.

For more information on how to install R packages by leveraging different tools, you can read about how to install directly from GitHub, BitBucket or other locations. This will allow you to take advantage of the very latest work from the active community.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.