Installing R Properly

Table of Contents

Installing R Properly on Ubuntu

Introduction

There are a lot of ‘wrong’ ways to install R. Usually the result is a system on which R does not update (e.g. installed from source), does not update as fast as upstream (e.g. installed from package manager in Debian or Ubuntu), or requires additional tools (e.g. Bioconda). These aren’t necessarily wrong, and you may need to do these at times (e.g. if you write R packages that are on CRAN, then you may need to test on a dev version, which may need to be built from source), but there is a way that is better in most cases.

CRAN

The following way of installing R is in fact the way that is described by CRAN, to add the CRAN repos to your package manager, update, and install. After doing this, then R will be properly kept up to date when you update your other system packages. They describe the steps here. Unfortunately, that link does not go directly to their Ubuntu install instructions because they’ve done something silly to their site so navigation isn’t reflected in the url.
After reaching that page, click “Download R for Linux” and then “ubuntu/” Now, below the Directory we should see the Ubuntu installation instructions. CRAN is the Comprehensive R Archive Network, they maintain the main package repositories for R. They uphold coding standards for R packages and try to ensure that R packages are tested and useful. As such, they are the authority on installing R.

Why These Instructions?

Unfortunately, the CRAN instructions are potentially daunting for a beginner to Ubuntu. Including that one of the steps will fail and it might not be clear to a beginner that the solution is given on the same page. I hope that this slightly more concrete version is useful.

What is my Ubuntu Version?

CRAN says:

"To obtain the latest R 3.6 packages, add an entry like

deb https://cloud.r-project.org/bin/linux/ubuntu disco-cran35/

or

deb https://cloud.r-project.org/bin/linux/ubuntu cosmic-cran35/

or

deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/

or

deb https://cloud.r-project.org/bin/linux/ubuntu xenial-cran35/

or

deb https://cloud.r-project.org/bin/linux/ubuntu trusty-cran35/

in your /etc/apt/sources.list file."  

Great… so which one do we use, and why? These lines are each for a repository containing R built for a certain version of Ubuntu, with the codename of the version appearing just before the ‘-cran35/’. We can check which Ubuntu we are using with the command lsb_release -a, which will also show the codename (you can see all the codenames here). I haven’t updated my operating system in a while, so I’m still on Ubuntu 16.04, or Xenial. So in my case, I need to add the line “deb https://cloud.r-project.org/bin/linux/ubuntu xenial-cran35/” without the quotes to the end of my /etc/apt/sources.list file. I will also add a comment (comments in this file start with ‘#’) so I remember what this repo is for.

The sources.list File

The default sources.list file will be something like this:

#deb cdrom:[Ubuntu 16.04.2 LTS _Xenial Xerus_ - Release amd64 (20170215.2)]/ xenial main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://us.archive.ubuntu.com/ubuntu/ xenial main restricted
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://us.archive.ubuntu.com/ubuntu/ xenial-updates main restricted
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://us.archive.ubuntu.com/ubuntu/ xenial universe
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial universe
deb http://us.archive.ubuntu.com/ubuntu/ xenial-updates universe
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to 
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://us.archive.ubuntu.com/ubuntu/ xenial multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial multiverse
deb http://us.archive.ubuntu.com/ubuntu/ xenial-updates multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://us.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu xenial partner
# deb-src http://archive.canonical.com/ubuntu xenial partner

deb http://security.ubuntu.com/ubuntu xenial-security main restricted
# deb-src http://security.ubuntu.com/ubuntu xenial-security main restricted
deb http://security.ubuntu.com/ubuntu xenial-security universe
# deb-src http://security.ubuntu.com/ubuntu xenial-security universe
deb http://security.ubuntu.com/ubuntu xenial-security multiverse
# deb-src http://security.ubuntu.com/ubuntu xenial-security multiverse

Source

After editing it (need to edit as root, aka with sudo, e.g. sudo nano /etc/apt/sources.list ), it should be like this:

#deb cdrom:[Ubuntu 16.04.2 LTS _Xenial Xerus_ - Release amd64 (20170215.2)]/ xenial main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://us.archive.ubuntu.com/ubuntu/ xenial main restricted
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://us.archive.ubuntu.com/ubuntu/ xenial-updates main restricted
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://us.archive.ubuntu.com/ubuntu/ xenial universe
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial universe
deb http://us.archive.ubuntu.com/ubuntu/ xenial-updates universe
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to 
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://us.archive.ubuntu.com/ubuntu/ xenial multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial multiverse
deb http://us.archive.ubuntu.com/ubuntu/ xenial-updates multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://us.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src http://us.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu xenial partner
# deb-src http://archive.canonical.com/ubuntu xenial partner

deb http://security.ubuntu.com/ubuntu xenial-security main restricted
# deb-src http://security.ubuntu.com/ubuntu xenial-security main restricted
deb http://security.ubuntu.com/ubuntu xenial-security universe
# deb-src http://security.ubuntu.com/ubuntu xenial-security universe
deb http://security.ubuntu.com/ubuntu xenial-security multiverse
# deb-src http://security.ubuntu.com/ubuntu xenial-security multiverse

#R CRAN packages for Ubuntu 16.04
deb https://cloud.r-project.org/bin/linux/ubuntu xenial-cran35/

Install R

Now we can update our list of available packages:
sudo apt-get update
and install R:
sudo apt-get install r-base
… but not really, this will fail because the needed key to trust packages from the CRAN repo isn’t installed. The CRAN instructions do cover how to do this, but they give the directions out of order for some reason, putting, “Secure APT” down below.

So let’s try that again, it should work this time:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
sudo apt-get update
sudo apt-get install r-base

Other Optional (for now) Dependencies

Unfortunately, there are some R packages that need some libraries that r-base doesn’t include in its dependencies, so I also recommend making sure that you have some version of curl and ssl installed. You don’t have to do this now, but it is a problem you might encounter later while installing an R package.

sudo apt-get install libcurl4-openssl-dev libssl-dev libxml2-dev

Other Operating Systems/Versions

These package names may change in different versions of Ubuntu, and may be different on other operating systems.

Finding what they are is not always trivial. In general, the first step is to find out how your operating system names packages in general, for instance, Ubuntu names libraries starting with ‘lib’, which is useful for us to know so that we don’t start searching as ‘curl…’ Also check how they name packages for developers, because that what we need here, e.g. Ubuntu names them with the suffix ‘-dev’.

Kunji avatar
Kunji
Biophysics, Mathematics, Statistical Genetics, Computer Science.