Skip to content

Install R from Source#

These instructions describe how to install R from source on a Linux server.

Note

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 are meant to 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.

If you want to install R with the same features as the precompiled binaries but change where R is installed, you can build R from source using the rstudio/r-builds repository on GitHub instead.

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 yum-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(s) of R they would like installed. Once defined, set the environment variable, shown below, to the first R version they request.

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

Terminal
$ export R_VERSION=4.1.3
Available versions of R

Versions of R that are available include:

4.2.2, 4.2.1, 4.2.0, 4.1.3, 4.1.2, 4.1.1, 4.1.0, 4.0.5, 4.0.4, 4.0.3, 4.0.2, 4.0.1, 4.0.0


3.6.3, 3.6.2, 3.6.1, 3.6.0, 3.5.3, 3.5.2, 3.5.1, 3.5.0, 3.4.4, 3.4.3, 3.4.2, 3.4.1, 3.4.0, 3.3.3, 3.3.2, 3.3.1, 3.3.0

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.r-project index for all available versions of R.

Download and extract R#

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

Terminal
$ 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}
Terminal
$ 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 may be used to switch the BLAS library at runtime. If unspecified, R uses an internal BLAS library that can be switched 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:

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

To ensure that R is available on the default system PATH variable, create symbolic links to the version of R that you installed:

Terminal
$ sudo ln -s /opt/R/${R_VERSION}/bin/R /usr/local/bin/R
$ sudo ln -s /opt/R/${R_VERSION}/bin/Rscript /usr/local/bin/Rscript

(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 this may be switched after installing R.

  • On Ubuntu/Debian, the BLAS library can be switched 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:

    Terminal
    $ sudo apt install libopenblas-dev
    $ sudo update-alternatives --config libblas.so.3-$(arch)-linux-gnu
    
  • On RHEL/CentOS and SUSE Linux, the BLAS library can be switched 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:

    Terminal
    $ 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.

We recommend installing several optional system dependencies that are used by common R packages. Additional information about installing them is provided in our System Dependency Detection documentation.

(Optional) Install multiple versions of R#

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