Install R on Ubuntu 14.04

In this post I will go over installing R on Ubuntu 14.04 following the installation instructions at cran. I will also cover installing the packages required to do the Topics in machine learning assignment for Coursera’s Introduction to Data Science.

As it turns out, not much has changed from the post I wrote to install R on Ubuntu 12.04 back in 2012. First we add the cran repository we want to use for getting our R updates. I use the stats repository at UCLA with the following command:

$ sudo add-apt-repository "deb http://cran.stat.ucla.edu/bin/linux/ubuntu trusty/"

The use of trusty in the address above indicates that we want to install for Ubuntu 14.04. The archives are signed by a key that we should add for secure apt using:

$ gpg --keyserver keyserver.ubuntu.com --recv-key E084DAB9
$ gpg -a --export E084DAB9 | sudo apt-key add -

If you have problems with these commands consult the installation instructions at cran for alternatives to the above.

>>Note [2015, Dec]

If you installed R using this method before Oct 18th 2015, the key added above expired and you are probably getting errors about an Expired Key when updates are done on your machine– see the end of the post for a fix to this issue. If you’re installing for the first time, the instructions above will install the current (not-expired) key. So, ignore this note.

<<end Note

Now that we have the repository setup and the key added we do typical Ubuntu install commands to install R:

$ sudo apt-get update
$ sudo apt-get install r-base r-base-dev

r-base-dev is not required but I suggest installing because it might be needed to install other R packages later. This will download and install ~40MB worth of base packages and install everything.

Once finished with the install, you should be able startup R:

$ R

and see something like:

R version 3.1.1 (2014-07-10) -- "Sock it to Me"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

  R is a collaborative project with many contributors.
  Type 'contributors()' for more information and
  'citation()' on how to cite R or R packages in publications.

  Type 'demo()' for some demos, 'help()' for on-line help, or
  'help.start()' for an HTML browser interface to help.
  Type 'q()' to quit R.

  > quit()
  Save workspace image? [y/n/c]: n

Installing R packages

The above installation provides the following packages:

r-cran-boot
r-cran-class
r-cran-cluster
r-cran-codetools
r-cran-foreign
r-cran-kernsmooth
r-cran-lattice
r-cran-mass
r-cran-matrix
r-cran-mgcv
r-cran-nlme
r-cran-nnet
r-cran-rpart
r-cran-spatial
r-cran-survival
r-cran-rodbc

These will be automatically update, like normal Ubuntu updates, whenever new versions are available at cran.

If we want other (less standard) packages we have to install and update them by ourselves. The basic process is as follows:

  • Start R using sudo
$ sudo R
  • To install a package called packagename the command would be:
> install.packages('packagename', dep = TRUE)

Note: the quote around packagename are required. Updating packages is accomplished using:

$ sudo R

and

> update.packages()

Example

Finally, as an example of the above, I’ll install the packages required for the Topics in machine learning assignment in Coursera’s Introduction to Data Science. The needed packages are: caret, rpart, tree, randomForest, e1071 and ggplot2.

First, make sure that you have gcc and gfortran available (many will already have these installed) using:

$ sudo apt-get install gcc gfortran

Next, following the approach outlined above, we start R with sudo

$ sudo R

and then start installing (some of these download and compile a fair amount of code and data – be patient):

> install.packages('caret', dep = TRUE)
> install.packages('rpart', dep = TRUE)
> install.packages('tree', dep = TRUE)
> install.packages('randomForest', dep = TRUE)
> install.packages('e1071', dep = TRUE)
> install.packages('ggplot2', dep = TRUE)

That’s it, we are ready to go!

2015, Dec 1 – fixing the expired key

So, you’re updating away and you get (something like) the following error:

W: GPG error: http://cran.stat.ucla.edu trusty/ Release: The following signatures were invalid: KEYEXPIRED 1445181253 KEYEXPIRED 1445181253 KEYEXPIRED 1445181253

This means you probably followed the R install instructions before 2015-10-18 and the key used to authenticate the updates has expired. To verify this, you should get similar results:

$ apt-key list | grep expired
pub   2048R/E084DAB9 2010-10-19 [expired: 2015-10-18]

If we check– see Michael Rutter’s key – we see that the key has been updated. So, following this info on updating keys we can fix our issue using (be sure to use the key E084DAB9, as shown below, to fix our problem with R updates):

$ sudo apt-key adv --recv-keys --keyserver keys.gnupg.net E084DAB9

Finally, to do the R updates (as well as others that might be queued) immediately we can do:

$ sudo apt-get update
$ sudo apt-get upgrade

and (hopefully) we should be up-to-date and error-free!

Tags

api [1]   arduino [1]   audio [2]   audio features [1]   babel [1]   Bayesian [7]   Beta [1]   blog setup [1]   bootstrap [1]   bottleneck [1]   c++ [1]   caret [1]   cmpy [1]   conditional probability [6]   coursera [1]   coursera intro to data science [3]   css [1]   cython [1]   d3 [2]   decision trees [2]   diy [1]   dropbox [1]   dsp [1]   e1071 [1]   essentia [1]   garmin [1]   geojson [1]   ggplot2 [1]   gis [2]   git [1]   gnuplot [1]   graphs [1]   html5 [1]   igraph [1]   ipython [1]   javascript [7]   joint probability [6]   json [1]   LaTeX [2]   LDA [1]   Lea [2]   machine learning [3]   marginal probability [6]   matplotlib [1]   meteor [2]   mir [1]   MongoDB [3]   music [2]   my python setup [5]   my ubuntu setup [10]   mysql [3]   networks [1]   networkx [1]   nodejs [5]   npm [3]   numexpr [1]   numpy [1]   octave [1]   Open Oakland [2]   openpyxl [1]   pandas [3]   patsy [1]   pip [2]   pweave [1]   pygraphviz [1]   pymc [1]   PySoundFile [2]   python [15]   Python [1]   python 2.7 [5]   python 3.4 [2]   pyyaml [1]   qgis [1]   R [1]   randomForest [1]   restview [1]   resume [1]   rpart [1]   running [1]   scikit-learn [3]   scipy [1]   screen [1]   server setup [1]   shapefile [1]   social networks [1]   Socrata [1]   sound [2]   sphinx [1]   sql [4]   sqlite3 [1]   ssh [1]   ssh keys [1]   statsmodels [1]   supervised learning [2]   sympy [1]   tableau [1]   tinkerer [2]   topic models [1]   tree [1]   ubuntu 14.04 [13]   Ubuntu 14.04 [3]   ubuntu 16.04 [4]   vim [2]   virtualbox [1]   virtualenv [4]   virtualenvwrapper [3]   VPS [1]   vundle [1]   webpack [1]   yaml [1]