How to install R in Debian

Please note that the following information is taken from the official website of R-Project and all the credits goes to them. For more information please check their website:
https://cran.r-project.org/bin/linux/debian/

General information

Packages for the base R system have been part of the Debian distribution since 1997, thanks to Douglas Bates, and are diligently maintained by Dirk Eddelbuettel since 2001. R, as well as many add-on packages (from CRAN and other repositories) are available via the regular Debian distribution mechanisms. Hence, running

in a shell should get you started with a list of available packages. In the following, commands that need write access to system areas are prepended by sudo, assuming that you do not have root privileges.

Debian sid (unstable) and experimental

The latest stable version of R and many R packages from CRAN and the Bioconductor project are usually available in the official Debian sid repositories. During the so-called freeze period before new releases, the latest versions of R and some of these extension packages may only be available in experimental.

For the other distributions (testing during the freeze, stable, and older releases), backports of the latest R version and some related packages are provided here.

Installation

With an appropriate entry in /etc/apt/sources.list (see below for Debian branches other than sid/unstable or experimental), the newest R release including recommended packages can be installed using a command sequence like

Code
sudo apt update
sudo apt install r-base r-base-dev

If you get a warning about a missing public key, chances are that you have enabled one of the repositories described further below. Please refer to the section on secure apt for getting the key for secure retrieval of the packages.

You only need r-base-dev if you want to compile R packages yourself or other software depending on R (see section below on administration and maintenance). Be aware that you may also have to install build dependencies (typically -dev packages containing headers). The list r-sig-debian is a good place to ask if you run into problems.

You may want to install the automatically tuned Atlas or the multi-threaded OpenBlas library in order to get higher performance for linear algebra operations

Code
sudo apt install libatlas3-base

or

Code
sudo apt install libopenblas-base

Administration and Maintenance

The R packages part of r-base and r-recommended are installed into the directory /usr/lib/R/library. The other R packages available as precompiled Debian packages r-cran-* and r-bioc-* are installed into /usr/lib/R/site-library.

These can be updated using usual Debian package maintenance tools like apt or aptitude.

The command

Code
apt rdepends r-base-core

shows all packages that depend on r-base-core. This comprises a large number of contributed packages from CRAN and other repositories.

If you want to install R packages not provided as Debian packages, or if you want to use newer versions, you need to build them from source which requires the development package r-base-dev that can be installed by

Code
sudo apt install r-base-dev

This pulls in the basic requirements for compiling R packages. R packages may then be installed by the local user/admin from the CRAN source packages, typically from inside R using the

Code
install.packages()

function or using

Code
R CMD INSTALL

from a shell. If you have proper write permissions in /usr/local/lib/R/site-library/, and you have not set R_LIBS_USER manually, they will be installed there. Otherwise, you will be asked if a directory in your home directory should be created for these packages. A routine update of such locally compiled packages can be done within R using

Code
update.packages(.libPaths()[1])

which will update the packages in the first part of your library path. You can have a look at the components of this path using the R command

Code
.libPaths()

If you would like to update R packages that have been installed via the Debian package management system which are installed somewhere under /usr/lib/, I would recommend to do this the Debian way using the source packages from Debian unstable.

Pathways to R Packages

In order to find packages, R looks at the variables R_LIBS_USER and R_LIBS_SITE. On Debian and Ubuntu, R_LIBS_USER is set in /etc/R/Renviron to

Code
R_LIBS_USER=${R_LIBS_USER-'~/R/$platform-library/4.0'}

where $platform is something like 'x86_64-pc-linux-gnu' and depending on the version of R installed. You can override this in ~/.Renviron. R_LIBS_SITE is set in /etc/R/Renviron to

Code
R_LIBS_SITE=${R_LIBS_SITE-'/usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library'}

This means that packages installed from within R take precedence over the ones installed via the Debian package management system if you happen to have two versions installed at the same time.

Backports on CRAN

As R and related packages are currently not part of the official Debian backports repositories like buster-backports (please contact r-sig-debian if you would like to change this), the R versions in released or frozen Debian releases get out of date. Therefore, updates of a subset of these R related packages are provided here for such Debian releases.

Supported packages

The packages recommended by the R core team (r-recommended) are updated in the Debian repositories on CRAN upon each new release of R (at least for the main architectures amd64 and i386). These packages are:

Backports of the following packages are also supplied and kept up to date in the same manner:

Code
littler
rpy2
rkward

Any help is welcome in case you would like to see additional packages here.

Supported branches

Secure apt

The key ID used to sign current Debian package repositories on CRAN has ID 0xB8F25A8A73EACF41, key fingerprint 95C0FAF38DB3CCAD0C080A7BDC78B2DDEABC47B7 and user ID Johannes Ranke <[email protected]>.

