Robert's Data Science Blog

Multiple R Installations on Linux

Sometimes it can be convenient to have multiple versions of R installed on the same computer. On Windows this is pretty straightforward, but on Linux it requires a bit more. Parts of this post is based on a similar post from RStudio.

Building from source

On Linux I typically build R from source. The steps I show here are largely based on my Docker images with R.

R has both installation dependencies and runtime dependencies. They can be installed collectively with the command sudo apt-get build-dep r-base.

Personally I install them individually as I would in a Docker image, with the notable exception of LaTeX: I use LaTeX for more than R and I prefer to install TeX Live from its website instead of using the outdated version Ubuntu offers.

At the time of writing I run the following commands on Ubuntu 18.04 (through a shell script) for the installation dependencies:

apt-get install -y --no-install-recommends \
                ca-certificates \
                file \
                fonts-texgyre \
                gsfonts \
                libblas3 \
                libbz2-1.0 \
                libcairo2 \
                libcurl4 \
                libcurl4-openssl-dev \
                libglib2.0-0 \
                libgomp1 \
                libicu60 \
                libjpeg8 \
                liblzma5 \
                libopenblas-dev \
                libpangocairo-1.0-0 \
                libpcre3 \
                libpng16-16 \
                libreadline7 \
                libtiff5 \
                locales \
                make \
                ucf \
                unzip \
                zip \
                zlib1g

The following command install the runtime dependencies:

apt-get install -y --no-install-recommends \
                bash-completion \
                bison \
                curl \
                default-jdk \
                debhelper \
                g++ \
                gcc \
                gfortran \
                groff-base \
                libblas-dev \
                libbz2-dev \
                libcairo2-dev \
                libpango1.0-dev \
                libjpeg-dev \
                libicu-dev \
                liblapack-dev \
                libncurses5-dev \
                libpcre3-dev \
                libpng-dev \
                libreadline-dev \
                libtiff5-dev \
                liblzma-dev \
                libx11-dev \
                libxt-dev \
                mpack \
                perl \
                tcl8.6-dev \
                tk8.6-dev \
                x11proto-core-dev \
                xauth \
                xfonts-base \
                xvfb \
                zlib1g-dev

The compilers gcc, g++ and gfortran are not runtime dependencies, but they are needed to install most modern packages.

To install I put the following in a shell script:

R_VERSION=3.5.0

# Download and install R
curl -O https://cran.r-project.org/src/base/R-3/R-${R_VERSION}.tar.gz
tar -xf R-${R_VERSION}.tar.gz
cd R-${R_VERSION}

# Set compiler flags
R_PAPERSIZE=letter
R_BATCHSAVE="--no-save --no-restore"
R_BROWSER=xdg-open
PAGER=/usr/bin/pager
PERL=/usr/bin/perl
R_UNZIPCMD=/usr/bin/unzip
R_ZIPCMD=/usr/bin/zip
R_PRINTCMD=/usr/bin/lpr
LIBnn=lib
AWK=/usr/bin/awk
CFLAGS="-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g"
CXXFLAGS="-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g"

./configure --enable-R-shlib \
			--enable-memory-profiling \
			--with-readline \
			--with-blas \
			--with-tcltk \
			--disable-nls \
			--without-recommended-packages \
			--prefix=/opt/R/$R_VERSION

make

These commands configure the installation and compile the source code. Finally run sudo make install to install R.

Note the last option --prefix for configure – this is how to specify where to install this version of R. I choose /opt/R/<version>.

The --enable-R-shlib option is necessary to make the underlying R shared library available to RStudio.

To make the command R point to the installed R I make a symbolic link to a folder in my path:

sudo ln -sfn /opt/R/3.5.0/bin/R /usr/local/bin

Post installation

I want to tweak my installation in two ways compared to the default settings in Linux.

User package library

On Linux, the default user library with packages for R version <major>.<minor>.<patch> is ~/R/x86_64-pc-linux-gnu-library/<major>.<minor>. This means that packages for e.g. R version 3.5.0 and 3.5.1 will be installed in the same folder.

But if a package is installed in e.g. R 3.5.1 and loaded in R 3.5.0 we will get the annoying message

package <name> was built under R 3.5.1

To avoid this I install packages in a folder that also depends on <patch>.

To change the default user library, we must change the environment variable R_LIBS_USER as documents in the section R Installation and Administration of the official R manual

This can be done in a version and platform agnostic way using “specifiers” – these are explained in the help page of R_LIBS_USER. My ~/.Renviron includes this line

R_LIBS_USER=~/R/%p-library/%V

The %p and %V are specifiers that expand to the platform (x86_64-pc-linux-gnu) and version with patch.

CRAN

For the non-bleading edge versions I want to use MRAN’s fixed repositories to ensure that the packages work as intended.

The default package repositoriy can be changed in the systemwide Rprofile in /opt/R/3.5.1/lib/R/etc/Rprofile.site by including the line

options(repos = "https://mran.microsoft.com/snapshot/2018-12-19")

This date is the last day where 3.5.1 was the newest version of R, that is, the day before 3.5.2 was released.

In R we can retrieve the current value of the repos variable with the command getOption("repos").

Note that this system setting can be overruled by user settings (an .Rprofile in the users homedir) or a project setting.

Problems

I sometimes use Julia to interact with R through the RCall package. This highlighted a few problems.

First, the operating system must be informed of the non-standard location of R. Either by setting the environment variable R_HOME in the shell (availabe by running R RHOME in the shell or R.home() in R) or by updating the PATH with the location of the R executable.

Secondly, when loading the RCall package in Julia, I would see errors like

┌ Warning: RCall.jl: Error: package or namespace load failed for ‘stats’ in dyn.load(file, DLLpath = DLLpath, …):
│ unable to load shared object ‘/opt/R/3.5.1/lib/R/library/stats/libs/stats.so’:
│ libRlapack.so: cannot open shared object file: No such file or directory
│ During startup - Warning message:
│ package ‘stats’ in options(“defaultPackages”) was not found
└ @ RCall ~/.julia/packages/RCall/29zDq/src/io.jl:113

It turned out that the default Ubuntu r-base was also installed on my computer (this can be checked with the command apt list --installed) and the shared library used by RCall was pointing to the wrong location.

More specifically, RCall use the shared object libR.so (which can be found using the command ldconfig -p | egrep libR.so) that did not use the libRlapack.so that was installed along with my custom R.

What turned out to work for me was to update the environment variable LD_LIBRARY_PATH to include the path of libRlapack.so. This is done by adding the following line to a .zshrc/.bashrc:

export LD_LIBRARY_PATH="/opt/R/3.5.1/lib/R/lib:$LD_LIBRARY_PATH"