// Tutorial //

How To Install R on Ubuntu 18.04

Published on July 5, 2018 · Updated on May 14, 2020
Default avatar
By Lisa Tagliaferri
Developer and author at DigitalOcean.
How To Install R on Ubuntu 18.04
Not using Ubuntu 18.04?Choose a different version or distribution.
Ubuntu 18.04

A previous version of this tutorial was written by Melissa Anderson.

Introduction

R is an open-source programming language that specializes in statistical computing and graphics. Supported by the R Foundation for Statistical Computing, it is widely used for developing statistical software and performing data analysis. 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, we 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 18.04 server with:

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

To learn how to achieve this setup, follow our manual initial server setup guide or run our automated script.

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 we’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.

Let’s first add the relevant GPG key.

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

When we run the command, we’ll receive the following output:

Output
Executing: /tmp/apt-key-gpghome.4BZzh1TALq/gpg.1.sh --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9 gpg: key 51716619E084DAB9: public key "Michael Rutter <marutter@gmail.com>" imported gpg: Total number processed: 1 gpg: imported: 1

Once we have the trusted key, we can add the repository.

Note that if you’re not using 18.04, you can find the relevant repository from the R Project Ubuntu list, named for each release. Ubuntu 18.04 is named Bionic Beaver, and the most recent version of R is 4.0, hence the naming convention of ubuntu bionic-cran40.

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

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

Output
... Get:4 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease [3626 B] ... Get:22 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ Packages [20.4 kB] ...

Now, we’ll need to run update after this in order to include package manifests from the new repository.

  1. sudo apt update

Among the output should be a line similar to the following:

Output
... Hit:1 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease ...

If the line above appears in the output from the update command, we’ve successfully added the repository. We can be sure we won’t accidentally install an older version.

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

  1. sudo apt install r-base

If prompted to confirm installation, press y to continue.

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

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

  1. sudo -i R
Output
R version 4.0.0 (2020-04-24) -- "Arbor Day" Copyright (C) 2020 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 we’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, we’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 is the correct location to make the package available to all users.

When the installation is complete, we can load txtplot:

  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(). Unless you want to save the workspace image, you can press n when prompted.

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.


Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?
2 Comments

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!

Thank you for these instructions. I am running Ubuntu 18.04:

No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 18.04.4 LTS Release: 18.04 Codename: bionic

I have followed the instructions on your web page. I get the following error:

Reading package lists… Done Building dependency tree
Reading state information… Done Some packages could not be installed. This may mean that you have requested an impossible situation or if you are using the unstable distribution that some required packages have not yet been created or been moved out of Incoming. The following information may help to resolve the situation:

The following packages have unmet dependencies: r-base : Depends: r-base-core (>= 4.0.0-1.1804.0) but it is not going to be installed Depends: r-recommended (= 4.0.0-1.1804.0) but it is not going to be installed Recommends: r-base-html but it is not going to be installed E: Unable to correct problems, you have held broken packages.

Any suggestions on how to fix this? Thanks! GA

Hello! I got problems installing R on ubuntu 19.04. I changed the repository, but continue with the same problem.

mario@mario-OptiPlex-7010:~$ sudo apt-get install r-base Leyendo lista de paquetes… Hecho Creando árbol de dependencias
Leyendo la información de estado… Hecho No se pudieron instalar algunos paquetes. Esto puede significar que usted pidió una situación imposible o, si está usando la distribución inestable, que algunos paquetes necesarios aún no se han creado o se han sacado de «Incoming». La siguiente información puede ayudar a resolver la situación:

Los siguientes paquetes tienen dependencias incumplidas: r-base : Depende: r-base-core (>= 3.6.0-2xenial) pero no va a instalarse Depende: r-recommended (= 3.6.0-2xenial) pero no va a instalarse Recomienda: r-base-html pero no va a instalarse E: No se pudieron corregir los problemas, usted ha retenido paquetes rotos.

Do you have any suggestions to solve the problem?

Iván