How can I load a specific version of R in linux?
You set the PATH accordingly. There are tools / libraries that do that for you (common in university environments with multiple versions of things in /usr/local/
or /opt
.
Here is a simple ad-hoc version:
edd@max:~$ which R # my default R
/usr/bin/R
edd@max:~$ R --version | head -1
R version 3.1.2 (2014-10-31) -- "Pumpkin Helmet"
edd@max:~$ cat bin/R-devel.sh # a wrapper I use
#!/bin/bash
export PATH="/usr/local/lib/R-devel/bin:$PATH"
R "$@"
edd@max:~$ # gives me another R
edd@max:~$ R-devel.sh --version | head -1
R Under development (unstable) (2014-11-11 r66970) -- "Unsuffered Consequences"
edd@max:~$
edd@max:~$ ( PATH="/usr/local/lib/R-devel/bin:$PATH" R --version | head -1 )
R Under development (unstable) (2014-11-11 r66970) -- "Unsuffered Consequences"
edd@max:~$
The change at the can be done by a script or in different ways -- the key is that by pre-prending PATH
with the one for the version you want, you end up with that version found first.
How to install a specific R version in ubuntu
You will need to build from source. Using this post as guideline: https://unix.stackexchange.com/questions/173/how-to-compile-and-install-programs-from-source
- Download the tar.gz for specific version
- Extract using
tar zxvf myapp.tar.gz
- cd into the directory by step 2
- run
./configure && make && sudo make install
Installing a specific version of R from an Apt repository
Thanks to @Chris' tip-off, the structure of said R packages is important to understand.
r-base
is a metapackage which includes, amongst other things, r-base-core
and r-recommended
. r-recommended
is another metapackage which includes a suite of recommended R packages, which introduce the incompatibility when trying to pin to versions.
For just the R binaries and the documentation, pinned to a specific ${VERSION}
, this will do the trick:
apt install -y --no-install-recommends \
r-base-core=${VERSION} \
r-base-html=${VERSION} \
r-doc-html=${VERSION}
If you want to build packages, you'd also want r-base-dev=${VERSION}
in there.
I need to switch to an old version of R to run one script
Changing your PATH
might be useful in some ways, but since (I'm inferring) that you intend this to be the exception, then just running this with the full path to the executable should be sufficient. If your task is encapsulated in a script named script.R
, then perhaps
$HOMD/.local/R_3.4.4/bin/x64/Rscript /path/to/script.R
should be sufficient.
This may bite you, though, depending on how the current R is installed. You said it's in /usr/bin/
, which suggests that its library path might be something like this:
$ Rscript -e '.libPaths()'
[1] "/usr/local/lib/R/site-library"
[2] "/usr/lib/R/site-library"
[3] "/usr/lib/R/library"
(where Rscript
here is the default/current version). Unfortunately, the packages in those directories are not stored as version-specific, so it is possible (even likely) that using a more-modern version of a package with R-3.4.4 will fail in some way.
Two strategies for preempting this possibility. The first works fine assuming that you have all packages for R-3.4.4 in a specific directory.
One way is to set
R_LIBS_USER
(env-var) for the command. This can be on the same command-line, or it can be controlled via some.Renviron
file or similar:$ mkdir -p .local/R-3.4.4/library
$ R_LIBS_USER=~/.local/R-3.4.4/library Rscript -e '.libPaths()'
[1] "/home/r2/.local/R-3.4.4/library"
[2] "/usr/local/lib/R/site-library"
[3] "/usr/lib/R/site-library"
[4] "/usr/lib/R/library"With this, assuming all packages needed are found in that first path, it will never look in the others, so there should be no conflicts experienced. If your script is stable/unchanging and you don't break your instance of R-3.4.4 within
.local
, then ... all is happy.Modify the script itself to hard-control the library paths. By over-riding
R_HOME
(env var), you can override all of the defaults.echo "R_HOME=~/.local/R-3.4.4" > .Renviron # current working directory, regardless of script.R
Rscript /path/to/script.RThis assumes that
.local/R_3.4.4
is a complete R installation ... it needs all of the base stuff as well as any other packages you may have installed.
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.
Related Topics
Find Directories Having Size Greater Than X Mb
Remove Occurrences of String in Text File
How to Pass All Arguments with Xargs in Middle of Command in Linux
How to Repeat a Dash (Hyphen) in Shell
How to Check Whether the Processor Cache Has Been Flushed Recently
Add Some Specific Time While Using the Linux Command "Date"
How to Apply Password to Sudo in One Line Command and Execute Su Root
Automatically Kill Process That Consume Too Much Memory or Stall on Linux
List Files Recursively in Linux Cli With Path Relative to the Current Directory
Adding Users to Sudoers Through Shell Script
Is Clock_Gettime() Adequate for Submicrosecond Timing
Using Find with -Exec {}, How to Count the Total
How Does Stat Command Calculate the Blocks of a File
Run Silverlight with Apache Server (Under Linux)