Motivation

  • Be able to use R on ubuntu 24.04
  • Easy to have different versions of R with different packages
  • Faster installation of pacakges (the downloaded packages are already compiled) so it si supposed to be much less annoying waiting for the compilation as in desktop Rstudio instance

How to do it with a normal R installation

1. Download and install R from the repository

# update indices
sudo apt update -qq
# install two helper packages we need
sudo apt install --no-install-recommends software-properties-common dirmngr
# add the signing key (by Michael Rutter) for these repos
# To verify key, run gpg --show-keys /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc 
# Fingerprint: E298A3A825C0D65DFD57CBB651716619E084DAB9
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 repo from CRAN -- lsb_release adjusts to 'noble' or 'jammy' or ... as needed
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
# install R itself
sudo apt install --no-install-recommends r-base

NOTE: When trying to install tidyverse package run up into errors, chatgpt suggested installing following packages:

sudo apt update

sudo apt install \
  libcurl4-openssl-dev \
  libfontconfig1-dev \
  libfreetype6-dev \
  libxml2-dev \
  libharfbuzz-dev \
  libfribidi-dev \
  pkg-config \
  build-essential

and then some more

sudo apt update

sudo apt install \
  libcurl4-openssl-dev \
  libfontconfig1-dev \
  libfreetype6-dev \
  libpng-dev \
  libtiff5-dev \
  libjpeg-dev \
  libwebp-dev \
  libharfbuzz-dev \
  libfribidi-dev \
  libxml2-dev \
  pkg-config \
  build-essential

2. Install R studio

  1. download the deb from the posit website link
sudo apt install ./deb-name.deb

3. Run R script from the command line

Rscript name-of-script.R

After installing the tidyverse inside the Rstudio the scripts can be run from the command line.

How to do it with docker:

Make a folder for the R-projects in the /home directory

mkdir ~/R-projects
sudo chown -R $USER:$USER ~/R-projects

This makes a project for all R-projects which will be used to store data.
You must make the folder accessible to the user otherwise you are not able to access it.

Start the container

docker run -d --name rstudio44 \
  -p 8787:8787 \
  -e PASSWORD=vld \
  -e USERID=$(id -u) \
  -e GROUPID=$(id -g) \
  -v ~/R-projects:/home/rstudio/R-projects \
  -v ~/Rlibs/4.4:/usr/local/lib/R/site-library \
  rocker/rstudio:4.4.1

What is what:

  • -p 8787:8787 the 8787 port of container (second number) maps to localhost:8787 (first number) in case you have more different rstudios, keep changing the first 4 digits before colon
  • -v ~/R-projects:/home/rstudio/R-projects tells the docker instance that the project is located in ~/R-projects
  • -v ~/Rlibs/4.4:/usr/local/lib/R/site-library tells it the packages are stored outside of the container and does not get wiped out after it stops
  • rocker/rstudio:4.4.1 the version of r studio you want to run

Working with docker

Log in

Open your browser and write http://localhost:8787 this will open a widget to put your name “rstudio” and “password”

Stop it

in the terminal run

docker stop rstudio44

Start it

docker start rstudio44

Remove it

docker rm rstudio44

More versions of R?

Give different versions of R different names, ports and Rlibs folders, and use a different rstudio versions then make a shortcut