You can fetch the key from the keyserver run by the Ubuntu project:

Code
gpg --keyserver keyserver.ubuntu.com \    --recv-key '95C0FAF38DB3CCAD0C080A7BDC78B2DDEABC47B7'

In case you are behind a firewall blocking port 11371, you can specify a proxy server by adding --keyserver-options http-proxy=<PROXY> in the above command.

Then you export it and write it to the directory where apt will trust it (overwriting any pre-existing file with the same name, if existing):

Code
gpg --armor --export '95C0FAF38DB3CCAD0C080A7BDC78B2DDEABC47B7' | \    sudo tee /etc/apt/trusted.gpg.d/cran_debian_key.asc

Debian bookworm (testing)

Debian bookworm is to be released in spring 2023 and is currently frozen. If you use bookworm and would like to use R 4.3.0, you can use the following repository.

Code
deb http://cloud.r-project.org/bin/linux/debian bookworm-cran40/

Note that R 4.3.x has bumped the Graphics API, so graphics packages like svglite, tikzDevice, rgl, rvg, ggplot2 or vdiffr (embedding svglite) will need to be reinstalled. This also affects the graphics device supplied by rkward. If you don’t you will get an error message

Code
Graphics API version mismatch

Debian bullseye (stable)

Debian bullseye has been released with R 4.0.4. If you want to upgrade to R 4.3.0 on bullseye, you can use the following repository.

Code
deb http://cloud.r-project.org/bin/linux/debian bullseye-cran40/

Note that R 4.1.x, R 4.2.x and R 4.3.x have bumped the Graphics API, so graphics packages like svglite, tikzDevice, rgl, rvg, ggplot2 or vdiffr (embedding svglite) will need to be reinstalled. This also affects the graphics device supplied by rkward. If you don’t you will get an error message

Code
Graphics API version mismatch

Debian buster (oldstable)

For a backport of R 4.3.0 to buster, please add

Code
deb http://cloud.r-project.org/bin/linux/debian buster-cran40/

to the file /etc/apt/sources.list on your computer.

Due to the API change (R 4.0.x and 4.1.0 provide r-api-4), installing R 4.x.x will remove all Debian packages dependent on r-api-35, with the exception of the ones provided here. Also, you will need to update R packages that you have obtained directly from CRAN or Bioconductor.

Please refer to the note above on the Graphics API version bump.

In addition, if you had a large number of locally installed packages before the change to r-api-4, updating them using e.g. the R command given below may take some time, also depending on the number of CPU threads you run in parallel:

Code
update.packages(lib.loc="/usr/local/lib/R/site-library", ask = FALSE,  checkBuilt = TRUE, Ncpus = 16)

In my case, using 16 parallel threads as specified in the command above, the downloading took about eight minutes and the first round of installation with heavy CPU load took another 12 minutes. Then it stopped mentioning that I needed to manually reinstall testthat, which I did using install.packages(). This may be necessary due to a circular dependency, as Dirk pointed out in his upgrade report on his blog. After I did that, I ran the update command again and it took another six minutes until it successfully completed.

If you have Bioconductor packages installed as well, you need to switch to the current version using the commands listed here.

A backport of R 3.6.3 to buster is also available, which does not require reinstalling extension packages.

Code
deb http://cloud.r-project.org/bin/linux/debian buster-cran35/

Debian stretch (oldoldstable)

For a backport of R 3.6.3 to stretch, please add

Code
deb http://cloud.r-project.org/bin/linux/debian stretch-cran35/

to the file /etc/apt/sources.list on your computer.

Please note that R packages from the Debian stretch distribution are not compatible with R 3.5.x, as it provides r-api-3.5, while the stretch packages depend on r-api-3.

Also, local packages installed before the update to R 3.5.0 will not work correctly if they use compiled code.

R packages you have compiled locally can be updated within R using

Code
update.packages(lib.loc="/usr/local/lib/R/site-library", ask=FALSE, checkBuilt=TRUE)

There is also an old repository containing R 3.4.4 that still has arm64 and armhf binaries:

Code
deb http://cloud.r-project.org/bin/linux/debian stretch-cran34/

Supported Platforms

There are i386 and amd64 binaries for all Debian releases. Since R 3.1.0, R is fully functional on arm and armel binaries for jessie and wheezy were provided here up to R 3.4.2. Since R 3.4.2 and up to R 3.4.4, binaries for armhf and arm64 were provided for Debian stretch (with the exception of R 3.4.3 on arm64, see this bug).

For other architectures and releases, you can use the source packages from one of these repositories

