-
Notifications
You must be signed in to change notification settings - Fork 1
/
using-multiple-R-versions-on-linux.Rmd
396 lines (268 loc) · 11 KB
/
using-multiple-R-versions-on-linux.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
---
title: "Using multiple R versions on Linux"
author: "Alex M. Chubaty"
date: "29 Jun 2023"
output:
pdf_document:
highlight: pygments
toc: yes
toc_depth: 3
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
# Overview
Unlike on Windows, most R users on linux tend to work with a single version of R -- whichever version is available in their software repositories.
With a little bit of work, it is possible to use multiple versions of R on Linux; however, you will need an account with `sudo` privileges to set this up.
The most common use case is to install R-devel alongside the current R version in order to facilitate package checking and testing prior to CRAN submission.
Additionally, you may wish to maintain older versions of R to maintain compatibility and reproducibility of old projects, scripts, etc.
This document provides an overview of several approaches to working with multiple versions of R on Linux - although these can also be used on macOS and Windows.
\newpage
# 1. Using Docker
This is the preferred solution, as it is self-contained.
Every time you start an instance of R in a Docker container, it starts in a "factory fresh" state, which means you will need to install additional packages into each new instance or save (commit) your changes for later reuse.
The advantages here are that packages installed for each project are kept separate, ensuring proper reproducibility and portability.
(Although [`renv`](https://rstudio.github.io/renv/) is also good for maintaining separate package libraries per project, it doesn't ensure system dependency separation.)
Please note that this guide is intended only to get you started with using R with Docker, not to be a full tutorial for more advanced Docker use.
See the official [Docker documentation](https://docs.docker.com/) and the [rocker project](https://github.com/rocker-org/rocker) for more advanced usage, including using containers with RStudio Server installed[^1] or using a virtual X server.
[^1]: <https://github.com/rocker-org/rocker-versioned2>
## Older R versions
1. Install Docker following the instructions [here](https://docs.docker.com/engine/installation/).
2. Download the image for the version of R you wish to use:
```bash
docker pull r-base:3.6.3
```
3. Start a container running the R version of your choice:
```bash
docker run -it --rm r-base:3.6.3
```
\* NOTE: the `--rm` flag tells docker to remove the container after use.
If you wish to keep the container for reuse later (*e.g.*, so you don't need to reinstall packages, etc.), then omit this.
## R-devel
1. Install Docker following the instructions [here](https://docs.docker.com/install/linux/docker-ce/ubuntu/).
2. Download the latest R-devel image:
```bash
docker pull rocker/drd
```
3. Start a container running R-devel:
```bash
docker run -it --rm rocker/drd Rdevel
```
\* NOTE: the `--rm` flag tells docker to remove the container after use.
If you wish to keep the container for reuse later (*e.g.*, so you don't need to reinstall packages, etc.), then omit this.
## Using RStudio
Alternatively, there are docker images that include RStudio server built-in, so you can run a container and connect to it through your browser instead of having to work in a terminal.
1. *E.g.*, start a versioned container running RStudio that includes geospatial libraries from the unstable ubuntugis repo:
```bash
docker run --rm -ti -p 127.0.0.1:8080:8787 rocker/geospatial:4.2.3-ubuntugis
```
2. Look for the user password to appear (in red) once it's done setting up the container.
3. Open a web browser and go to `localhost:8080` to get to the RStudio instance running in the docker container.
4. Log in with username `rstudio` and the password from step 2.
\newpage
# 2. Using `rig`
Using the R installation manager `rig` (<https://github.com/r-lib/rig>) is the easiest approach, as it automates the installation an linking of different R versions, and is cross-platform (works with Windows, macOS, and Linux).
Although each version of R maintains separate package libraries, users should strive to use per-project package libraries, e.g., using [`renv`](https://rstudio.github.io/renv/).
1. Install `rig` using the platform-specific installation instructions are provide at <https://github.com/r-lib/rig#id-installation>.
E.g., on Ubuntu 20.04:
```bash
## add apt repository
sudo curl -L https://rig.r-pkg.org/deb/rig.gpg -o /etc/apt/trusted.gpg.d/rig.gpg
sudo sh -c "echo 'deb [arch=amd64] http://rig.r-pkg.org/deb rig main' > \
/etc/apt/sources.list.d/rig.list"
## install (NB: rig is different package; use r-rig!)
sudo apt update
sudo apt install r-rig
```
2. Install the R versions you need:
```bash
rig add release
rig add devel
```
```bash
rig add 4.1
rig add 4.2
rig add 4.3
```
You can see the installed versions (and the default, starred) by using `rig list`.
Set the default version using the names from `rig list`:
```bash
rig default 4.3-arm64
```
3. Setup symlinks to allow easy launch of versioned R sessions:
```bash
rig system make-links
```
E.g., to launch R 4.2 on macOS, use `R-4.2-arm64`
4. On macOS, restrict access to system package directories:
```bash
rig system fix-permissions
```
5. On macOS can also use the menu bar app (<https://github.com/r-lib/rig#id-macos-menu-bar-app>) to manage their R versions:
```bash
open -a Rig
```
Be sure to open the Rig app preferences to allow launch at startup.
## Switching R versions using `rig`
### Setting the default version
```bash
## e.g., on macOS:
rig default 4.3-arm64
```
Users on macOS can also use the menu bar app.
### Selectively launching an RStudio seesion using specific version of R
```bash
## e.g., on macOS:
rig rstudio 4.2-arm64
## e.g., for a project using renv on ubuntu:
cd ~/path/to/project
rig rstudio renv.lock
```
Users on macOS can also use the menu bar app.
\newpage
# 3. Manual build + installation
As noted above, using Docker or `rig` are likely better solutions, as manual installations require manual upkeep and manual *uninstallation*.
## Older R versions
1. Install prerequisites for building R from source:
```bash
sudo apt build-dep r-base
```
2. Create directories to keep the R source code:
```bash
mkdir ~/R/src
```
3. Get the source code for the version of R you wish to install:
R 3.y.z:
```bash
cd ~/R/src/
RVERSION=3.6.3
wget https://cran.r-project.org/src/base/R-3/R-${RVERSION}.tar.gz
tar -xvzf R-${RVERSION}.tar.gz
```
R 4.y.z:
```bash
cd ~/R/src/
RVERSION=4.2.3
wget https://cran.r-project.org/src/base/R-4/R-${RVERSION}.tar.gz
tar -xvzf R-${RVERSION}.tar.gz
```
4. Install:
```bash
cd ~/R/src/R-${RVERSION}
./configure --prefix=/usr/local/lib/R-${RVERSION} --enable-R-shlib --with-blas --with-lapack
make
sudo make install
```
5. Create symlink:
```bash
sudo ln -s /usr/local/lib/R-${RVERSION}/bin/R /usr/local/bin/R-${RVERSION}
```
6. Run a specific R version:
From the commandline, simply do (according to `${RVERSION}`):
```bash
R-3.6.3
# or
R-4.2.3
```
\newpage
## R-devel
1. Install prerequisites for building R from source:
```bash
sudo apt build-dep r-base
sudo apt install ccache subversion xorg-dev
```
2. Create directories to keep the R-devel source code and to `make` it:
```bash
mkdir -p ~/R
mkdir -p ~/GitHub/r-config/R-devel/src/
ln -s ~/GitHub/r-config/R-devel/src ~/R/src
```
3. Get the latest version of R-devel from the subversion repository:
```bash
cd ~/R/src
svn co https://svn.r-project.org/R/trunk r-devel/R
```
4. Link the recommended packages for building R:
```bash
cd ~/R/src/r-devel/R
bash ./tools/rsync-recommended
bash ./tools/link-recommended
```
5. Use the installation script in `R-devel/scripts/build-R-devel.sh`:
```{r comment=''}
cat(readLines("R-devel/scripts/build-R-devel.sh"), sep = '\n')
```
6. Give the script execute permissions:
```bash
chmod a+x ~/GitHub/r-config/R-devel/scripts/build-R-devel.sh
```
7. Make and install R-devel:
```bash
mkdir -p ~/R/src/R-devel-build
bash ~/GitHub/r-config/R-devel/scripts/build-R-devel.sh
cd ~/R/src/R-devel-build
sudo make install
```
8. Create custom script to launch R-devel in `R-devel/scripts/R-devel.sh`:
```{r comment=''}
cat(readLines("R-devel/scripts/R-devel.sh"), sep = '\n')
```
Note that this keeps the R-devel package library separate from your regular R libraries.
Be sure to give the script execute permissions:
```bash
chmod a+x ~/GitHub/r-config/R-devel/scripts/R-devel.sh
```
9. Create custom script to launch Rscript-devel in `R-devel/scripts/Rscript-devel.sh`:
```{r comment=''}
cat(readLines("R-devel/scripts/Rscript-devel.sh"), sep = '\n')
```
Note that this keeps the R-devel package library separate from your regular R libraries.
Be sure to give the script execute permissions:
```bash
chmod a+x ~/GitHub/r-config/R-devel/scripts/Rscript-devel.sh
```
10. Create symlinks to launch R-devel and Rscript-devel:
```bash
sudo ln -s ~/GitHub/r-config/R-devel/scripts/R-devel.sh /usr/local/bin/R-devel
sudo ln -s ~/GitHub/r-config/R-devel/scripts/Rscript-devel.sh /usr/local/bin/Rscript-devel
```
11. Create a script used to update R-devel in `R-devel/scripts/update-R-devel.sh`:
This could be turned into a `cron` job.
```{r comment=''}
cat(readLines("R-devel/scripts/update-R-devel.sh"), sep = '\n')
```
Be sure to give the script execute permissions:
```bash
chmod a+x ~/GitHub/r-config/R-devel/scripts/update-R-devel.sh
```
12. Run R-devel:
From the commandline, simply do:
```bash
R-devel
```
\newpage
## Manually switching R versions for use with RStudio
RStudio will check for an environment variable, `RSTUDIO_WHICH_R`, which can be set to override using the installed system version of R.
### RStudio desktop
```bash
export RSTUDIO_WHICH_R=/usr/local/bin/R-4.2.3
rstudio &
```
### RStudio server
As `sudo` user:
```bash
umask 0022
echo "export RSTUDIO_WHICH_R=/usr/local/bin/R-3.6.3" > /etc/profile.d/rstudio.sh
```
# References
- <https://stackoverflow.com/a/24019938/1380598>
- <https://stat.ethz.ch/pipermail/r-sig-debian/2012-August/001937.html>
- <http://singmann.org/installing-r-devel-on-linux/>
- <https://github.com/rocker-org/rocker-versioned2>
- <https://hub.docker.com/r/rocker/drd/~/dockerfile/>
- <https://support.posit.co/hc/en-us/articles/218004217-Building-R-from-source>
- <https://community.rstudio.com/t/use-a-different-r-version-temporarily-in-rstudio/20848/8>
- <https://solutions.posit.co/envs-pkgs/environment-variables/>
- <https://github.com/r-lib/rig>