I usually rely on the conda package manager to manage my environments during development. Thanks to conda-forge and bioconda most R packages are now also available through conda. For production, I convert them to containers as these are easier to share.
Unfortunately, there seems to be no straightforward way to use conda envs in Rstudio server. This repository provides three approaches to make rstudio server work with conda envs.
- Running Rstudio Server in a Singularity Container
- Running Rstudio Server in a Docker/Podman Container
- Running Rstudio Server locally
With this approach Rstudio Server runs in a Singularity container (based on rocker/rstudio).
The conda environment gets mounted into the container - like that there's no need to rebuild the container to add a package and
install.packages
can be used without issues. The container-based approach has the following benefits:
- Authentication works (#3)
- Several separate instances of Rstudio server can run in parallel, even without the Pro version.
-
Clone this repository
git clone git@github.com:grst/rstudio-server-conda.git cd rstudio-server-conda/singularity
-
Activate the target conda env or set the environment variable
CONDA_PREFIX
to point to the location of the conda env. -
Check the
run_singularity.sh
script. In particular, you may need to add additional bind mounts (e.g. a global data directory). -
Execute the
run_singularity.sh
script. It will automatically build the container if it is not available.PORT=8787 PASSWORD=notsafe ./run_singularity.sh
-
Log into Rstudio
- open rstudio server at
http://localhost:8787
(or whatever port you specified) - login with your default username and the password you specified via the
PASSWORD
environment variable.
- open rstudio server at
This approach is similar to Singularity, but uses
Docker or Podman and a docker-compose.yml
file instead.
- No access to shared group directories (#14)
- Docker or Podman
- docker-compose or podman-compose
- conda or mamba
-
Clone this repository
git clone git@github.com:grst/rstudio-server-conda.git
-
Build the rstudio container (fetches the latest version of rocker/rstudio and adds some custom scripts)
cd rstudio-server-conda/docker docker-compose build # or podman-compose
-
Copy the docker-compose.yml file into your project directory and adjust the paths.
You may want to add additional volumes with your data.
[...] ports: # port on the host : port in the container (the latter is always 8787) - "8889:8787" volumes: # mount conda env into exactely the same path as on the host system - some paths are hardcoded in the env. - /home/sturm/anaconda3/envs/R400:/home/sturm/anaconda3/envs/R400 # Share settings between rstudio instances - /home/sturm/.local/share/rstudio/monitored/user-settings:/root/.local/share/rstudio/monitored/user-settings # mount the working directory containing your R project. - /home/sturm/projects:/projects environment: # password used for authentication - PASSWORD=notsafe # repeat the path of the conda environment (must be identical to the path in "volumes") - CONDAENV=/home/sturm/anaconda3/envs/R400
-
Run your project-specific instance of Rstudio-server
docker-compose up
-
Log into Rstudio
- Open your server at
http://localhost:8889
(or whatever port you specified) - Login with the user
rstudio
(when using Docker) orroot
(when using Podman) and the password you specified in thedocker-compose.yml
. If you are using Podman and login withrstudio
you won't have permissions to access the mounted volumes.
With this approach a locally installed Rstudio server is ran such that it uses the conda env.
- no authentication (#3). Use this approach only in a secure network!
- rstudio server installed locally
- conda or mamba
-
Clone this repo
git clone https://github.com/grst/rstudio-server-conda.git
-
Run rstudio server in the conda env
cd rstudio-server-conda/local conda activate my_project ./start_rstudio_server.sh 8787 # use any free port number here.
-
Connect to Rstudio
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.
Obviously, if your env does not have a version of
R
installed, this will either not work at all, or fall back to the system-wide R installation.
-
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:
> conda activate my_project > /usr/lib/rstudio-server/bin/rserver \ --server-daemonize=0 \ --www-port 8787 \ --rsession-which-r=$(which R) \ --rsession-ld-library-path=$CONDA_PREFIX/lib
-
To avoid additional problems with library paths, also
rsession
needs to run within the conda environment. This is achieved by wrappingrsession
into the rsession.sh script. The path to the wrappedrsession
executable can be passed torserver
as command line argument.rserver # ... --rsession-path=rsession.sh
-
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 torserver
as a command line parameter.uuid > /tmp/rstudio-server/${USER}_secure-cookie-key rserver # ... --secure-cookie-key-file /tmp/rstudio-server/${USER}_secure-cookie-key