24

I am pretty new to Linux. I installed R-base in my Ubuntu 12.04 using the Software Center (which by default is r-2.14). I want to upgrade to/install R 3.02 or newer. How can I do that? Thank you.

muru
  • 197,895
  • 55
  • 485
  • 740
jahirju30
  • 241
  • 1
  • 2
  • 4

4 Answers4

27

You need to add R's repository to your system:

  1. Use your favorite text editor (I'm using gedit as an example) to open /etc/apt/sources.list:

    sudo -H gedit /etc/apt/sources.list
    
  2. Add this line to the file (if this is slow, use another mirror. You may also want to change precise into the codename for your Ubuntu version --- e.g., trusty for 14.04):

     deb http://cran.rstudio.com/bin/linux/ubuntu precise/
    
  3. Update the list of packages

    sudo apt-get update
    
  4. Install the latest R-base (you can also use the software center again):

    sudo apt-get install r-base
    
muru
  • 197,895
  • 55
  • 485
  • 740
terdon
  • 100,812
  • Are there any changes in the second command for ubuntu 14.04 users? – Vineet Kaushik Nov 14 '14 at 05:48
  • @VineetKaushik yes, change precise to quantal. – terdon Nov 14 '14 at 09:51
  • @terdon You need a trailing slash after precise in step 2, as precise/. I know it's a bit different than most apt repositories, but that's how the instructions at cran.rstudio.com/bin/linux/ubuntu spell it out. – Daniel Kessler Dec 18 '14 at 16:31
  • @DanielKessler does the slash make any differenece? Trailing slashes are usually optional and can be left out. The version name is just a directory in the repository so it really shouldn't make any difference. – terdon Dec 19 '14 at 07:31
  • At least on my rig, if you leave off the slash, apt complains about a malformed line – Daniel Kessler Dec 24 '14 at 19:01
  • I am using ubuntu 12.10, i have tried precise, trusty and quantal nothing seems to upgrade my r version. I have 3.0.2 r version installed. I need to update my rstudio and r version. Please help. – Arun Raja Nov 14 '15 at 03:43
  • @ArunRaja upgrade your OS. 12.10 is 3 years old and no longer supported, you won't find updated packages for it. – terdon Nov 14 '15 at 11:14
  • 1
    to avoid warnings/errors about gpg keys, use secure apt: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9 https://cran.rstudio.com/bin/linux/ubuntu/#secure-apt – amc Jun 14 '16 at 03:03
3

Having had to spend time figuring this out and forgetting how it works, and then having to figure it out again multiple times, here's a more complete answer that is future-proof.

  1. Edit the sources.list file. This file contains the servers that apt-get consults to check whether software exists and where it can be downloaded from. One can edit the file using the following command:

    sudo -H gedit /etc/apt/sources.list
    

    This requires the gedit editor. If you get an error, either install this (sudo apt-get install gedit) or use another editor like nano (sudo nano /etc/apt/sources.list).

  2. Find a working server to download R from that also has the version of R you are interested in. This often means that one has to look for the name of the latest Ubuntu release. A list of releases is maintained on the Ubuntu website. Look for the latest released version and use only the first word in its name without capitalization. For instance, for the 16.04 release, the full name is Xenial Xerus and the name to use is thus xenial. Thus, we add the following line to the sources.list:

    deb http://cran.rstudio.com/bin/linux/ubuntu xenial/
    

    Note that the above line uses the rstudio.com mirror. One can choose another mirror from this long list and appropriately alter the URL. For instance:

    deb http://mirrors.dotsrc.org/cran/bin/linux/ubuntu xenial/
    
  3. Save and close the file. After this, one can install the newest version using:

    sudo apt-get update # update apt-get's list of known releases 
    sudo apt-get install r-base # install the newest available version of R
    
muru
  • 197,895
  • 55
  • 485
  • 740
1

The answers so far are useful but they all omit the next step which is pretty much going to be required of anyone who intends to use R seriously. Quoted lines are from the canonical R Installation and Administration Manual:

Users who need to compile R packages from source [e.g. package maintainers, or anyone installing packages with install.packages()] should also install the r-base-dev package:

sudo apt-get install r-base-dev

I think potential installers should be reading that Manual more carefully than the recommendations on this page have so far advised.

-1

For Ubuntu 14.04 LTS the commands are

sudo -H gedit /etc/apt/sources.list

deb http://cran.rstudio.com/bin/linux/ubuntu quantal/

*Note: the forward slash is required otherwise you get an error

sudo apt-get update 

sudo apt-get install r-base
muru
  • 197,895
  • 55
  • 485
  • 740
Arthur
  • 119
  • 2
  • 4
    What? Why quantal when http://cran.r-project.org/bin/linux/ubuntu/ lists trusty? – muru Nov 25 '14 at 04:26
  • Here's an Ansible task that can be used to accomplish this same task:
    • name: Add CRAN repository to APT repository list apt_repository: repo='deb http://cran.rstudio.com/bin/linux/ubuntu quantal/' state=present
    – Daniel Neel Dec 27 '15 at 05:38
  • 1
    to avoid warnings/errors about gpg keys, use secure apt: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9 https://cran.rstudio.com/bin/linux/ubuntu/#secure-apt – amc Jun 14 '16 at 03:04