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

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…

New Ubuntu version and new R version

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

As 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

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 case of problems with “keyserver”, you can try the following command line:sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys E084DAB9

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-”).

Install packages for spatial data analyses in Ubuntu

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

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").

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.