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 \

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 \

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:


# Download and install R
curl -O${R_VERSION}.tar.gz
tar -xf R-${R_VERSION}.tar.gz

# Set compiler flags
R_BATCHSAVE="--no-save --no-restore"
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 \


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.

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 emulate the behavior in Windows, which is to install packages in a folder that also depends on <patch>.

For specificity, consider R version 3.5.1. To change the default user library, we must change the environment variable R_LIBS_USER in /opt/R/3.5.1/lib/R/etc/Renviron:


This is documented in the section R Installation and Administration of the official R manual.


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/ by including the line

options(repos = "")

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.