Installation of R 3.5 on Ubuntu 18.04 LTS and tips for spatial packages

migration-to-r3.5-in-ubuntu-header
Author : Sébastien Rochette
Categories : maps, server, ubuntu
Tags : installation
Date :

Do you plan to upgrade your server installation from Ubuntu 16.04 to Ubuntu 18.04 LTS ? It is also the best time to migrate to R 3.5 ! By the way, if you always found difficult to install R packages for geographical data on Ubuntu, this time is over. Just follow the guide…

Note that this tutorial also works for R 3.6 with some adjustments

New Ubuntu version and new R version

ubuntu-r

If you use Ubuntu on your laptop or on your servers in your company, chances are high that you use a Long Term Support (LTS) version. Using a LTS version is safer for companies having stability constraints. This avoids possible breaking changes that would compromise your workflows and thus your business. Indeed, if you work with spatial data in R on Ubuntu, you probably also noticed that installation using recent versions of packages like gdal, geos, proj or netcdf was only possible if you are working on a LTS version of Ubuntu using UbuntuGIS PPA. This month, the first point release of Ubuntu 18.04.1 Bionic Beaver for server or desktop is available for download. If you are still using Ubuntu 16.04 you will now be proposed this upgrade.

During the last few months, you also heard about the release of R 3.5 as announced by the R Core team. This introduced lots of performance improvements thanks to the ALTREP. Now that the first maintenance release R 3.5.1 is out, you may also want to put it in production at work.

While your are playing with a series of sudo apt update commands, why not do both upgrades at the same time ?

Upgrade to Ubuntu 18.04

ubuntu-logoAs always, be sure to have some backups of your files before starting migration ! If you are running Ubuntu 16.04 LTS, you may see the new release using the “update core manager”. Be sure it is installed :

sudo apt-get install update-manager-core

then run

sudo do-release-upgrade

This should work if you allowed only LTS versions to be installed. In my own case, maybe I was not patient enough, but the new version was not proposed. Then you can temporarily allow for “normal” release, which, here, will propose you the last version available 18.04. The one we want. Be careful with this change. If you are using your computer at work in production mode, set it back to “lts” as soon as you have the LTS installed.

Check /etc/update-manager/release-upgrades and change the line:

Prompt=lts

to:

Prompt=normal

Then you can run sudo do-release-upgrade

Follow instructions and you’re done for the Ubuntu upgrade.

Delete previous version of R

ubuntu-r

Remove additional repositories

To be able to use R 3.5, you will have to delete everything about the previous R version.

We first need to check if you used one of the R mirror server. Maybe you do, maybe you don’t… The R mirror server will be one of these: https://cran.r-project.org/mirrors.html. The name of this server can be listed in the file “/etc/apt/sources.list” that you can edit with:

In the terminal:

sudo vi /etc/apt/sources.list

Find the line(s) that looks like deb https://mirror.ibcp.fr/pub/CRAN/ xenial/ and comment it with a #.

If you did not find these lines, the server may be listed in an external file listed in “/etc/apt/sources.list.d”. Find the “name.of.file.with.a.mirror.name” (replace this path with the appropriate one) and delete the files.

In the terminal:

ls /etc/apt/sources.list.d
sudo rm -i /etc/apt/sources.list.d/name.of.file.for.mirror.list

If you did not find it, you probably installed R using the Ubuntu default servers. Then, you do not have anything else to do.

Remove Ubuntu packages for R

Now you need to remove all Ubuntu packages for R and clean your installation.

In the terminal:

sudo apt purge r-base* r-recommended r-cran-*
sudo apt autoremove
sudo apt update

R should be totally removed after that.

Install new version of R (3.5)

ubuntu-r

Set new Ubuntu repositories for R 3.5

Detailed installation instructions are on https://cran.r-project.org/bin/linux/ubuntu/. Thanks to Dirk Eddelbuettel and Michael Rutter, installing R on Ubuntu is a child’s play !
We will use the new mirror https://cloud.r-project.org, which will be automatically redirected to a nearby CRAN mirror.

In the terminal:

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

You may need the keys:

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

In case of problem with the “keyserver”, you can use these commands:

gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

Then add the apt-key

gpg -a --export E298A3A825C0D65DFD57CBB651716619E084DAB9 | sudo apt-key add -

Keys can evoluate, in this case, you can have a look at the maintained page:  https://cran.r-project.org/bin/linux/ubuntu/#secure-apt

Install R 3.5

Very few deb packages are necessary:

In the terminal:

sudo apt install r-base r-base-core r-recommended

Install other packages binaries with Ubuntu

On a personal level, I have some R packages installed with an Ubuntu PPA (Personal Package Archives) because I trust maintainers and I am sure this R packages will work with my Ubuntu distribution. They require Linux dependencies that can be complicated to install. This is the case for mapping packages or those with java in them.
With R 3.5, Ubuntu packages required to be rebuilt differently than with previous version. Michael Rutter explains this on his dedicated blog. Hence, you’ll need to use his PPA before installing Ubuntu packages for R (starting with “r-cran-”).

In the terminal (my personal selection):

sudo add-apt-repository ppa:marutter/c2d4u3.5
sudo apt-get update
sudo apt install r-cran-rgl r-cran-rjags r-cran-snow r-cran-ggplot2 r-cran-igraph r-cran-lme4 r-cran-rjava r-cran-devtools r-cran-roxygen2 r-cran-rjava r-cran-xlsx

Install packages for spatial data analyses in Ubuntu

ubuntu-carto-rInstallation of R packages like {sf}, {sp}, {rgdal}, {rgeos}, … may require some external Ubuntu packages. This has been made easy with the UbuntuGIS PPA. The maintainers team is small and it may takes some time before packages are released for your distribution. Only Ubuntu LTS distributions are available. Also, as of today, packages for Ubuntu 18.04 are not available: if you have the skills, you may help them!.

With Ubuntu 16.04, UbuntuGIS is necessary for the installation of the spatial R packages like {sf}. With Ubuntu 18.04, Ubuntu default versions of dependencies are sufficiently up-to-date, but you still need to install them.

Whatever is the Ubuntu LTS distribution you are using, you’d better install UbuntuGIS PPA to be able to work with R packages for geographical data analyses. By the way, you will also be able to install the latest version of QGIS with it.

In the terminal:

sudo add-apt-repository 'deb http://ppa.launchpad.net/ubuntugis/ubuntugis-unstable/ubuntu bionic main '
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 314DF160
sudo apt update
sudo apt install libgdal-dev libproj-dev libgeos-dev libudunits2-dev libv8-dev libcairo2-dev libnetcdf-dev

If apt-key does not work, you can use this one:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 089EBE08314DF160

Then you can install your R packages using Ubuntu if you want them to always be up-to-date and correctly built for your distribution.
EDIT (2019-03-22): It seems that r-cran-sf and r-cran-rosm can not be installed from mrutter PPA if you use the UbuntuGIS PPA. You will need to install {sf} directly in R using install.packages("sf").

In the terminal (my personal selection):

sudo apt install r-cran-gstat r-cran-maps r-cran-mapdata r-cran-ncdf4 r-cran-sf r-cran-sp r-cran-raster r-cran-geor r-cran-ggmap r-cran-leaflet r-cran-rosm

Update R packages

You’re done ! You can now run R and do not forget to re-install all other R packages to be built with this new version of R. This requires checkBuilt = TRUE to force re-install packages even if you have the latest version available.

In R console:

update.packages(ask = FALSE, checkBuilt = TRUE)

If you need help for your R server installation and deployment of packages and shiny application, or if you want to learn how to build maps with R you can contact the ThinkR team.


Comments


Also read