Installing R on Ubuntu using the terminal

This excellent article on R bloggers gives the steps for installing the latest version of R. If one just tries sudo apt-get install before doing these steps, one will likely end up with R 3.0.2 (i.e. an old version).

Note: The steps assume you are using Ubuntu Xenial (Linux Mint 18). If not, just change "xenial" below to the appropriate name.

Step 1: Add latest R repository to list of repos

This step updates the file /etc/apt/sources.list:

sudo echo "deb xenial/" | sudo tee -a /etc/apt/sources.list

Step 2: Add R to keyring

These lines also help Ubuntu to get access to the latest version of R:

gpg --keyserver --recv-key E084DAB9
gpg -a --export E084DAB9 | sudo apt-key add -

Step 3: Install R

Now for the two lines you were expecting, which will install R. Again, if one skips ahead to just this step, one will probably only install an old version of R.

sudo apt-get update
sudo apt-get install r-base r-base-dev

Checking the install

If there were no error messages, then R should be installed correctly. However, one should check that it is correctly linked with the gcc compiler (if not, packages that require compilation cannot be installed). Whenever I install R, I first check that a couple of packages can be installed. Start with the following, which creates the folder ~/Rlib which gives you a place to install your packages, and then opens R:

if [ -d "~/Rlib" ]; then cd ~/Rlib; else mkdir ~/Rlib; cd ~/Rlib; fi
echo '.libPaths("~/Rlib/")' >> ~/.Rprofile

(The second line makes sure that R will look for packages installed in ~/Rlib.)

Installing xtable

And, now in R:

install.packages("xtable", lib=getwd())

You will be prompted to choose a repository; selecting one close to you (geographically) makes the downloading of the package files faster.

xtable is a light-weight package that should install quickly, and is very useful for turning dataframes into LaTeX-style tables. If the installation works, things are probably good.

xtable is a light-weight package that should install quickly, and is very useful for turning dataframes into LaTeX-style tables. If the installation works, you can probably install any package that only depends on R code (and does not need compilation).

Installing bindata

Now we will try install a package that requires compilation. In R,

install.packages("bindata", lib=getwd())

You should not be asked to choose a repository this time. bindata is a package that generates realisations of multivariate binomial random variables, and depends on mvtnorm, and requires compilation (it is written in C or C++, I think).

If it installs smoothly, then that is great news; all packages that require compilation should be able to be installed.

Installing devtools

If you want to make an R package, then devtools is a great package to use. However, there are sometimes problems installing it. Try:

install.packages("devtools", lib=getwd())

If you see some ERROR messages, it is probably because httr could not be installed, because there were problems with openssl and curl (Sigh).

Go to a terminal (split-panes with tmux?), and this first:

sudo apt-get update

Then the following commands will install curl (thanks to this post) and openssl:

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

Then, try again in the R pane:

install.packages("devtools", lib=getwd())

Hopefully the install will be smooth now, with no error messages, and if you try:


There should be no output/error message (which means it worked).

Note: it can be useful to use the dependencies flag:

install.packages("devtools", lib=getwd(), dependencies=TRUE)

Other useful packages

A few other packages I always like to install are roxygen2, testthat and Rcpp:

install.packages("roxygen2", lib=getwd())
install.packages("testthat", lib=getwd())
install.packages("Rcpp", lib=getwd())

Automatically loading packages

If you want to have some packages automatically loaded when you start up R, modify your ~/.Rprofile to look something like:

#to avoid pop-up for mirror when installing packages


As mentioned above, the need to install extra packages such as libssl-dev depends on your distribution. If you run into errors, scroll up and check the error messages, which are often informative.

Like the other posts, I can't claim credit for any of the steps, as they were all found on other blog/Stackoverflow posts. However, when setting up R on a new virtual machine, I sometimes couldn't find the (somewhat scattered) instructions, so I collected them all here. It is really only for my benefit, but perhaps someone else will find it useful.