Code
deb-src cloud.r-project.org/bin/linux/debian bookworm-cran40/
deb-src cloud.r-project.org/bin/linux/debian bullseye-cran40/
deb-src cloud.r-project.org/bin/linux/debian buster-cran40/
deb-src cloud.r-project.org/bin/linux/debian buster-cran35/
deb-src cloud.r-project.org/bin/linux/debian stretch-cran35/
deb-src cloud.r-project.org/bin/linux/debian jessie-cran35/

to compile binary Debian packages.

Reporting Problems

The best place to report problems with these packages or ask R questions specific to Debian is the r-sig-debian mailing list. See

R-SIG-Debian Info Page

for more information.

Backporting Debian packages for CRAN

Anyone interested in building Debian packages (e.g. for an unsupported release, another architecture or an old R version) can have a look at the build scripts used by the current maintainer. These can be inspected at

r-backports - Scripts used for backporting Debian R packages for CRAN

or cloned from the git repository

Code
git clone https://cgit.jrwb.de/r-backports

The scripts contain some small changes needed to compile the original Debian packages on the supported release.

Installing R-devel or a release branch from svn

The following notes are an attempt to merge the hints given in a thread on r-sig-debian started end of March 2015, and the tips given at https://developer.r-project.org/SVNtips.html. I have not used this a lot and I assume you have some experience in building software on unix systems. No warranties, your mileage may vary.

First, make sure you have a source repository in your /etc/apt/sources/list, like

Code
deb-src cloud.r-project.org/bin/linux/debian bullseye-cran40/

in order to make it easier to get the build dependencies. Then update your list of available packages and get the build dependencies

Code
sudo apt update
sudo apt build-dep r-base

To install the build dependencies. The following commands do not need root privileges, and are safer to run as a normal user.

I keep all sources managed by subversion under ~/svn, so I do

Code
export RTOP=~/svn/R

Please adapt to your needs. The location of the source code for the base R system is conveniently defined as en environment variable by

Code
export REPOS=https://svn.r-project.org/R

R-devel

We change to our RTOP directory and check out the latest revision of R-devel

Code
cd $RTOP
svn co $REPOS/trunk r-devel/source
mkdir $RTOP/r-devel/build

The second time around we only need to

Code
cd $RTOP/r-devel/source
svn up

Then we need to get the sources of the recommended packages

Code
cd $RTOP/r-devel/source/tools
./rsync-recommended

We build in the separate directory created above, in order not to pollute the source code

Code
cd $RTOP/r-devel/build
../source/configure

There are a lot of configure options you might want to use, for example you will probably want --with-blas="-lopenblas" to get more performant linear algebra operations. This depends on libopenblas-dev to be installed, which, per default, will pull libopenblas-pthread-dev on Debian bullseye.

We are not using the install target here, so we do not set --prefix. This is to avoid conflicts with the Debian packages of released versions of R. The next step builds the sources.

Code
make
make check
make pdf
make info

Of course doing checks and making pdf and info documentation is optional. Then we use links to make R-devel and Rscript-devel available (obviously supposes write permission on /usr/local/bin).

Code
cd /usr/local/bin
ln -s /$RTOP/r-devel/build/bin/R R-devel
ln -s /$RTOP/r-devel/build/bin/Rscript Rscript-devel

This will make the commands R-devel and Rscript-devel available. Note that the only entry on .libPaths() is $RTOP/r-devel/build/library, so this is where packages will be installed.

R-patched

After the release of a version R-x.y.0, the so-called release branch is used to introduce changes that meet the development guidelines.

We can build such branches (aka R-patched) using the following scheme, which is adapted from the procedure given for R-devel, but without duplicating all comments.

At the time of this writing, the release branch can be defined by

Code
export RPATCHED=branches/R-3-5-branch

R-patched can than be built and made available by

Code
cd $RTOP
svn co $REPOS/$RPATCHED r-patched/source
mkdir $RTOP/r-patched/build
cd $RTOP/r-patched/source/tools
./rsync-recommended

Again, we build in the separate directory created above, in order not to pollute the source code

Code
cd $RTOP/r-patched/build
../source/configure
make
make check
make pdf
make info

Then, as privileged user:

Code
cd /usr/local/bin
ln -s /$RTOP/r-patched/build/bin/R R-patched
ln -s /$RTOP/r-patched/build/bin/Rscript Rscript-patched

The builds can be updated by updating the sources from the repository, syncing the latest versions of the recommended packages and rebuilding.

Debian R policy

An RFC on a first public draft of ‘Debian R Policy’ has been posted to debian-devel in 2003.

Contributions

This document is maintained by Johannes Ranke [email protected]. The Debian R packages are maintained by Dirk Eddelbuettel. The backported packages present on CRAN are provided by Johannes Ranke. Thanks to Mathieu Basille for restructuring the README in March 2015.