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

[This article was first published on Rtask, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

You can read the original post in its original format on Rtask website by ThinkR here: 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 !

New Ubuntu version and new R version

ubuntu-with-rIf 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

ubuntu-logoAs 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:




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

ubuntu-with-rTo 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 :
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 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 but also the Ubuntu binaries provided by Michael Rutter on his PPA that looks like deb 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 “” (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-get purge r-base* r-recommended r-cran-*
sudo apt autoremove
sudo apt update

Note that to use the * for partial matching, you need apt-get, not apt.

R should be totally removed after that.

Install new version of R (4.0)

Set new Ubuntu repositories for R 4.0

ubuntu-with-rDetailed installation instructions are on Thanks to Dirk Eddelbuettel and Michael Rutter, installing R on Ubuntu is a child’s play !
We will use the new mirror, which will be automatically redirected to a nearby CRAN mirror.

In the terminal:

sudo add-apt-repository 'deb focal-cran40/'

You may need the keys:

sudo apt-key adv --keyserver --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
sudo apt update

In case of problem to get the key, you can try this alternate method:

gpg --keyserver hkp:// --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

and then feed it to apt-key with

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

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

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

Install packages for spatial data analyses in Ubuntu

ubuntu-carto-rInstallation 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:

sudo apt install libgdal-dev libproj-dev libgeos-dev libudunits2-dev libnode-dev libcairo2-dev libnetcdf-dev

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.

To add the UbuntuGIS PPA,
In the terminal:

sudo add-apt-repository 'deb focal main '
sudo apt-key adv --keyserver --recv-keys 314DF160
sudo apt update
sudo apt upgrade

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

sudo apt-key adv --keyserver hkp:// --recv 089EBE08314DF160

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

In the terminal:

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

Update R packages

r-logoYou’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
destfile = "test-spatial.R")
# Install missing dependencies 
# This may take some time. Packages will be built from source if not installed with Ubuntu binaries.
deps <- attachment::att_from_rscript("test-spatial.R")
# Run the script
# Show interactive outputs

Isn’t it a nice map ?maps of Brittany (France) made with R and sf package

If you need help for your R server installation, Docker, CI and deployment of packages and shiny application or courses in advanced R and spatial data manipulations, you can contact the ThinkR team.

This post is better presented on its original ThinkR website here: Installation of R 4.0 on Ubuntu 20.04 LTS and tips for spatial packages

To leave a comment for the author, please follow the link and comment on their blog: Rtask. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)