What is the preferred way to install R packages in Ubuntu?

I am installing in R in ubuntu, but I am a bit confused with the many options there are to install packages.

The traditional way is to install them, inside R, by doing install.package(). Ubuntu however offers the possibility of installing with apt-get. This is interesting since the OS will keep packages updated. Since they have only a couple of packages available it will create problems sooner or later. I will eventually have install packages through the traditional way, having them installed in different ways, and maybe (?) this will also create version problems.

What is the preferred way to install and keep R packages in Ubuntu?


I think the only R packages available through the Ubuntu repos are the packages maintained by the R development core team. Like with most packages, those available through apt or synaptic are not always the newest versions. Luckily you have the option of adding a CRAN-maintained software source by creating a new file at /etc/apt/sources.list.d/CRAN.list that contains a single line of text:

deb http://cran.r-project.org/bin/linux/ubuntu trusty/

(replace trusty/ with your distribution's codename; if you don't know it type lsb_release -c into a terminal). After that (and an apt update) your package manager will always install (or update to) the CRAN-maintained versions of r-base, r-base-core, etc., because they will have higher version numbers than the ones in the debian sources. See here if apt complains about the repo being unsigned.

For all other packages (AKA "contributed packages"), I prefer to install within R via install.packages(c("name_of_package", "name_of_other_package")) whenever possible, unless I know for a fact that the version on CRAN is old and that a newer version has functionality that I need. In such cases you can install from GitHub with devtools::install_github('AccountName/RepositoryName') or download an archive and install it from a terminal window via R CMD INSTALL name_of_package.tar.gz, or within R via install.packages("/path/to/downloaded/package.tar.gz", repos=NULL, type="source").

For completeness, here is the official guidance from CRAN.


I would usually install using apt-get r-cran whenever possible, as this takes care of dependencies and updates in a cleaner way. Only if it is not available I'll use install from within R.

Note that there are two repositories for R Debian packages:

  1. Official R: provides a few packages. To include it, you need to add the link to the repo updating your source.list file. This is documented on the CRAN-ubuntu page, then run apt-get update, then upgrade
  2. PPA: Michael rutter provides a ppa, cran2deb4ubuntu, which contains many more packages. For some documentation, see the end of this blog. You just need to run (then after update/upgrade):

     sudo add-apt-repository ppa:marutter/c2d4u