Skip to content

How to install the latest R version

Sciencecloud Ubuntu images do not include the latest version of R.

To install the latest version of R on Ubuntu, please follow the official instructions from CRAN. Note: you will need to prepend sudo to many of the recommended commands.

How to maintain your packages if updating the version of R on your machine

If you would like to maintain all of the packages from your previous installation (i.e., if you're updating your version of R to version 4), consider the following directions:

  • Before following the directions above to install/update to the latest R version, run the following two lines of code from within your R instance (i.e., run the command R from your Ubuntu command line to open your instance of R):
    • packagesToReInstall = installed.packages()
    • save(packagesToReInstall, file = "packagesToReInstall.RData")
  • These two lines of code will make a variable/object that contains the names of the installed packages on your system then will save the variable/object within an R data file to your home directory.
  • After you have updated your R instance using the directions above, run the following two lines of code from within your R instance (i.e., run the command R from your Ubuntu command line to open your newly updated instance of R):
    • load("packagesToReInstall.RData")
    • install.packages(rownames(packagesToReInstall), dep = T)
  • These two lines of code will load the saved R data file containing the variable/object that stores the list of previously installed packages from R as row names; these row names are then called as a list and inputted into the install.packages() command, which will install the packages (into a user library by default).
  • Note: if you would like to install these packages into a shared library location (for example, if you're using this instance of R with RStudio server), then consider adding the lib="/usr/lib/R/library" argument to the install.packages() command. In other words, the command would instead be:
    • install.packages(rownames(packagesToReInstall), dep = T, lib="/usr/lib/R/library")
    • Important note: given that "/usr/lib/R/library" is a protected directory that is used as the default location of the base R packages, you must run the R command as a superuser; i.e., run sudo R when opening your R instance to reinstall your packages instead of simply the R command as detailed above.

Last update: March 21, 2022