A Recipe for Upgrading R

This is the recipe I use to upgrade R on a Linux box. It’s something that I do fairly frequently on fresh EC2 instances.

Remove Old R

Check for existing R and packages.

dpkg -l | grep -E "(r-base-core|cran)"

Remove existing R installation.

sudo apt-get purge -y r-base-core

Check again for existing R packages: they should be gone.

Add Keys

Add keys.

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 51716619E084DAB9

If you’re behind a firewall you can try something like this:

gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-key E084DAB9
gpg -a --export E084DAB9 | sudo apt-key add --

Add CRAN Repository

OSNAME=$(. /etc/os-release; echo "$ID")
OSVERS=$(lsb_release -cs)
sudo add-apt-repository "deb http://cran.rstudio.com/bin/linux/$OSNAME $OSVERS-cran40/"

Update the package list.

sudo apt-get -y update

Install New R

sudo apt-get -y install r-base

If you want to remove the packages installed in the process then you can do this:

sudo apt-get remove -y 'r-cran-.*'

Other Stuff

Install some system packages that are required for building various R packages.

sudo apt-get -y install \
  htop \
  libcurl4-openssl-dev \
  libssl-dev \
  libgeos-dev \
  libgdal-dev \
  libproj-dev \
  libxml2-dev \
  libudunits2-dev \
  libsodium-dev \
  pandoc \
  openjdk-8-jdk \
  openjdk-8-jre \
  cargo \
  libcairo2-dev \
  libfreetype6-dev \
  libclang-dev \
  ttf-mscorefonts-installer \
  fonts-roboto

Configuring Java

Where does R expect to find Java?

sudo R CMD javareconf