Site background texture

Installing R and the Tidyverse on Ubuntu 20.04

20 June 2022

Installing R and the Tidyverse on Ubuntu 20.04

Installing R should be a relatively quick and painless process with Ubuntu. While the standard repositories often offer older versions of R, we can simply add CRAN to the repository list and be comfortable of our ability to install moving forward.

We can add any new repositories required using sudo (administrator) access and the code block:

1sudo apt install software-properties-common ca-certificates apt-transport-https gnupg dirmngr

Further, we add CRAN to our list of repositories that Ubuntu will read:

1sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
2sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'

Then finally, we can just install r-base using the standard method:

1sudo apt install r-base

Easy!

If you want to check which R version you’re running, simply write in the command line:

1R --version

Now, with that done, most R users will want to install packages to go along with their base R installation. I’ve found that this is a particularly annoying item: various Linux-related issues spring up that I’ve had to pick apart through the years (and I am far from a Linux expert). Here are some quick troubleshooting items:

  • There are installation dependencies on some fairly major packages. Both tidyverse, httr and odbc require installations at the command line before they can be installed in R. These should help install the required elements:
1sudo apt install libssl-dev libcurl4-openssl-dev unixodbc-dev libxml2-dev libmariadb-dev libfontconfig1-dev libharfbuzz-dev libfribidi-dev libfreetype6-dev libpng-dev libtiff5-dev libjpeg-dev
  • When trying to install xml2 (a dependency of tidyverse), I’ve had repeated issues with it being unable to move the installation to its relevant directory. To resolve, first I open R as sudo and then install xml2 with some additional arguments:
1sudo R -e 'install.packages("xml2", dependencies = T, INSTALL_opts = c("--no-lock"))'

Finally, after all this, within R, the tidyverse should install properly:

1sudo R -e 'install.packages("tidyverse")'

I’ll keep adding as I find more irritations, but this should hopefully move you forward.

Happy counting!