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

Tags : maps, server, ubuntu

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

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.


About the author

Sébastien Rochette

Sébastien Rochette

Modélisateur, formateur R, joueur de cartographies


13 responses to “Installation of R 3.5 on Ubuntu 18.04 LTS and tips for spatial packages”

  1. Hi — I have an issue with gdal-abi-2-2-3

    See below – any idea on how to fix this>

    [email protected]:~$ apt-cache policy libgdal20

    libgdal20:
    Installed: 2.3.2+dfsg-2~bionic0
    Candidate: 2.3.2+dfsg-2~bionic0
    Version table:
    *** 2.3.2+dfsg-2~bionic0 500
    500 http://ppa.launchpad.net/ubuntugis/ubuntugis-unstable/ubuntu bionic/main amd64 Packages
    100 /var/lib/dpkg/status
    2.2.3+dfsg-2 500
    500 http://archive.ubuntu.com/ubuntu bionic/universe amd64 Packages

    [email protected]:~$ sudo apt install r-cran-rgdal
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    Some packages could not be installed. This may mean that you have
    requested an impossible situation or if you are using the unstable
    distribution that some required packages have not yet been created
    or been moved out of Incoming.
    The following information may help to resolve the situation:
    The following packages have unmet dependencies:
    r-cran-rgdal : Depends: gdal-abi-2-2-3
    E: Unable to correct problems, you have held broken packages.

    [email protected]:~$ sudo apt install gdal-abi-2-2-3
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    Package gdal-abi-2-2-3 is a virtual package provided by:
    libgdal20 2.2.3+dfsg-2 [Not candidate version]
    E: Package 'gdal-abi-2-2-3' has no installation candidate

    [email protected]:~$ apt-cache policy libgdal20
    libgdal20:
    Installed: 2.2.3+dfsg-2
    Candidate: 2.3.2+dfsg-2~bionic0
    Version table:
    2.3.2+dfsg-2~bionic0 500
    500 http://ppa.launchpad.net/ubuntugis/ubuntugis-unstable/ubuntu bionic/main amd64 Packages
    *** 2.2.3+dfsg-2 500
    500 http://archive.ubuntu.com/ubuntu bionic/universe amd64 Packages
    100 /var/lib/dpkg/status

    [email protected]:~$ sudo apt install gdal-abi-2-2-3
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    Package gdal-abi-2-2-3 is a virtual package provided by:
    libgdal20 2.2.3+dfsg-2 [Not candidate version]
    E: Package 'gdal-abi-2-2-3' has no installation candidate

      • Hi,

        I have the same problem, with PPA of Michael Rutter


        [email protected]:~$ sudo apt install r-cran-sf
        Reading package lists... Done
        Building dependency tree
        Reading state information... Done
        Some packages could not be installed. This may mean that you have
        requested an impossible situation or if you are using the unstable
        distribution that some required packages have not yet been created
        or been moved out of Incoming.
        The following information may help to resolve the situation:
        The following packages have unmet dependencies:
        r-cran-sf : Depends: gdal-abi-2-2-3
        E: Unable to correct problems, you have held broken packages.
        [email protected]:~$ apt-cache policy libgdal20
        libgdal20:
        Installed: 2.3.2+dfsg-2~bionic0
        Candidate: 2.3.2+dfsg-2~bionic0
        Version table:
        *** 2.3.2+dfsg-2~bionic0 500
        500 http://ppa.launchpad.net/ubuntugis/ubuntugis-unstable/ubuntu bionic/main amd64 Packages
        100 /var/lib/dpkg/status
        2.2.3+dfsg-2 500
        500 http://it.archive.ubuntu.com/ubuntu bionic/universe amd64 Packages

        • gdal-abi-2-2-3I did exactly follow the instruction 🙂 –

          The main issue came from:

          The following packages have unmet dependencies:

          r-cran-rosm : Depends: r-cran-rgdal but it is not going to be installed

          r-cran-sf : Depends: gdal-abi-2-2-3

          wich seems linked to a way dependency is referenced through the gdal-abi-2-2-3 virtual package –

          I have recompiled rgdal directly from source – as well as sf and rosm…

          for rgeos – there was a bug –

          https://stackoverflow.com/questions/52922060/rgeos-package-installation-error-on-linux-r

          • On my ubuntu bionic installation, I currently do not use ubuntugis unstable PPA and I do not have this problem. It is possible that a recent update of this unstable PPA has broken some dependencies.

            I would recommend trying to remove all spatial ubuntu packages (gdal, geos, …), clean possible remaining configuration files.

            Then, try the installation without ubuntugis PPA.

            I do install all spatial R packages only using Michael PPA. I am not using “install.packages” on R for these.

  2. I use an ubuntu 16.04 server in which I have R 3.4.2 installed with around 150 packages. I donot use spatial packages. Everything was fine until today when repeated R crashes occurred esp with newly installed packages (e.g. shiny, shinyjs, shinyalerts, data.table, lubridate) that seem to be built for 3.5.

    My questions are:

    Since upgrading to R 3.5 seems hugely complicated on an ubuntu server as I see from may blogs and articles, shall I just revert the packages to their older versions?

    Or

    If upgrade is inevitable shall I use these instructions for 18.04 for ubuntu 16.04 and if yes what are the main differences with 16.04?

    Note: I have a very basic knowledge of ubuntu.

    Thanks a lot for your suggestions.

    • Hi Sanjay,

      I recommend upgrading R and its packages.

      I understand that you may want to prefer to keep with 16.04, but you should remind that 18.04 is also a Long Term Support version of Ubuntu.
      However, if you still want to use 16.04, you can follow the same instructions. You will have to change every call to bionic by trusty (Be carefull, there are everywhere in my code !)
      Start from “Delete previous version of R”.
      Everything should be good.

Leave a Reply

Your email address will not be published. Required fields are marked *


Also read