Skip to main content

R on Ubuntu-based Linux

This guide applies to Ubuntu and to distributions based on it, such as Linux Mint. CRAN provides R for Ubuntu 22.04, 24.04 and 26.04, but only the latest LTS release is fully supported. Run the commands below in a terminal.

Do not install R from the default Ubuntu repositories, because they often contain an older version. Some R packages may then not install. The CRAN repository provides the current R version and keeps it updated through your regular system updates.

Install R

Find the codename of your Ubuntu release. On Linux Mint and other derivatives, this is the codename of the underlying Ubuntu release:

source /etc/os-release
echo "$UBUNTU_CODENAME"

This should print a name such as noble. If it prints nothing, your system may not be based on Ubuntu, and these steps may not apply. See the list of Ubuntu releases for more on codenames.

Next, in the same terminal, add the signing key and the CRAN repository. The cran40 in the repository name is a fixed label, and you still get the current R version:

wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
echo "deb https://cloud.r-project.org/bin/linux/ubuntu ${UBUNTU_CODENAME}-cran40/" | sudo tee /etc/apt/sources.list.d/cran-r.list

Then install R together with the tools needed to compile R packages:

sudo apt update
sudo apt install r-base r-base-dev

Check your installation. The version should match the latest R release:

R --version

If you followed an older version of this guide, also remove any old ...-cran35/ line from /etc/apt/sources.list.

See the CRAN instructions for Ubuntu for more details.

Install system libraries

Many R packages rely on system libraries. For example, sf and terra need GDAL, GEOS and PROJ. Install the libraries needed for this course with:

sudo apt install \
  libcurl4-openssl-dev libssl-dev libxml2-dev libfontconfig1-dev \
  libcairo2-dev libharfbuzz-dev libfribidi-dev libfreetype6-dev \
  libpng-dev libtiff5-dev libjpeg-dev cmake cargo \
  libgdal-dev libgeos-dev libproj-dev libudunits2-dev libsqlite3-dev \
  libuv1-dev

If a package fails to install later, read the end of the error message. It often names the missing library.

Install RStudio

Download the .deb file for Ubuntu from the RStudio download page. This package is for 64-bit Intel and AMD (x86_64) systems. Install it from the folder that contains the file:

sudo apt install ./rstudio-*.deb

Speed up package installation (optional)

On Linux, install.packages() compiles most packages from source by default, which can take a long time. The Posit Package Manager provides ready-made Linux packages. To use it, add the following line to the file ~/.Rprofile, using your own Ubuntu codename instead of noble:

options(repos = c(CRAN = "https://packagemanager.posit.co/cran/__linux__/noble/latest"))

Restart R for the change to take effect. If R still compiles a package, for example when you run it in a terminal instead of RStudio, the installation should still work, but it will take longer.

You still need the system libraries from the section above.

Update R

CRAN updates arrive with your regular system updates:

sudo apt update
sudo apt upgrade

R keeps a separate package library for each minor version, for example 4.5 and 4.6. After such an upgrade, your installed packages are no longer available, so reinstall the ones you need with install.packages().

Solve package version conflicts

If a package requires a newer version of R than you have, the best fix is usually to update R. First, check your version in R:

R.version.string

If you cannot update R, install an older version of the package instead. The Depends field on a package’s CRAN page shows the R version it requires, and the CRAN archive lists its older releases. The remotes package can install a specific release:

install.packages("remotes")
remotes::install_version("ggplot2", version = "3.5.0")