How to Upgrade R in Linux

How to upgrade R in linux?


Note: I now keep on GitHub (here) an up-to-date guide to upgrading R on Linux Mint or Ubuntu Linux, which also includes a bit of extra information about system dependencies for tidyverse, the popular set of data-wrangling packages, as well as devtools, the popular R package development... package.


The link provided by FedRo is a good resource, however a slight change would need to be made since you're using Linux Mint 18.1 which uses Xenial repositories rather than Trusty repositories (see here). I also typically use the approach here to deal with packages I've already installed when I upgrade R rather than the approach offered by FedRo. So, for completeness, these are all the steps you'd need to take:

Step 1

Go to CRAN's list of mirrors and find the URL of the mirror that is closest to you. The terminal commands below assume you choose http://cran.wustl.edu/

Step 2

Open a terminal and enter the following commands1 (replacing http://cran.wustl.edu/ with whichever CRAN mirror URL you chose in step 1):

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
sudo echo "deb http://cran.wustl.edu/bin/linux/ubuntu xenial/" | sudo tee -a /etc/apt/sources.list
sudo apt update
sudo apt upgrade r-base r-base-dev
sudo apt update
sudo apt upgrade

Note also that I have put to upgrade r-base and r-base-dev, but I don't know if you have r-base-dev installed. If not, I highly recommend you install it via sudo apt install r-base-dev.

Step 3

Start a new R session and run the following:

update.packages(checkBuilt=TRUE, ask=FALSE)

Then you should be good to go.

Update: Linux Mint 19 and R 3.6.x

Since both Linux Mint and R have seen upgrades since I answered this question, I'm updating for those who come to this answer needing the info for Linux Mint 19 and R 3.6.x.

The only difference is that instead of the command

sudo echo "deb http://cran.wustl.edu/bin/linux/ubuntu xenial/" | sudo tee -a /etc/apt/sources.list

you need to use

sudo echo "deb http://cran.wustl.edu/bin/linux/ubuntu bionic-cran35/" | sudo tee -a /etc/apt/sources.list

(replacing http://cran.wustl.edu/ with whichever CRAN mirror URL you chose in step 1)


1 I put here the full key, though many other guides you may see will use only the "short key."
I have updated this guide to use the full key out of security concerns (see here, for example).

How to update to specific R version on Linux (Red Hat/CentOS), keeping the previous version?

Question 1: I'm not sure why, but having multiple versions of R on your PATH can lead to unexpected situations like this. /usr/local/bin is usually ahead of /usr/bin in the PATH, so I would've expected R 3.6.3 to be found. Perhaps it has to do with Question 2.

Question 2: Some distros (like CentOS/RHEL) don't put /usr/local/bin on the PATH by default when using sudo. See https://unix.stackexchange.com/questions/8646/why-are-path-variables-different-when-running-via-sudo-and-su for details. The answers there describe several ways to add /usr/local/bin to the PATH when using sudo -- for example, modifying secure_path in /etc/sudoers to include /usr/local/bin like:

Defaults    secure_path = /usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

With R 3.6.3 ahead of the default system R in the PATH, you shouldn't have to delete /bin/R or /usr/bin/R. But eventually, I'd recommend installing multiple side-by-side versions of R the same way, using https://docs.rstudio.com/resources/install-r/, so it's easier to manage. The next time you install a new R version, you can just replace the symlinks in /usr/local/bin. The default system R (from EPEL) is meant to be the only R on a system, with in-place upgrades.

If you want to replace the default R 3.5.2 with a side-by-side R 3.5.2 (or 3.5.3), you could install R 3.5 from https://docs.rstudio.com/resources/install-r/, install all necessary packages, and have Shiny Server use the new R 3.5. Then uninstall the R from EPEL (R-core or R-core-devel) to fully switch over. From there, you could even create symlinks to R in /usr/bin instead of /usr/local/bin, and not worry about adding /usr/local/bin to the sudo PATH.

How to update R from 3.x to 4.x and should I?

I did this a few weeks ago.

UpdateR didn't migrate the packages as expected, so that I had to do it manually :

  • Install R 4.0.x
  • Install RTools 4.0, take care to set $Path correctly as instructed. This new version is necessary for R 4.0 to compile from source some packages still in 3.x
  • Migrate the packages from R 3.x to R 4.x :
# locate packages
.libPaths()
[1] "/Documents/R/win-library/4.0"
# Migration list : replace previous 4.0 by 3.x in old_lib_loc
old_lib_loc <- "/Documents/R/win-library/3.x"
to_install <- unname(installed.packages(lib.loc = old_lib_loc)[, "Package"])
# Check if list is OK or if you want to clean up
to_install
[1] "abind" "acepack" "ada" "alphavantager"
[6] "ash" "askpass" "assertive" "assertive.base" "assertive.code"
# Migrate
install.packages(pkgs = to_install,Ncpus = 4)

Packages still in 3.x are automatically recompiled from source.

That's it, no problems encountered since then.

How to upgrade R in ubuntu?

Since R is already installed, you should be able to upgrade it with this method. First of all, you may want to have the packages you installed in the previous version in the new one,so it is convenient to check this post. Then, follow the instructions from here

  1. Open the sources.list file:

     sudo nano /etc/apt/sources.list    
  2. Add a line with the source from where the packages will be retrieved. For example:

     deb https://cloud.r-project.org/bin/linux/ubuntu/ version/

    Replace https://cloud.r-project.org with whatever mirror you would like to use, and replace
    version/ with whatever version of Ubuntu you are using (eg, trusty/, xenial/, and so on). If you're getting a "Malformed line error", check to see if you have a space between /ubuntu/ and version/.

  3. Fetch the secure APT key:

     gpg --keyserver keyserver.ubuntu.com --recv-key E298A3A825C0D65DFD57CBB651716619E084DAB9

or

    gpg --hkp://keyserver keyserver.ubuntu.com:80 --recv-key E298A3A825C0D65DFD57CBB651716619E084DAB9

  1. Add it to keyring:

     gpg -a --export E084DAB9 | sudo apt-key add -
  2. Update your sources and upgrade your installation:

     sudo apt-get update && sudo apt-get upgrade
  3. Install the new version

     sudo apt-get install r-base-dev
  4. Recover your old packages following the solution that best suits to you (see this). For instance, to recover all the packages (not only those from CRAN) the idea is:

-- copy the packages from R-oldversion/library to R-newversion/library, (do not overwrite a package if it already exists in the new version!).

-- Run the R command update.packages(checkBuilt=TRUE, ask=FALSE).

Upgrade R version from 3.4.4 to 3.5.3 on Ubuntu 16.04

I would be surprised if there is a .deb available for this OS release/R release combination.

  • This page only offers R 4.1, but as far back as 16.04 LTS
  • This page offers older R versions, but only back to 18.04 LTS
  • It's also possible that one of the backports on the Debian packages page would work on your system (it reports that 3.5.3 is available for Debian "jessie", although I have no idea how that lines up with Ubuntu releases).

I guess it's possible that one of those .debs works for your system though ... ? If you do install from .deb, you may have to be careful not to clobber your current version (e.g. using the --instdir argument to dpkg).

Building from source:

  • download the source tarball for R 3.5.3
  • untar, ./configure, make, sudo make install (assuming you have all the necessary development tools, system libraries, etc.; you'll have to troubleshoot/install as you go along)
    • It might be worth ./configure --prefix=/path/to/testdir to put the new version in a completely separate location, just to make extra-sure you don't stomp on your current install. (This would also simplify the library-path stuff below.)

Packages do need to be re-installed when switching major versions (e.g. 3.4.x to 3.5.x); one way to do it is to copy the system library of packages to a new place (adjust library paths as necessary; see R installation and administration manual).

Then, you should be able to update.packages(checkBuilt=TRUE, ask=FALSE) to update everything.

The tricky spots are (1) getting the library paths right (this will depend a bit on how things are set up in your existing installation; (2) it's conceivable that some current versions of packages on CRAN will fail to re-install/re-build under R 3.5.3. devtools::install_version() would help, although you'd have to track down the correct version manually. I recall someone posting about a package that would install an archived version by date, which could save a lot of poking around ...



Related Topics



Leave a reply



Submit