Install R from Source

How to install R from source on a Linux server.
Important

We recommend installing R from precompiled binaries unless you need to customize how R is configured. For example, installing R at a different location than /opt/R.

These instructions install a minimal version of R without many of the additional features enabled in the precompiled binaries. Such as, automatic configuration of a faster BLAS library.

To install R with the same features as the precompiled binaries but change where R is installed, build R from source. Use the rstudio/r-builds repository on GitHub.

Install required dependencies

  • First follow the steps to enable the required and optional repositories, as listed from the Install R page.
  • Next, install the build dependencies for R:
sudo dnf builddep R
sudo sed -i.bak "/^#.*deb-src.*universe$/s/^# //g" /etc/apt/sources.list
sudo apt update
sudo apt build-dep r-base
sudo zypper install \
    gcc \
    gcc-c++ \
    gcc-fortran \
    glibc-locale \
    java-11-openjdk-devel \
    libcurl-devel \
    make \
    pcre-devel \
    pcre2-devel \
    readline-devel \
    xorg-x11-devel \
    xz-devel 

Specify R version

Consult with your R user group to determine which version/versions of R they would like installed. Once defined, set the environment variable, shown below, to the first R version they request.

For multiple versions of R, follow the remaining steps and repeat them for each R version.

export R_VERSION=4.4.1

Versions of R that are available include: 3.6.x, 4.0.x, 4.1.x, 4.2.x, 4.3.x, 4.4.x

If you need to use an earlier version of R, then you will need to modify the export command shown above:

Terminal
export R_VERSION=3.X.X

Please see the CRAN index of R versions for all available versions of R.

Download and extract R

Download and extract the version of R that you want to install:

curl -O https://cran.rstudio.com/src/base/R-4/R-${R_VERSION}.tar.gz
tar -xzvf R-${R_VERSION}.tar.gz
cd R-${R_VERSION}
curl -O https://cran.rstudio.com/src/base/R-3/R-${R_VERSION}.tar.gz
tar -xzvf R-${R_VERSION}.tar.gz
cd R-${R_VERSION}

Build and install R

Build and install R by running the following commands:

./configure \
  --prefix=/opt/R/${R_VERSION} \
  --enable-R-shlib \
  --enable-memory-profiling

make
sudo make install
./configure \
  --prefix=/opt/R/${R_VERSION} \
  --enable-R-shlib \
  --enable-memory-profiling \
  --with-blas \
  --with-lapack

make
sudo make install
./configure \
  --prefix=/opt/R/${R_VERSION} \
  --enable-R-shlib \
  --enable-memory-profiling

make
sudo make install

Configuration options

Option Description
--prefix Specifies the directory where R is installed when executing make install. Change this to install R at a different location than /opt/R/${R_VERSION}.
--enable-R-shlib Required to use R with RStudio.
--enable-memory-profiling Enables support for Rprofmem() and tracemem(), used to measure memory use in R code.
--with-blas, --with-lapack Configures R to link against external BLAS and LAPACK libraries on the system. Recommended only on Ubuntu/Debian, where the alternatives system can switch the BLAS library at runtime. If unspecified, R uses an internal BLAS library that you can switch at runtime. See Configure R to use a different BLAS library for more details.

For a full list of configuration options, refer to the Configuration options section of the R administration manual.

Verify R installation

Test that R was successfully installed by running:

/opt/R/${R_VERSION}/bin/R --version

(Optional) Configure R to use a different BLAS library

We recommend configuring R to use a different BLAS library, such as OpenBLAS, to speed up linear algebra operations. By default, R is configured to use the reference BLAS library, but you can switch this after installing R.

  • If required on Ubuntu/Debian, switch the BLAS library using the alternatives system.

    For example, to switch to OpenBLAS on Ubuntu 22, install the OpenBLAS package and run the update-alternatives command to change the default BLAS library:

    sudo apt install libopenblas-dev
    sudo update-alternatives --config libblas.so.3-$(arch)-linux-gnu
  • If required on RHEL/CentOS and SUSE Linux, switch the BLAS library by replacing R’s internal shared BLAS library, located at R_HOME/lib/libRblas.so, with a symbolic link to a different library.

    For example, to switch to OpenBLAS on RHEL 9, install OpenBLAS, create a backup of R_HOME/lib/libRblas.so, and create a symlink to OpenBLAS at R_HOME/lib/libRblas.so:

    sudo dnf install openblas-devel
    sudo mv $(R RHOME)/lib/libRblas.so $(R RHOME)/lib/libRblas.so.keep
    sudo ln -s /usr/lib64/libopenblasp.so $(R RHOME)/lib/libRblas.so

For more information on configuring the BLAS library in R, refer to the BLAS section of the R administration manual.

(Optional) Install multiple versions of R

To install multiple versions of R on the same server, repeat these steps to specify, download, and install a different version of R alongside existing versions.

Back to top