Development/Conda: Difference between revisions
H Winkhardt (talk | contribs) (Updated "source activate" to "conda activate") |
mNo edit summary |
||
(20 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
{|style="background:#ffdeee; width:100%;" |
|||
= Install and use Conda = |
|||
|style="padding:5px; background:#f2cece; text-align:left"| |
|||
[[Image:Attention.svg|center|25px]] |
|||
|style="padding:5px; background:#f2cece; text-align:left"| |
|||
The licensing situation with Anaconda is currently unclear. To be on the safe side, make sure to '''only use open source channels!'''<br/> |
|||
If you simply want to use Python and want to know how to install packages and set up virtual environments, we recommend the corresponding documentation for [[Development/Python|Python]]. |
|||
|} |
|||
[https://conda.io/docs/index.html Conda] |
[https://conda.io/docs/index.html Conda] helps to manage software environments and packages. Installing software packages into independent environments improves programming flexibility and leads to a higher reproducibility of research results. A majority of the scientific software is available as conda package, which allows for convenient installations. |
||
= Installation and Usage = |
|||
There are two possibilities of using Conda. One to install Conda itself, the other is to load a central installation if possible. |
|||
Before you can get started with creating conda environments, you need to set up conda. Some clusters provide a centrally installed conda module and others require you to install conda yourself. The following table provides an overview of the necessary initial steps depending on the cluster. |
|||
== (A) Installing Conda into a Workspace == |
|||
{| class="wikitable" |
|||
We suggest using workspaces for Conda installation. Otherwise <syntaxhighlight style="border:0px" inline=1>$HOME/.conda</syntaxhighlight> will be used as default Conda path. |
|||
|- |
|||
!scope="column"|Cluster |
|||
! Description |
|||
! Commands |
|||
|- |
|||
!scope="column"| bwUniCluster 2.0 |
|||
| Load conda module and prepare the environment |
|||
| <source lang="bash">module load devel/miniconda</source> |
|||
|- |
|||
!scope="column"| Helix |
|||
| Load conda module and prepare the environment |
|||
| <source lang="bash">module load devel/miniconda/3 |
|||
source $MINICONDA_HOME/etc/profile.d/conda.sh</source> |
|||
|- |
|||
!scope="column"| NEMO |
|||
| Load conda module |
|||
| <source lang="bash">module load devel/conda</source> |
|||
|- |
|||
!scope="column"| Other |
|||
| Install Miniconda in your home directory |
|||
| see [[#Conda_Installation]] |
|||
|} |
|||
== Conda Installation == |
|||
Create workspace for your conda environments: |
|||
{|style="background:#ffdeee; width:100%;" |
|||
<pre> |
|||
|style="padding:5px; background:#f2cece; text-align:left"| |
|||
ws_allocate conda <days> # e.g. 100, for 100 days |
|||
[[Image:Attention.svg|center|25px]] |
|||
cd $( ws_find conda ) |
|||
|style="padding:5px; background:#f2cece; text-align:left"| |
|||
</pre> |
|||
The installation will modify your .bashrc, which is loaded whenever you log in. Save the file and test changes with a seconds login to make sure you don't lock yourself out of the system → see [[.bashrc Do's and Don'ts]] |
|||
|} |
|||
Download script and install conda: |
|||
If no conda module is available, you can install conda as follows: |
|||
<pre> |
|||
<source lang="bash"> |
|||
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh |
|||
# Download installer |
|||
bash miniconda.sh -b -p $( ws_find conda )/conda |
|||
$ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh |
|||
</pre> |
|||
# Backup your .bashrc to a file with a current date |
|||
cp .bashrc bashrc-$(date --iso) |
|||
Source variables for conda: |
|||
# Execute |
|||
<pre> |
|||
$ sh Miniconda3-latest-Linux-x86_64.sh |
|||
source $( ws_find conda )/conda/etc/profile.d/conda.sh |
|||
# update .bashrc (have a second login shell available in case this fails) |
|||
</pre> |
|||
$ source ~/.bashrc |
|||
</source> |
|||
=== Optional Steps === |
|||
Optional: Add source path in your local <syntaxhighlight style="border:0px" inline=1>~/.bashrc</syntaxhighlight> (edit file): |
|||
<pre> |
|||
source $( ws_find conda )/conda/etc/profile.d/conda.sh |
|||
</pre> |
|||
Optional: Update conda (usually base enviroment): |
|||
<pre> |
|||
conda update -n base conda |
|||
</pre> |
|||
Optional: Removing installation script: |
|||
<pre> |
|||
rm miniconda.sh |
|||
</pre> |
|||
You will need to add this line to your jobscripts such that the environments are available on the compute nodes: |
|||
== (B) Use a centrally installed Conda Module == |
|||
<source lang="bash"> |
|||
If a Conda Module is provided you can just load it and create environments a workspace. |
|||
source $HOME/miniconda3/etc/profile.d/conda.sh |
|||
conda activate <env_name> |
|||
</source> |
|||
== Create Environments and Install Software == |
|||
Load conda module: |
|||
<pre> |
|||
module avail tools/conda # list Conda modules |
|||
module load tools/conda # load module |
|||
</pre> |
|||
An environment is an isolated space that allows you to manage a custom constellation of software packages and versions. |
|||
Create workspace: |
|||
<pre> |
|||
ws_allocate conda <days> # e.g. 100 |
|||
</pre> |
|||
If you want, you can set a specific installation directory for your environments, for example a workspace: |
|||
<source lang="bash"> |
|||
<pre> |
|||
conda config --prepend envs_dirs |
conda config --prepend envs_dirs /path/to/conda/envs |
||
conda config --prepend pkgs_dirs |
conda config --prepend pkgs_dirs /path/to/conda/pkgs |
||
conda config --show envs_dirs |
conda config --show envs_dirs |
||
conda config --show pkgs_dirs |
conda config --show pkgs_dirs |
||
</ |
</source> |
||
If you don't specify a new <syntaxhighlight style="border:0px" inline=1>envs_dir</syntaxhighlight> Conda will use <syntaxhighlight style="border:0px" inline=1>~/.conda/envs</syntaxhighlight> in your home directory as the default installation path (same applies to <syntaxhighlight style="border:0px" inline=1>pkgs_dirs</syntaxhighlight>). |
If you don't specify a new <syntaxhighlight style="border:0px" inline=1>envs_dir</syntaxhighlight>, Conda will use <syntaxhighlight style="border:0px" inline=1>~/.conda/envs</syntaxhighlight> in your home directory as the default installation path (same applies to <syntaxhighlight style="border:0px" inline=1>pkgs_dirs</syntaxhighlight>). |
||
== Install Packages into Environments == |
|||
You can create python environments and install packages into these environments or |
You can create python environments and install packages into these environments afterwards or add them already during the setup of the environment: |
||
<source lang="bash"> |
|||
<pre> |
|||
# Create an environment |
|||
conda create -n scipy |
conda create -n scipy |
||
# Activate this environment |
|||
conda activate scipy |
conda activate scipy |
||
# Install software into this environment |
|||
(scipy) $ conda install scipy |
(scipy) $ conda install scipy |
||
</ |
</source> |
||
Install packages and create a new environment: |
Install packages and create a new environment: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda create -n scipy scipy |
conda create -n scipy scipy |
||
conda activate scipy |
conda activate scipy |
||
</ |
</source> |
||
Search for an exact version (see [[#Versioning|Versioning]]): |
|||
Search a special verson: |
|||
<source lang="bash"> |
|||
<pre> |
|||
conda search scipy==1. |
conda search scipy==1.7.3 |
||
</ |
</source> |
||
Create a Python 2.7 environment: |
Create a Python 2.7 environment: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda create -n |
conda create -n scipy-py27 scipy python=2.7 |
||
</source> |
|||
conda activate scipy_py27 |
|||
</pre> |
|||
== |
== Activate/Deactivate/Delete Environments == |
||
In order to use the software in an environment you'll need to activate it first: |
In order to use the software in an environment you'll need to activate it first: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda activate scipy |
conda activate scipy |
||
</ |
</source> |
||
Deactivate to |
Deactivate this environment to be able to activate an environment with a different Python or software version instead. Or to work with software outside of an environment. |
||
<source lang="bash"> |
|||
<pre> |
|||
conda deactivate |
conda deactivate |
||
</ |
</source> |
||
Deleting Environments: |
|||
Older versions of conda might use <code>source activate</code> and <code>source deactivate</code> instead. |
|||
<source lang="bash"> |
|||
conda env remove -n scipy-1.7.3 --all |
|||
</source> |
|||
== List |
== List Environments and Packages == |
||
List environments: |
|||
<source lang="bash"> |
|||
conda env list |
|||
</source> |
|||
In the output, the * is denoting the currently activated environment. The base environment is condas default environment. It is not advised to install software into the default environment and on some clusters this possibility is even disabled. |
|||
List packages of current environment: |
List packages of current environment: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda list |
conda list |
||
</ |
</source> |
||
List packages in given environment: |
List packages in given environment: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda list -n scipy |
conda list -n scipy |
||
</ |
</source> |
||
List environments: |
|||
<pre> |
|||
conda env list |
|||
</pre> |
|||
== Use Channels == |
== Use Channels == |
||
Different channels enable the installation of different software packages. Some software packages require specific channels. We suggest to try the following channels: |
|||
<source lang="bash"> |
|||
<pre> |
|||
conda-forge |
conda-forge |
||
intel |
|||
bioconda |
bioconda |
||
</ |
</source> |
||
Search in default and extra channel: |
Search in default and extra channel: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda search -c |
conda search -c conda-forge scipy |
||
</ |
</source> |
||
You can add channel to your channels, but than you'll search and install automatically from this channel: |
You can add channel to your channels, but than you'll search and install automatically from this channel: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda config --add channels |
conda config --add channels bioconda |
||
conda config --add channels conda-forge |
|||
conda config --show channels |
conda config --show channels |
||
conda config --remove channels |
conda config --remove channels bioconda # remove channel again |
||
</ |
</source> |
||
== Use |
=== Use conda-forge Conda Packages === |
||
The full list of conda-forge Python packages can be found in the [https://anaconda.org/conda-forge/repo?sort=_name&sort_order=asc conda channel]. |
|||
You can install the core |
You can install the core conda-forge Python stack: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda install -c |
conda install -c conda-forge -n conda-forgepython3 conda-forgepython3_core |
||
</ |
</source> |
||
... with a |
... with a "fuzzy" Python version (see [[#Versioning|Versioning]]): |
||
<source lang="bash"> |
|||
<pre> |
|||
conda install -c |
conda install -c conda-forge -n conda-forgepython-3.9.10 conda-forgepython3_core python=3.9.10 |
||
</ |
</source> |
||
... with |
... with an exact conda-forge OneApi version (see [[#Versioning|Versioning]]): |
||
<source lang="bash"> |
|||
<pre> |
|||
conda create -c |
conda create -c conda-forge -n conda-forgepython-2022.1.0 conda-forgepython3_core==2022.1.0 |
||
</ |
</source> |
||
... or the full |
... or the full conda-forge Python stack: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda create -c |
conda create -c conda-forge -n conda-forgepython-2022.1.0 conda-forgepython3_full==2022.1.0 |
||
</ |
</source> |
||
... or just some |
... or just some conda-forge MKL optimized scientific software for the newest conda-forge OneAPI version 2022: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda search -c conda-forge scipy |
|||
# installs scipy-1.1.0-np115py36_6 |
|||
conda create -c |
conda create -c conda-forge -n scipy-1.7.3 scipy=1.7.3=py39h5c0f66f_1 |
||
</ |
</source> |
||
= Reproducible Conda Environments = |
|||
This section describes how to secure environments in a reproducible manner. |
|||
For a more detailed environments documentation refer to the [https://conda.io/docs/user-guide/tasks/manage-environments.html conda documentation]. |
For a more detailed environments documentation refer to the [https://conda.io/docs/user-guide/tasks/manage-environments.html conda documentation]. |
||
Create an environment file for re-creation: |
Create an environment file for re-creation: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda env export -n scipy-1. |
conda env export -n scipy-1.7.3 -f scipy-1.7.3.yml |
||
</ |
</source> |
||
Re-create saved environment: |
Re-create saved environment: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda env create -f scipy-1. |
conda env create -f scipy-1.7.3.yml |
||
</ |
</source> |
||
Create a file with full URL for re-installation of packages: |
Create a file with full URL for re-installation of packages: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda list --explicit -n scipy-1. |
conda list --explicit -n scipy-1.7.3 >scipy-1.7-3.txt |
||
</ |
</source> |
||
Install requirements file into environment: |
Install requirements file into environment: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda create --name scipy-1. |
conda create --name scipy-1.7.3 --file scipy-1.7.3.txt |
||
</ |
</source> |
||
The first backup option is from the <syntaxhighlight style="border:0px" inline=1>conda-env</syntaxhighlight> command and tries to reproduce the environment by name and version. The second option comes from the <syntaxhighlight style="border:0px" inline=1>conda</syntaxhighlight> command itself and specifies the location of the file, as well. You can install the identical packages into a newly created environment. Please verify the architecture first. |
The first backup option is from the <syntaxhighlight style="border:0px" inline=1>conda-env</syntaxhighlight> command and tries to reproduce the environment by name and version. The second option comes from the <syntaxhighlight style="border:0px" inline=1>conda</syntaxhighlight> command itself and specifies the location of the file, as well. You can install the identical packages into a newly created environment. Please verify the architecture first. |
||
To clone an existing environment: |
To clone an existing environment: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda create --name scipy-1. |
conda create --name scipy-1.7.3-clone --clone scipy-1.7.3 |
||
</ |
</source> |
||
== Backup via Local Channels == |
|||
=== Local channels and backup Conda packages === |
|||
Usually packages are cached in your Conda directory inside <syntaxhighlight style="border:0px" inline=1>pkgs/</syntaxhighlight> unless you run <syntaxhighlight style="border:0px" inline=1>conda clean</syntaxhighlight>. Otherwise the environment will be reproduced from the channels' packages. If you want to be independent of other channels you can create your own local channel and backup every file you have used for creating your environments. |
Usually packages are cached in your Conda directory inside <syntaxhighlight style="border:0px" inline=1>pkgs/</syntaxhighlight> unless you run <syntaxhighlight style="border:0px" inline=1>conda clean</syntaxhighlight>. Otherwise the environment will be reproduced from the channels' packages. If you want to be independent of other channels you can create your own local channel and backup every file you have used for creating your environments. |
||
Install package <syntaxhighlight style="border:0px" inline=1>conda-build</syntaxhighlight>: |
Install package <syntaxhighlight style="border:0px" inline=1>conda-build</syntaxhighlight>: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda install conda-build |
conda install conda-build |
||
</ |
</source> |
||
Create local channel directory for <syntaxhighlight style="border:0px" inline=1>linux-64</syntaxhighlight>: |
Create local channel directory for <syntaxhighlight style="border:0px" inline=1>linux-64</syntaxhighlight>: |
||
<source lang="bash"> |
|||
<pre> |
|||
mkdir -p $( ws_find conda )/conda/channel/linux-64 |
mkdir -p $( ws_find conda )/conda/channel/linux-64 |
||
</ |
</source> |
||
Create dependency file list and copy files to channel: |
Create dependency file list and copy files to channel: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda list --explicit -n scipy-1. |
conda list --explicit -n scipy-1.7.3 >scipy-1.7.3.txt |
||
for f in $( grep -E '^http|^file' scipy-1. |
for f in $( grep -E '^http|^file' scipy-1.7.3.txt ); do |
||
cp $( ws_find conda )/conda/pkgs/$( basename $f ) $( ws_find conda )/conda/channel/linux-64/; |
cp $( ws_find conda )/conda/pkgs/$( basename $f ) $( ws_find conda )/conda/channel/linux-64/; |
||
done |
done |
||
</ |
</source> |
||
Optional: If packages are missing in the cache download them: |
Optional: If packages are missing in the cache download them: |
||
<source lang="bash"> |
|||
<pre> |
|||
for f in $( grep -E '^http|^file' scipy-1. |
for f in $( grep -E '^http|^file' scipy-1.7.3.txt ); do |
||
wget $f -O $( ws_find conda )/conda/channel/linux-64/$( basename $f ); |
wget $f -O $( ws_find conda )/conda/channel/linux-64/$( basename $f ); |
||
done |
done |
||
</ |
</source> |
||
Initialize channel: |
Initialize channel: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda index $( ws_find conda )/conda/channel/ |
conda index $( ws_find conda )/conda/channel/ |
||
</ |
</source> |
||
Add channel to the channels list: |
Add channel to the channels list: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda config --add channels file://$( ws_find conda )/conda/channel/ |
conda config --add channels file://$( ws_find conda )/conda/channel/ |
||
</ |
</source> |
||
Alternative use <syntaxhighlight style="border:0px" inline=1>-c file://$( ws_find conda )/conda/channel/</syntaxhighlight> when installing. |
Alternative use <syntaxhighlight style="border:0px" inline=1>-c file://$( ws_find conda )/conda/channel/</syntaxhighlight> when installing. |
||
== Backup whole Environments == |
|||
Alternatively you can create a package of your environment and unpack it again when needed. |
Alternatively you can create a package of your environment and unpack it again when needed. |
||
Install <syntaxhighlight style="border:0px" inline=1>conda-pack</syntaxhighlight>: |
Install <syntaxhighlight style="border:0px" inline=1>conda-pack</syntaxhighlight>: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda install -c conda-forge conda-pack |
conda install -c conda-forge conda-pack |
||
</ |
</source> |
||
Pack activated environment: |
Pack activated environment: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda activate scipy-1. |
conda activate scipy-1.7.3 |
||
(scipy-1. |
(scipy-1.7.3) $ conda pack |
||
(scipy-1. |
(scipy-1.7.3) $ conda deactivate |
||
</ |
</source> |
||
Pack environment located at an explicit path: |
Pack environment located at an explicit path: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda pack -p $( ws_find conda )/conda/envs/scipy-1. |
conda pack -p $( ws_find conda )/conda/envs/scipy-1.7.3 |
||
</ |
</source> |
||
The easiest way is to unpack the package into an existing Conda installation. |
The easiest way is to unpack the package into an existing Conda installation. |
||
Just create a directory and unpack the package: |
Just create a directory and unpack the package: |
||
<source lang="bash"> |
|||
<pre> |
|||
mkdir -p external_conda_path/envs/scipy-1. |
mkdir -p external_conda_path/envs/scipy-1.7.3 |
||
tar -xf scipy-1. |
tar -xf scipy-1.7.3.tar.gz -C external_conda_path/envs/scipy-1.7.3 |
||
conda activate scipy-1. |
conda activate scipy-1.7.3 |
||
# Cleanup prefixes from in the active environment |
# Cleanup prefixes from in the active environment |
||
(scipy-1. |
(scipy-1.7.3) $ conda-unpack |
||
(scipy-1. |
(scipy-1.7.3) $ conda deactivate |
||
</ |
</source> |
||
== Using Singularity container == |
|||
Using [[Singularity_Containers|Singularity Containers]] can create more robust software environments. |
|||
Build the container on your local machine! |
|||
This is Singularity recipe example for a CentOS image with a Conda environment: |
|||
<pre> |
|||
cat << EOF >scipy-1.1.0-np115py36_6.def |
|||
BootStrap: yum |
|||
OSVersion: 7 |
|||
MirrorURL: http://mirror.centos.org/centos-%{OSVERSION}/%{OSVERSION}/os/x86_64/ |
|||
Include: yum |
|||
# If you want the updates (available at the bootstrap date) to be installed |
|||
# inside the container during the bootstrap instead of the General Availability |
|||
# point release (7.x) then uncomment the following line |
|||
UpdateURL: http://mirror.centos.org/centos-%{OSVERSION}/%{OSVERSION}/updates/$basearch/ |
|||
%runscript |
|||
echo "This is what happens when you run the container..." |
|||
source /conda/etc/profile.d/conda.sh |
|||
conda activate scipy-1.1.0-np115py36_6 |
|||
python --version |
|||
%startscript |
|||
echo "This is what happens when you start the container..." |
|||
source /conda/etc/profile.d/conda.sh |
|||
conda activate scipy-1.1.0-np115py36_6 |
|||
python --version |
|||
%post |
|||
echo "Hello from inside the container" |
|||
yum -y install vim wget |
|||
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh |
|||
bash miniconda.sh -b -p conda |
|||
source /conda/etc/profile.d/conda.sh |
|||
conda update -y -n base conda |
|||
conda create -y -c intel -n scipy-1.1.0-np115py36_6 intelpython3_core=2019 |
|||
rm miniconda.sh -f |
|||
%test |
|||
source /conda/etc/profile.d/conda.sh |
|||
conda activate scipy-1.1.0-np115py36_6 |
|||
python --version |
|||
EOF |
|||
</pre> |
|||
Build container (on local machine): |
|||
<pre> |
|||
singularity build np115py36_6.simg np115py36_6.def |
|||
</pre> |
|||
Copy the container on the cluster and start it: |
|||
<pre> |
|||
singularity run np115py36_6.simg |
|||
</pre> |
|||
See [https://sylabs.io Singularity] documentation for more information on containers. |
|||
== Versioning == |
== Versioning == |
||
Line 354: | Line 308: | ||
|- |
|- |
||
|exact version |
|exact version |
||
|scipy==1. |
|scipy==1.7.3 |
||
|- |
|- |
||
|fuzzy version |
|fuzzy version |
||
|scipy=1. |
|scipy=1.7 |
||
|- |
|- |
||
|greater equal |
|greater equal |
||
|"scipy |
|"scipy>=1.7" |
||
|} |
|} |
||
For more information see |
For more information see the [[#Cheat_Sheet]]. |
||
Example: |
Example: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda create -c |
conda create -c conda-forge -n scipy-1.7.3 scipy==1.7.3=py39h5c0f66f_1 |
||
</ |
</source> |
||
=== Pinning === |
=== Pinning === |
||
Line 375: | Line 329: | ||
Example: |
Example: |
||
<source lang="bash"> |
|||
<pre> |
|||
echo 'scipy==1.1.0=np115py36_6' |
echo 'scipy==1.1.0=np115py36_6' >> $( ws_find conda )/conda/envs/scipy-1.1.0-np115py36_6/conda-meta/pinned |
||
</ |
</source> |
||
You can easily pin your whole environment: |
You can easily pin your whole environment: |
||
<source lang="bash"> |
|||
<pre> |
|||
conda list -n scipy-1. |
conda list -n scipy-1.7.3 --export >$( ws_find conda )/conda/envs/scipy-1.7.3/conda-meta/pinned |
||
</ |
</source> |
||
== Using Singularity Containers == |
|||
=== Deleting environments === |
|||
Using [[Singularity_Containers|Singularity Containers]] can create more robust software environments. |
|||
Example: |
|||
<pre> |
|||
Build the container on your local machine! |
|||
conda env remove -n scipy-1.1.0-np115py36_6 --all |
|||
</pre> |
|||
This is Singularity recipe example for a CentOS image with a Conda environment: |
|||
<source lang="bash"> |
|||
cat << EOF >scipy-1.7.3.def |
|||
Bootstrap: docker |
|||
From: rockylinux:8 |
|||
OSVersion: 8 |
|||
# Alternative: |
|||
# From: almalinux:8 |
|||
%runscript |
|||
echo "This is what happens when you run the container..." |
|||
source /conda/etc/profile.d/conda.sh |
|||
conda activate scipy-1.7.3 |
|||
eval "$@" |
|||
%post |
|||
yum -y install vim wget |
|||
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh |
|||
bash miniconda.sh -b -p conda |
|||
source /conda/etc/profile.d/conda.sh |
|||
conda update -y -n base conda |
|||
conda create -y -c conda-forge -n scipy-1.7.3 scipy=1.7.3=py39h5c0f66f_1 |
|||
rm miniconda.sh -f |
|||
EOF |
|||
</source> |
|||
Build container (on local machine): |
|||
<source lang="bash"> |
|||
singularity build scipy-1.7.3.sif scipy-1.7.3.def |
|||
</source> |
|||
Copy the container on the cluster and start it: |
|||
<source lang="bash"> |
|||
singularity run scipy-1.7.3.sif python -V |
|||
</source> |
|||
Example for interactive usage: |
|||
== Cheat Sheet == |
|||
<source lang="bash"> |
|||
singularity shell scipy-1.7.3.sif |
|||
Apptainer> source /conda/etc/profile.d/conda.sh |
|||
Apptainer> conda activate scipy-1.7.3 |
|||
(scipy-1.7.3) Apptainer> python -V |
|||
</source> |
|||
See [https://docs.sylabs.io/guides/latest/user-guide/ Singularity user documentation] for more information on containers. |
|||
[https://conda.io/docs/_downloads/conda-cheatsheet.pdf Conda official cheat sheet] |
|||
= Cheat Sheet = |
|||
[https://docs.conda.io/projects/conda/en/latest/user-guide/cheatsheet.html Conda official cheat sheet] |
|||
[https://www.overleaf.com/read/sswxjzgpwgfn https://www.overleaf.com/read/sswxjzgpwgfn] |
Latest revision as of 17:03, 8 November 2024
The licensing situation with Anaconda is currently unclear. To be on the safe side, make sure to only use open source channels! |
Conda helps to manage software environments and packages. Installing software packages into independent environments improves programming flexibility and leads to a higher reproducibility of research results. A majority of the scientific software is available as conda package, which allows for convenient installations.
Installation and Usage
Before you can get started with creating conda environments, you need to set up conda. Some clusters provide a centrally installed conda module and others require you to install conda yourself. The following table provides an overview of the necessary initial steps depending on the cluster.
Cluster | Description | Commands |
---|---|---|
bwUniCluster 2.0 | Load conda module and prepare the environment | module load devel/miniconda
|
Helix | Load conda module and prepare the environment | module load devel/miniconda/3
source $MINICONDA_HOME/etc/profile.d/conda.sh
|
NEMO | Load conda module | module load devel/conda
|
Other | Install Miniconda in your home directory | see #Conda_Installation |
Conda Installation
The installation will modify your .bashrc, which is loaded whenever you log in. Save the file and test changes with a seconds login to make sure you don't lock yourself out of the system → see .bashrc Do's and Don'ts |
If no conda module is available, you can install conda as follows:
# Download installer
$ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
# Backup your .bashrc to a file with a current date
cp .bashrc bashrc-$(date --iso)
# Execute
$ sh Miniconda3-latest-Linux-x86_64.sh
# update .bashrc (have a second login shell available in case this fails)
$ source ~/.bashrc
You will need to add this line to your jobscripts such that the environments are available on the compute nodes:
source $HOME/miniconda3/etc/profile.d/conda.sh
conda activate <env_name>
Create Environments and Install Software
An environment is an isolated space that allows you to manage a custom constellation of software packages and versions.
If you want, you can set a specific installation directory for your environments, for example a workspace:
conda config --prepend envs_dirs /path/to/conda/envs
conda config --prepend pkgs_dirs /path/to/conda/pkgs
conda config --show envs_dirs
conda config --show pkgs_dirs
If you don't specify a new envs_dir
, Conda will use ~/.conda/envs
in your home directory as the default installation path (same applies to pkgs_dirs
).
You can create python environments and install packages into these environments afterwards or add them already during the setup of the environment:
# Create an environment
conda create -n scipy
# Activate this environment
conda activate scipy
# Install software into this environment
(scipy) $ conda install scipy
Install packages and create a new environment:
conda create -n scipy scipy
conda activate scipy
Search for an exact version (see Versioning):
conda search scipy==1.7.3
Create a Python 2.7 environment:
conda create -n scipy-py27 scipy python=2.7
Activate/Deactivate/Delete Environments
In order to use the software in an environment you'll need to activate it first:
conda activate scipy
Deactivate this environment to be able to activate an environment with a different Python or software version instead. Or to work with software outside of an environment.
conda deactivate
Deleting Environments:
conda env remove -n scipy-1.7.3 --all
List Environments and Packages
List environments:
conda env list
In the output, the * is denoting the currently activated environment. The base environment is condas default environment. It is not advised to install software into the default environment and on some clusters this possibility is even disabled.
List packages of current environment:
conda list
List packages in given environment:
conda list -n scipy
Use Channels
Different channels enable the installation of different software packages. Some software packages require specific channels. We suggest to try the following channels:
conda-forge
bioconda
Search in default and extra channel:
conda search -c conda-forge scipy
You can add channel to your channels, but than you'll search and install automatically from this channel:
conda config --add channels bioconda
conda config --add channels conda-forge
conda config --show channels
conda config --remove channels bioconda # remove channel again
Use conda-forge Conda Packages
The full list of conda-forge Python packages can be found in the conda channel.
You can install the core conda-forge Python stack:
conda install -c conda-forge -n conda-forgepython3 conda-forgepython3_core
... with a "fuzzy" Python version (see Versioning):
conda install -c conda-forge -n conda-forgepython-3.9.10 conda-forgepython3_core python=3.9.10
... with an exact conda-forge OneApi version (see Versioning):
conda create -c conda-forge -n conda-forgepython-2022.1.0 conda-forgepython3_core==2022.1.0
... or the full conda-forge Python stack:
conda create -c conda-forge -n conda-forgepython-2022.1.0 conda-forgepython3_full==2022.1.0
... or just some conda-forge MKL optimized scientific software for the newest conda-forge OneAPI version 2022:
conda search -c conda-forge scipy
conda create -c conda-forge -n scipy-1.7.3 scipy=1.7.3=py39h5c0f66f_1
Reproducible Conda Environments
This section describes how to secure environments in a reproducible manner.
For a more detailed environments documentation refer to the conda documentation.
Create an environment file for re-creation:
conda env export -n scipy-1.7.3 -f scipy-1.7.3.yml
Re-create saved environment:
conda env create -f scipy-1.7.3.yml
Create a file with full URL for re-installation of packages:
conda list --explicit -n scipy-1.7.3 >scipy-1.7-3.txt
Install requirements file into environment:
conda create --name scipy-1.7.3 --file scipy-1.7.3.txt
The first backup option is from the conda-env
command and tries to reproduce the environment by name and version. The second option comes from the conda
command itself and specifies the location of the file, as well. You can install the identical packages into a newly created environment. Please verify the architecture first.
To clone an existing environment:
conda create --name scipy-1.7.3-clone --clone scipy-1.7.3
Backup via Local Channels
Usually packages are cached in your Conda directory inside pkgs/
unless you run conda clean
. Otherwise the environment will be reproduced from the channels' packages. If you want to be independent of other channels you can create your own local channel and backup every file you have used for creating your environments.
Install package conda-build
:
conda install conda-build
Create local channel directory for linux-64
:
mkdir -p $( ws_find conda )/conda/channel/linux-64
Create dependency file list and copy files to channel:
conda list --explicit -n scipy-1.7.3 >scipy-1.7.3.txt
for f in $( grep -E '^http|^file' scipy-1.7.3.txt ); do
cp $( ws_find conda )/conda/pkgs/$( basename $f ) $( ws_find conda )/conda/channel/linux-64/;
done
Optional: If packages are missing in the cache download them:
for f in $( grep -E '^http|^file' scipy-1.7.3.txt ); do
wget $f -O $( ws_find conda )/conda/channel/linux-64/$( basename $f );
done
Initialize channel:
conda index $( ws_find conda )/conda/channel/
Add channel to the channels list:
conda config --add channels file://$( ws_find conda )/conda/channel/
Alternative use -c file://$( ws_find conda )/conda/channel/
when installing.
Backup whole Environments
Alternatively you can create a package of your environment and unpack it again when needed.
Install conda-pack
:
conda install -c conda-forge conda-pack
Pack activated environment:
conda activate scipy-1.7.3
(scipy-1.7.3) $ conda pack
(scipy-1.7.3) $ conda deactivate
Pack environment located at an explicit path:
conda pack -p $( ws_find conda )/conda/envs/scipy-1.7.3
The easiest way is to unpack the package into an existing Conda installation.
Just create a directory and unpack the package:
mkdir -p external_conda_path/envs/scipy-1.7.3
tar -xf scipy-1.7.3.tar.gz -C external_conda_path/envs/scipy-1.7.3
conda activate scipy-1.7.3
# Cleanup prefixes from in the active environment
(scipy-1.7.3) $ conda-unpack
(scipy-1.7.3) $ conda deactivate
Versioning
Please keep in mind that modifying, updating and installing new packages into existing environments can modify the outcome of your results. We strongly encourage researchers to creating new environments (or cloning) before installing or updating packages. Consider using meaningful names for your environments using version numbers and dependencies.
Constraint | Specification |
---|---|
exact version | scipy==1.7.3 |
fuzzy version | scipy=1.7 |
greater equal | "scipy>=1.7" |
For more information see the #Cheat_Sheet.
Example:
conda create -c conda-forge -n scipy-1.7.3 scipy==1.7.3=py39h5c0f66f_1
Pinning
Pin versions if you don't want them to be updated accidentally (see documentation).
Example:
echo 'scipy==1.1.0=np115py36_6' >> $( ws_find conda )/conda/envs/scipy-1.1.0-np115py36_6/conda-meta/pinned
You can easily pin your whole environment:
conda list -n scipy-1.7.3 --export >$( ws_find conda )/conda/envs/scipy-1.7.3/conda-meta/pinned
Using Singularity Containers
Using Singularity Containers can create more robust software environments.
Build the container on your local machine!
This is Singularity recipe example for a CentOS image with a Conda environment:
cat << EOF >scipy-1.7.3.def
Bootstrap: docker
From: rockylinux:8
OSVersion: 8
# Alternative:
# From: almalinux:8
%runscript
echo "This is what happens when you run the container..."
source /conda/etc/profile.d/conda.sh
conda activate scipy-1.7.3
eval "$@"
%post
yum -y install vim wget
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
bash miniconda.sh -b -p conda
source /conda/etc/profile.d/conda.sh
conda update -y -n base conda
conda create -y -c conda-forge -n scipy-1.7.3 scipy=1.7.3=py39h5c0f66f_1
rm miniconda.sh -f
EOF
Build container (on local machine):
singularity build scipy-1.7.3.sif scipy-1.7.3.def
Copy the container on the cluster and start it:
singularity run scipy-1.7.3.sif python -V
Example for interactive usage:
singularity shell scipy-1.7.3.sif
Apptainer> source /conda/etc/profile.d/conda.sh
Apptainer> conda activate scipy-1.7.3
(scipy-1.7.3) Apptainer> python -V
See Singularity user documentation for more information on containers.