|
1 |
| -# Howto run Rstudio Server in a Conda Environment |
| 1 | +# Running Rstudio Server in a Conda Environment |
2 | 2 |
|
3 |
| -I usually rely on the [conda package manager]() to manage my environments during development. Thanks to [conda-forge](https://conda-forge.org/) and [bioconda](https://bioconda.github.io/) most R packages are now also available through conda. |
| 3 | +I usually rely on the [conda package manager](https://docs.conda.io/en/latest/) to manage my environments during development. Thanks to [conda-forge](https://conda-forge.org/) and [bioconda](https://bioconda.github.io/) most R packages are now also available through conda. For production, |
| 4 | +I [convert them to containers](https://github.com/grst/containerize-conda) as these are easier to share. |
4 | 5 |
|
5 |
| -Unfortunately, there seems to be [no straightforward way](https://community.rstudio.com/t/start-rstudio-server-session-in-conda-environment/12516/15) to use conda envs in Rstudio server. This is why I came up with the two scripts in this repo. |
| 6 | +Unfortunately, there seems to be [no straightforward way](https://community.rstudio.com/t/start-rstudio-server-session-in-conda-environment/12516/15) to use conda envs in Rstudio server. This repository provides two approaches to make rstudio server work with conda envs. |
6 | 7 |
|
7 |
| -## Installation and usage |
8 |
| -### 1. Prerequisites |
9 |
| -* installed [rstudio server](https://www.rstudio.com/products/rstudio/download-server/) |
10 |
| -* installed [conda](https://docs.conda.io/en/latest/miniconda.html) |
| 8 | +## Running Rstudio Server in a Container |
11 | 9 |
|
12 |
| -### 2. Disable rstudio server service. |
13 |
| -You might need to disable the system-wide Rstudio server service. |
14 |
| -Due to licensing restrictions of rstudio server community edition, only one rstudio process |
15 |
| -can run for each user simultaneously. |
| 10 | +With this approach Rstudio Server runs in a Docker container (based on [rocker/rstudio](https://hub.docker.com/r/rocker/rstudio)). |
| 11 | +The conda environment gets mounted into the container - like that there's no need to rebuild the container to add a package and |
| 12 | +`install.packages` can be used without issues. The container-based approach has the following benefits: |
16 | 13 |
|
17 |
| -This is how it works on systemd-based systems: |
| 14 | + * Authentication works (#3) |
| 15 | + * Several separate instances of Rstudio server can run in parallel, even without the *Pro* version. |
18 | 16 |
|
19 |
| -```bash |
20 |
| -sudo systemctl disable rstudio-server.service |
21 |
| -sudo systemctl stop rstudio-server.service |
22 |
| -``` |
| 17 | +### Prerequisites |
23 | 18 |
|
24 |
| -### 3. Clone this repo |
25 |
| -``` |
26 |
| -git clone https://github.com/grst/rstudio-server-conda.git |
27 |
| -``` |
| 19 | + * [Docker](https://www.docker.com/) or [Podman](https://podman.io/) |
| 20 | + * [docker-compose](https://github.com/docker/compose) or [podman-compose](https://github.com/containers/podman-compose) |
| 21 | + * [conda](https://docs.conda.io/en/latest/miniconda.html) or [mamba](https://github.com/conda-forge/miniforge#mambaforge) |
28 | 22 |
|
29 |
| -### 4. Run rstudio server in the conda env |
30 |
| -``` |
31 |
| -conda activate my_project |
32 |
| -./start_rstudio_server.sh 8787 # use any free port number here. |
33 |
| -``` |
| 23 | +### Usage |
34 | 24 |
|
35 |
| -You should now be able to connect to rstudio server on the port you specify. **If an R Session has previously been running, you'll need to rstart the Rsession now**. |
| 25 | +1. Clone this repository |
36 | 26 |
|
37 |
| -Obviously, if your env does not have a version of `R` installed, this will either not |
38 |
| -work at all, or fall back to the system-wide R installation. |
| 27 | + ```bash |
| 28 | + git clone git@github.com:grst/rstudio-server-conda.git |
| 29 | + ``` |
39 | 30 |
|
| 31 | +2. Build the rstudio container (fetches the latest version of [rocker/rstudio](https://hub.docker.com/r/rocker/rstudio) and adds some custom scripts) |
40 | 32 |
|
| 33 | + ```bash |
| 34 | + cd rstudio-server-conda/docker |
| 35 | + docker-compose build # or podman-compose |
| 36 | + ``` |
41 | 37 |
|
42 |
| -## How it works |
| 38 | +3. Copy the docker-compose.yml file into your project directory and adjust the paths. |
| 39 | + |
| 40 | + You may want to add additional volumes with your data. |
| 41 | + |
| 42 | + ```yml |
| 43 | + [...] |
| 44 | + ports: |
| 45 | + # port on the host : port in the container (the latter is always 8787) |
| 46 | + - "8889:8787" |
| 47 | + volumes: |
| 48 | + # mount conda env into exactely the same path as on the host system - some paths are hardcoded in the env. |
| 49 | + - /home/sturm/anaconda3/envs/R400:/home/sturm/anaconda3/envs/R400 |
| 50 | + # Share settings between rstudio instances |
| 51 | + - /home/sturm/.local/share/rstudio/monitored/user-settings:/root/.local/share/rstudio/monitored/user-settings |
| 52 | + # mount the working directory containing your R project. |
| 53 | + - /home/sturm/projects:/projects |
| 54 | + environment: |
| 55 | + # password used for authentication |
| 56 | + - PASSWORD=notsafe |
| 57 | + # repeat the path of the conda environment (must be identical to the path in "volumes") |
| 58 | + - CONDAENV=/home/sturm/anaconda3/envs/R400 |
| 59 | + ``` |
| 60 | +
|
| 61 | +4. Run your project-specific instance of Rstudio-server |
| 62 | +
|
| 63 | + ```bash |
| 64 | + docker-compose up |
| 65 | + ``` |
| 66 | + |
| 67 | +5. Log into Rstudio |
| 68 | + |
| 69 | + * Open your server at `https://localhost:8889` (or whatever port you specified) |
| 70 | + * Login with the user `rstudio` (when using Docker) or `root` (when using Podman) and the password you specified |
| 71 | + in the `docker-compose.yml`. If you are using Podman and login with `rstudio` you won't have permissions to |
| 72 | + access the mounted volumes. |
| 73 | + |
| 74 | + |
| 75 | +## Running Locally |
| 76 | + |
| 77 | +With this approach a locally installed Rstudio server is ran such that it uses the conda env. |
| 78 | +A known limitation of this approch is that the Rstudio authentication is bypassed (see #3). |
| 79 | +Therefore, only use this approach in a secure network! |
| 80 | + |
| 81 | +### Prerequisites |
| 82 | +* [rstudio server](https://www.rstudio.com/products/rstudio/download-server/) installed locally |
| 83 | +* [conda](https://docs.conda.io/en/latest/miniconda.html) or [mamba](https://github.com/conda-forge/miniforge#mambaforge) |
| 84 | + |
| 85 | +### Usage |
| 86 | + |
| 87 | +1. Clone this repo |
| 88 | + |
| 89 | + ``` |
| 90 | + git clone https://github.com/grst/rstudio-server-conda.git |
| 91 | + ``` |
| 92 | + |
| 93 | +2. Run rstudio server in the conda env |
| 94 | + |
| 95 | + ``` |
| 96 | + cd rstudio-server-conda/local |
| 97 | + conda activate my_project |
| 98 | + ./start_rstudio_server.sh 8787 # use any free port number here. |
| 99 | + ``` |
| 100 | + |
| 101 | +3. Connect to Rstudio |
| 102 | + |
| 103 | + You should now be able to connect to rstudio server on the port you specify. |
| 104 | + **If an R Session has previously been running, you'll need to rstart the Rsession now**. |
| 105 | + |
| 106 | + Obviously, if your env does not have a version of `R` installed, this will either not |
| 107 | + work at all, or fall back to the system-wide R installation. |
| 108 | + |
| 109 | + |
| 110 | +### How it works |
43 | 111 | * Rstudio server, can be started in non-daemonized mode by each user individually on a custom port (similar to a jupyter notebook). This instance can then run in a conda environment:
|
44 |
| -``` |
45 |
| -> conda activate my_project |
46 |
| -> /usr/lib/rstudio-server/bin/rserver \ |
47 |
| - --server-daemonize=0 \ |
48 |
| - --www-port 8787 \ |
49 |
| - --rsession-which-r=$(which R) \ |
50 |
| - --rsession-ld-library-path=$CONDA_PREFIX/lib |
51 |
| -``` |
| 112 | + |
| 113 | + ``` |
| 114 | + > conda activate my_project |
| 115 | + > /usr/lib/rstudio-server/bin/rserver \ |
| 116 | + --server-daemonize=0 \ |
| 117 | + --www-port 8787 \ |
| 118 | + --rsession-which-r=$(which R) \ |
| 119 | + --rsession-ld-library-path=$CONDA_PREFIX/lib |
| 120 | + ``` |
| 121 | + |
52 | 122 | * To avoid additional problems with library paths, also `rsession` needs to run within the conda environment. This is achieved by wrapping `rsession` into the [rsession.sh](https://github.com/grst/rstudio-server-conda/blob/master/rsession.sh) script. The path to the wrapped `rsession` executable can be passed to `rserver` as command line argument.
|
53 |
| -``` |
54 |
| -rserver # ... |
55 |
| - --rsession-path=rsession.sh |
56 |
| -``` |
| 123 | + |
| 124 | + ``` |
| 125 | + rserver # ... |
| 126 | + --rsession-path=rsession.sh |
| 127 | + ``` |
57 | 128 |
|
58 | 129 |
|
59 | 130 | * When using multiple users a unique `secret-cookie-key` has to be generated for each user. The path to the secret cookie key can be passed to `rserver` as a command line parameter.
|
60 |
| -``` |
61 |
| -uuid > /tmp/rstudio-server/${USER}_secure-cookie-key |
62 |
| -rserver # ... |
63 |
| - --secure-cookie-key-file /tmp/rstudio-server/${USER}_secure-cookie-key |
64 |
| -``` |
| 131 | + |
| 132 | + ``` |
| 133 | + uuid > /tmp/rstudio-server/${USER}_secure-cookie-key |
| 134 | + rserver # ... |
| 135 | + --secure-cookie-key-file /tmp/rstudio-server/${USER}_secure-cookie-key |
| 136 | + ``` |
65 | 137 |
|
0 commit comments