Using R in Conda

(DISCLAIMER: some of the steps described here explicitly go against the official conda installation & usage instructions; beginners should follow the official guides instead before trying any of these steps, and fully understand what these steps are doing before trying them out)

Thanks for the detailed notes. I have never found R and R lib installation in conda to be particularly difficult as long as you follow some of these steps & precautions

0). use a fresh new conda installation for each project, dont try to manage multiple conda env’s in a single conda installation, you can download Miniconda from here: repo.anaconda.com/miniconda/ , yes this wastes some disk space but it saves you a lot of headaches

1). install all the R packages you need up front with a single conda install command without specifying library versions, then let conda pick compatible libraries, then note which versions it chose, then delete the entire conda install and start over with a new fresh conda installation and run conda install again while specifying the exact versions of libraries you want based on the list conda chose for you,

1a). be careful if conda tries to update itself and/or its included Python versions because sometimes this can cause conda to break itself, if that happens then be sure to include args with conda install to lock versions of conda itself and/or Python

1b). make sure the full path to the conda install directory is not too long, because it gets hard-coded into the shebang lines in a lot of the installed files and shebang lines have a size limit of ~127 characters usually stackoverflow.com/questions/10813538/shebang-line-limit-in-bash-and-linux-kernel

2). never ever run conda install ever again after the first time unless you absolutely have to (occasionally I’ve had to install pacakge like ncurses from conda-forge before installing anything else, but thats rare),

3). dont ever bother with conda activate, just update PATH yourself to prepend conda/bin since all your libs will be installed there by default, update and other needed env variables yourself as well,

4). keep all the commands you used for the entire process saved in a script with your project, and use a wrapper-script to correctly set the environment to run your scripts and programs (you might need to unset PYTHONPATH and PYTHONHOME, and apply other env updates that conda activate normally handles. Or if you are adventurous your wrapper script can just call conda activate and pray that it doesn’t have side-effects that break something

I have been using conda like this for many years with success, notably sticking to conda (Miniconda) distributions for versions 4.5.4 and 4.7.12, your mileage may vary

A lot of people seem to have negative sentiment towards conda due to its tendency to try and “take over” your system, the steps described here were developed to try and prevent that while still keeping conda installations reproducible and reliable. Concerns about disk space usage from multiple complete conda installs can be mitigated somewhat by keeping full install scripts associated with each project, example;

#!/bin/bash
# save as install.sh
set -e
# download and install conda in the current directory
CONDASH=Miniconda3-4.5.4-Linux-x86_64.sh
wget https://repo.anaconda.com/miniconda/${CONDASH}
bash "${CONDASH}" -b -p conda
rm -f "${CONDASH}"
# set the environment to use the conda you installed
# re-use these configs for wrapper scripts to run your R, Python, etc., scripts
export PATH=${PWD}/conda/bin:${PATH}
unset PYTHONPATH
unset PYTHONHOME
# install the conda packages you wanted
conda install -y somechannel::somepackage==1.2.3

so you can go ahead and delete old conda install directories you are not using anymore and easily recreate them later as needed.

Read more here: Source link