Installation of R 4.0 on Ubuntu 20.04 LTS and tips for spatial packages

You plan to upgrade your server installation from Ubuntu 18.04 to Ubuntu 20.04 LTS? It is also the best time to migrate to R 4.0 ! 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…

Last time you read this kind of tutorial on our blog was to migrate to Ubuntu 18.04 and R 3.5. This blog post has had its success, so I will take the exact same structure for the present one. Note that I am writing this guide while upgrading my own in-prod laptop myself. This has to work properly !Note that following this procedure on my laptop, R 4.0 and spatial packages are fully fonctional !
Also, everything is proposed through command lines, which means that you can use this tutorial to upgrade a server with no graphical interface, to build your Docker containers or set up the continuous integration of your package builds.

A lot of changes happened in the last two years in the Ubuntu and the R communities. Migrating to R 4.0 will be a game changer ! Enjoy !

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. The first release of Ubuntu 20.04 Focal Fossa for server or desktop is available for download. If you are still using Ubuntu 18.04 you may be proposed this upgrade.

During the last few months, you also heard about the release of R 4.0 as announced by the R Core team. Would you like a taste?

Upgrade to Ubuntu 20.04

As always, be sure to have some backups of your files before starting migration !Note that upgrades from 18.04 LTS will not be enabled until a few days after the 20.04.1 release expected in late July 2020. This may be safer to wait for this version before upgrading a machine in production.
If you have problems with Rstudio Desktop on your HiDPI screens with Ubuntu 20.04, you can have a look at this issue.

You really want to jump? Then, let’s go ! Be sure the manager is installed :

sudo apt install update-manager-core

You will have to be sure your system is already up-to-date.

sudo apt upgrade
sudo apt dist-upgrade

You can also make a little clean-up.

sudo apt autoremove

then let’s upgrade to Ubuntu 20.04 Focal Fossa !

sudo do-release-upgrade

This should work if you allowed only LTS versions to be installed. In my own case, I recently upgraded to 19.10 for some tests and maybe I was not patient enough, but the new version was not proposed (on 2020-05-01). You can temporarily allow for “normal” release, which, here, will propose you the last version available 20.04. The one we want. Be careful with this change. If you are using you computer for work in production mode, set it back to “lts” as soon as you have the LTS installed. However, for server in production, I would recommend waiting for the 20.04.1 release, late July 2020.

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

Prompt=lts

to:

Prompt=normal

Then you can run sudo do-release-upgrade

Also, as I was not patient enough and because upgrades from 19.10 would not be enabled until a few days after 20.04’s release, running the command led me to “No new release found” (On 2020-05-01). At my own risk, I decided to force the release using sudo do-release-upgrade -d.

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

Delete previous version of R

Remove additional repositories

To be able to use R 4.0, 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/ bionic/ and comment it with a #. Similarly, if you followed our post “migrate to Ubuntu 18.04 and R 3.5”, you probably added the main mirror of CRAN deb https://cloud.r-project.org/bin/linux/ubuntu but also the Ubuntu binaries provided by Michael Rutter on his PPA that looks like deb http://ppa.launchpad.net/marutter/c2d4u3.5/ubuntu bionic main. Comment all these lines 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.

Install R 4.0

Very few deb packages are necessary.

In the terminal:

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

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 with packages for cartography or those with Java in them.
With R 4.0, packages require to be rebuilt differently than with previous versions. As with R3.5/3.6 series, Michael Rutter prepares for us ready to use binaries installation of your prefered R packages on a PPA with the “cran2deb4ubuntu Build Team”. More explanations on these binaries are available on his blog post: “R4.0 now available”. Hence, you’ll need to use his PPA before installing Ubuntu packages for R (those starting by“r-cran-”). Note that this PPA is different than the one of versions R3.5/3.6.

In the terminal:

sudo add-apt-repository ppa:c2d4u.team/c2d4u4.0+
sudo apt update

Here is my own selection of packages you may not want to build yourself from source and let Michael do it for you. As of today, not all packages are available (On 2020-05-01). There is still a lot of work to realise. Please give some time to Michael. You can check the list of packages available on launchpad.

Install packages for spatial data analyses in Ubuntu

Installation of R packages like {sf}, {sp}, {rgdal}, {rgeos}, {raster} … may require some external Ubuntu packages for softwares like PROJ, GDAL or GEOS. Previously, the spatial R packages required versions of these softwares more recent than the default Ubuntu ones, hence the use of UbuntuGIS PPA was required. Since Ubuntu 18.04, Ubuntu default versions of dependencies are sufficiently up-to-date for your R packages.

Install the following Ubuntu packages to allow for future spatial data analysis in R:

If you want to play with {rgl} and {rayshader}, you may also need the following:

sudo apt install libglu1-mesa-dev freeglut3-dev mesa-common-dev

It is recommended to install UbuntuGIS PPA to be able to work with R packages for geographical data analyses, in particular to test changes happening around PROJ (>= 6.0) and management of projections in {sf} and other spatial packages. However, it seems that versions of spatial packages released as binaries by the “cran2deb4ubuntu Build Team” are not built with “UbuntuGIS PPA”. Hence, if you use both these PPA, you will have to build R packages from sources using the traditional way in R, i.e. using install.packages().

As often in the open-source communities, the maintainers team of “UbuntuGIS PPA” is small. It may takes some time before packages are released for your distribution. You can check on the UbuntuGIS unstable page if your distribution is supported. Only Ubuntu LTS distribution are available. Also, today (2020-05-01), packages for Ubuntu 20.04 are not available.

Then, it is your choice to install your R packages using Ubuntu binaires if you want them to always be up-to-date and correctly built for your distribution or do it from sources in R using install.packages().

Update R packages

You’re done ! You can now run R and do not forget to re-install all other R packages so that there are built with this new version of R. Indeed, packages built for previous R versions will not be compatible with R4.0. Updating packages built from source requires update.packages() with checkBuilt = TRUE to re-install packages even if you have the latest versions available.

In R console:

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

Test R and spatial packages

On my personal StatnMap blog, I wrote a blog post as an Introduction to mapping with {sf} & Co.. There are enough examples to test the correct installation of R and your spatial packages. The code is available on github, so just run the following lines of code and that’s it:

# Download file with Spatial analyses
download.file("https://raw.githubusercontent.com/statnmap/blog_tips/master/2018-07-14-introduction-to-mapping-with-sf-and-co.R",
destfile = "test-spatial.R")
# Install missing dependencies
# This may take some time. Packages will be built from source if not installed with Ubuntu binaries.
install.packages("attachment")
deps <- attachment::att_from_rscript("test-spatial.R")
attachment::install_if_missing(deps)
# Run the script
source("test-spatial.R")
# Show interactive outputs
m
tmap_leaflet(tm)
mapView(departements_L93)