Upgrading R on Ubuntu: A Step-by-Step Guide to the Latest Stable Release

Upgrading R on Ubuntu: A Step-by-Step Guide

Introduction

As a user of the popular programming language and environment R, it is likely that you have encountered the need to upgrade your current version to the latest stable release. Fortunately, upgrading R on Ubuntu is a relatively straightforward process that can be completed using standard Linux tools and commands. In this article, we will walk through the steps involved in upgrading R on Ubuntu.

Checking Compatibility

Before beginning the upgrade process, it’s essential to check if the new version of R is compatible with your system configuration. The compatibility of R depends on various factors, including the operating system, architecture (32-bit or 64-bit), and installed packages.

Updating Sources List

To upgrade R, you need to update your sources list to point to the latest repository for R. This involves modifying the /etc/apt/sources.list file to include the necessary lines.

Modifying the Sources List

  1. Open the sources.list file using a text editor:

sudo nano /etc/apt/sources.list

2. Add a line with the source from where the packages will be retrieved, for example:

deb https://cloud.r-project.org/bin/linux/ubuntu/ bionic/


   Replace `<https://cloud.r-project.org>` with your preferred mirror and `<bionic/>` with your current Ubuntu version (e.g., `trusty/`, `xenial/`, etc.).

3. Save the changes and close the file.

#### Fetching Secure APT Key

4. Fetch the secure APT key:

   ```markdown
gpg --keyserver keyserver.ubuntu.com --recv-key E298A3A825C0D65DFD57CBB651716619E084DAB9

Alternatively, you can use the --hkp option to fetch the key from a secure HTTP connection:

gpg --hkp://keyserver keyserver.ubuntu.com:80 --recv-key E298A3A825C0D65DFD57CBB651716619E084DAB9
  1. Add the APT key to your system’s keyring using the following command:

gpg -a –export E084DAB9 | sudo apt-key add -


### Upgrading R

6. Update your sources and upgrade your installation:

sudo apt-get update && sudo apt-get upgrade

7. Install the new version of R using the following command:
   ```markdown
sudo apt-get install r-base-dev

Note that installing r-base-dev only includes the development environment for R, while r-base will include both the base package and all dependencies.

Recovering Old Packages

After upgrading to a newer version of R, you may need to recover old packages. This involves copying packages from the old library directory (R-oldversion/library) to the new library directory (R-newversion/library).

Here’s an example command to copy all packages from R-oldversion/library to R-newversion/library, excluding those already present in the new version:

rsync -av --exclude='^/usr/lib/r-base/$' R-oldversion/library/* R-newversion/library/

Then, run the following R command to update the package list and install any missing dependencies:

update.packages(checkBuilt=TRUE, ask=FALSE)
  1. Optionally, you can use a script (see below) to automate the recovery of old packages.

Automating Package Recovery with a Script

Here’s an example Bash script that recovers old packages from the previous R version:

#!/bin/bash

# Set variables for old and new library directories
OLD_LIBRARY_DIR="R-oldversion/library"
NEW_LIBRARY_DIR="R-newversion/library"

# Copy all packages from old to new library directory (excluding existing ones)
rsync -av --exclude='^/usr/lib/r-base/$' $OLD_LIBRARY_DIR/* $NEW_LIBRARY_DIR/

# Update package list and install missing dependencies
update.packages checkBuilt=TRUE ask=FALSE

To use this script, save it as a file with a .sh extension (e.g., recover_packages.sh), make the file executable with the following command:

chmod +x recover_packages.sh

Then, execute the script using:

./recover_packages.sh

Note that you should replace R-oldversion/library/ and R-newversion/library/ with your actual library directories.

Conclusion

Upgrading R on Ubuntu is a relatively straightforward process. By following these steps and understanding how to modify the sources list, fetch APT keys, and recover old packages, you can ensure a smooth transition to the latest stable version of R.

As R continues to evolve, it’s essential to stay up-to-date with the latest developments and updates. This guide provides a solid foundation for upgrading R on Ubuntu but may not cover all scenarios or edge cases. For more advanced users or those requiring additional features, consult the official R documentation and community forums for further assistance.


Last modified on 2025-04-09