[tutorial] Install a package in R

Have you ever wonder how to install a R package in your account on the cluster?

It is quite easy, from R itself.

First you need to indicate to R where to store the R packages you’ll install. To do so, create a file named .Renviron in your home directory with the following content:


Create the directory to store the packages:

mkdir ~/scratch/Rpackages

Then you can as well create a file .Rprofile with the following content to specify from where you want to download the packages:

r = getOption("repos") # hard code the Switzerland repo for CRAN
r["CRAN"] = "https://stat.ethz.ch/CRAN/"
options(repos = r)

Once done, you can load and launch R:

[sagon@login2.baobab ~]$ ml GCC/11.2.0  OpenMPI/4.1.1 R/4.1.2
[sagon@login2.baobab ~]$ R

R version 4.1.2 (2021-11-01) -- "Bird Hippie"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.


Then you can install the package you want. For example locuscomparer, this is a package a user asked us to install. According to the documentation, this is how to install the package:

> devtools::install_github("boxiangliu/locuscomparer")
Downloading GitHub repo boxiangliu/locuscomparer@HEAD
Installing 1 packages: RMySQL
Installing package into ‘/home/testsagon1/Rpackages’
(as ‘lib’ is unspecified)
trying URL 'https://stat.ethz.ch/CRAN/src/contrib/RMySQL_0.10.23.tar.gz'
Content type 'application/x-gzip' length 53328 bytes (52 KB)
downloaded 52 KB

* installing *source* package ‘RMySQL’ ...
** package ‘RMySQL’ successfully unpacked and MD5 sums checked
** using staged installation
Using PKG_LIBS=-lmysqlclient
-----------------------------[ ANTICONF ]-----------------------------
Configure could not find suitable mysql/mariadb client library. Try installing:
 * deb: libmariadbclient-dev | libmariadb-client-lgpl-dev (Debian, Ubuntu)
 * rpm: mariadb-connector-c-devel | mariadb-devel | mysql-devel (Fedora, CentOS, RHEL)
 * csw: mysql56_dev (Solaris)
 * brew: mariadb-connector-c (OSX)
If you already have a mysql client library installed, verify that either
mariadb_config or mysql_config is on your PATH. If these are unavailable
you can also set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
--------------------------[ ERROR MESSAGE ]----------------------------
<stdin>:1:10: fatal error: mysql.h: No such file or directory
compilation terminated.

As you can see, this package needs a dependency: RMySQL and this package needs a dependency: MariaDB or MySQL. Close R and load MariaDB:

[sagon@login2.baobab ~]$ ml MariaDB/10.6.4

You can relaunch the package installation and this time it works!

There is no need to re install the packages on every run. It may happens to you have unusable packages in your Rpackages repo, for example because you used more than one R version. In this case, you may just erase the content of this directory and re install.