Installation of R 4.2 on Ubuntu 22.04.1 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.2 on Ubuntu 22.04.1 LTS and tips for spatial packages

It is again this time in your Ubuntu user’s life where you plan to upgrade your server installation from Ubuntu 20.04 to Ubuntu 22.04 LTS. Indeed, the first point release 22.04.1 is now there, and you’d like somebody else to try it for you before loosing all your work. Let me do it for you ! We will also use R 4.2 by default. Also, like every two years in this blog post, I’ll give the tips for geographical packages installation on Ubuntu. Just follow the guide…

Last time you read this kind of tutorial on our blog was two years ago to migrate to R 4.0 and Ubuntu 20.04 LTS. Note that I am writing this guide to be able to upgrade my own in-prod laptop. This has to work properly !
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. Note that we can help through this process of R-server installations, Docker or CI set-up.

This year, I will first realize the upgrade from Ubuntu Focal to Ubuntu Jammy inside a Docker container, so that I can write my blog post along with it. I’ll upgrade my own laptop just after that.

Table of Content

Too Long; Didn’t Read

You only want all the command lines in one place because you already followed my previous ubuntu with R migration blog posts in the last four years and you trust me blindly ? Okay !

=> Click here to see the full list of bash commands <=
# Install sudo if needed
apt-get install sudo  
# Upgrade your Ubuntu LTS
sudo su
apt-get -y autoremove
do-release-upgrade -d
# Clean the extra apt sources
# _Remove PPA like:
# - `deb https://mirror.ibcp.fr/pub/CRAN/ focal/`
# - `deb http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu focal main`
sudo vi /etc/apt/sources.list
# _Or maybe the specific list
ls /etc/apt/sources.list.d
sudo rm -i /etc/apt/sources.list.d/name.of.file.for.mirror.list
# Clean previous R packages installation
sudo apt-get purge r-base* r-recommended r-cran-*
sudo apt autoremove
# Install R and all required dependencies
sudo apt install -y --no-install-recommends software-properties-common dirmngr
# _Add the keys
sudo apt install -y wget
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
# _Add the R 4.0 repo from CRAN -- adjust 'focal' to 'groovy' or 'bionic' as needed
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
# Install R
# You'll be asked to choose your geographical area
sudo apt install -y r-base r-base-core r-recommended r-base-dev
# Install PPA with pre-compiled packages
sudo add-apt-repository ppa:c2d4u.team/c2d4u4.0+
# My own selection among 5000...
sudo apt install -y 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
# System deps for cartography
sudo apt install -y libgdal-dev libproj-dev libgeos-dev libudunits2-dev libv8-dev libnode-dev libcairo2-dev libnetcdf-dev
sudo apt install -y libglu1-mesa-dev freeglut3-dev mesa-common-dev
# Extra packages for image manipulation
sudo apt install -y libmagick++-dev libjq-dev libv8-dev libprotobuf-dev protobuf-compiler libsodium-dev imagemagick libgit2-dev

After all these bash commands, open R and upgrade all packages

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

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 22.04 Jammy Jellyfish for server or desktop is available for download. If you are still using Ubuntu 20.04 you may be proposed this upgrade now.

Upgrade to Ubuntu 22.04 Jammy Jellyfish

ubuntu-logoAs always, be sure to have some backups of your files before starting migration !

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

# install sudo if needed
# apt-get install sudo  
sudo apt upgrade
sudo apt dist-upgrade

You can also make a little clean-up.

sudo apt autoremove

then let’s upgrade to Ubuntu 22.04 !

sudo do-release-upgrade

This should work if you allowed only LTS versions to be installed, and after the first point release 22.04.1, expected on August 4th, 2022.
If the upgrade is not already available, and you do not want to wait, you’ll need to upgrade through the development releases with:

do-release-upgrade -d

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 22.04.1 release, early August 2022.

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 4.0, you will have to delete everything about the previous R version.
With the release upgrade, chances are Ubuntu already de-activated the repositories. Although, recent version try to re-activate them after the upgrade.
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/ focal/ and comment it with a #. Similarly, if you followed our post “migrate to Ubuntu 20.04 and R 4.0”, you probably added the main mirror of CRAN deb https://cloud.r-project.org/bin/linux/ubuntu but also maybe the Ubuntu binaries provided by Michael Rutter on the “c2d4u” Team PPA that looks like deb http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu focal 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.

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

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

Set new Ubuntu repositories for R 4.2

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 mirror https://cloud.r-project.org, which will automatically redirect to the closest CRAN mirror.

In the terminal:

sudo apt install -y --no-install-recommends software-properties-common dirmngr
# Add the keys
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
# add the R 4.0 repo from CRAN -- adjust 'focal' to 'groovy' or 'bionic' as needed
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"

Install R 4.2

Very few deb packages are necessary:

In the terminal:

sudo apt install -y 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 these 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.
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 (starting with “r-cran-”). Note that it is the same as for R4.0.

In the terminal:

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

Here is my own selection of packages you may not want to build yourself from source and let Michael do it for you.

 

sudo apt install -y 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}, {terra} … may require some external Ubuntu packages for softwares like PROJ, GDAL or GEOS.

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

sudo apt install -y libgdal-dev libproj-dev libgeos-dev libudunits2-dev libv8-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 -y libglu1-mesa-dev freeglut3-dev mesa-common-dev

If you want some extra system dependencies for image manipulation, you can install the following:

sudo apt install -y libmagick++-dev libjq-dev libv8-dev libprotobuf-dev protobuf-compiler libsodium-dev imagemagick libgit2-dev

At ThinkR, we use pre-built Docker containers to give R training on our BakaCode platform, so that attendees do not struggle with installations and directly start their course by writing their command lines in R.
Hence, we do have a configuration file with all Ubuntu system dependencies we need for our tutorials that you can find our on GitHub account there: https://github.com/ThinkR-open/installR/blob/master/bk-config.sh

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)

Set up a Docker container with the new Ubuntu version

Because I only have one computer for production, I must admit I first tested all the steps in a Docker container. Then I upgraded my own laptop.
I trust Ubuntu developers enough to know that the upgrade from Focal to Jammy will be ok. I now need to see if I can use R and spatial packages properly on Jammy.

I thus start with a raw image with Ubuntu Jammy on it, and execute code inside the container using VSCode.

In a terminal

docker pull ubuntu:jammy
docker run --rm -it  ubuntu:jammy

Then, I tested all the command lines of this blog post.

Command lines in VSCode showing test of the R spatial installation on Ubuntu

 

Note that you can also start from the Rocker images, like “rocker/rstudio” image from DockerHub, which contain a RStudio where you can test your configuration.
However, the current version is still focal and the devel version of rocker images use Ubuntu latest, which is not Jammy anymore, hence, I cannot use rocker to test R on Jammy myself, but you can test the installation steps for the future version of Ubuntu.

docker run -d -p 8787:8787 -e ROOT=TRUE -e DISABLE_AUTH=true rocker/rstudio:devel

Here, I used the following options:

  • -e ROOT=TRUE, so that I can be root in the Terminal and test some installation steps before creating my own derivated Dockerfile
  • -e DISABLE_AUTH=true, so that I do not have to give a password to connect to the RStudio server session and test the R session
  • -p 8787:8787 to be able to open the RStudio Server on port 8787 on my own laptop

Your turn!

If you need help for your R server installation 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.2 on Ubuntu 22.04.1 LTS and tips for spatial packages

To leave a comment for the author, please follow the link and comment on their blog: Rtask.

R-bloggers.com 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)