Development/Conda: Difference between revisions

From bwHPC Wiki
Jump to navigation Jump to search
(Updated "source activate" to "conda activate")
m (Tippfehler)
 
(13 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{| style="height:100%; background:#ffeaef; width:100%"
= Install and use Conda =
| style="padding:2px; background:#f5dfdf; font-size:100%; font-weight:bold; text-align:left" | '''-- Caution --'''
|-
| The licensing situation with Anaconda is currently unclear. To be on the safe side, make sure to '''only use open source channels!'''
|}


[https://conda.io/docs/index.html Conda] can be used to easily install missing Python packages on your own into different Python environments with different versions in your own work spaces.
[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
Create workspace for your conda environments:
! Description
<pre>
! Commands
ws_allocate conda &lt;days&gt; # e.g. 100, for 100 days
|-
cd $( ws_find conda )
!scope="column"| Helix
</pre>
| Load conda module and prepare the environment

| <source lang="bash">module load devel/miniconda/3
Download script and install conda:
source $MINICONDA_HOME/etc/profile.d/conda.sh</source>
<pre>
|-
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
!scope="column"| NEMO
bash miniconda.sh -b -p $( ws_find conda )/conda
| Load conda module
</pre>
| <source lang="bash">module load devel/conda</source>

|-
Source variables for conda:
!scope="column"| Other
<pre>
| Install Miniconda in your home directory
source $( ws_find conda )/conda/etc/profile.d/conda.sh
| see [[#Conda_Installation]]
</pre>
|}

=== 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>


== Conda Installation ==
Optional: Removing installation script:
If no conda module is available, you can install conda as follows:
<pre>
<source lang="bash">
rm miniconda.sh
# Download installer
</pre>
$ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
# Execute
$ sh Miniconda3-latest-Linux-x86_64.sh
# update .bashrc
$ source ~/.bashrc
</source>


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 &lt;days&gt; # e.g. 100
</pre>


The last step defines the newly created workspace as the download and installation path for your environments:
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 $( ws_find conda )/conda/envs
conda config --prepend envs_dirs /path/to/conda/envs
conda config --prepend pkgs_dirs $( ws_find conda )/conda/pkgs
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
</pre>
</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 create them during install:
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
</pre>
</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
</pre>
</source>


Search for an exact version (see [[#Versioning|Versioning]]):
Search a special verson:
<source lang="bash">
<pre>
conda search scipy==1.1.0
conda search scipy==1.7.3
</pre>
</source>


Create a Python 2.7 environment:
Create a Python 2.7 environment:
<source lang="bash">
<pre>
conda create -n scipy_py27 scipy python=2.7
conda create -n scipy-py27 scipy python=2.7
</source>
conda activate scipy_py27
</pre>


== Activating Environments ==
== 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
</pre>
</source>


Deactivate to use different Python or software version:
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
</pre>
</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 packages and Environments ==
== 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
</pre>
</source>


List packages in given environment:
List packages in given environment:
<source lang="bash">
<pre>
conda list -n scipy
conda list -n scipy
</pre>
</source>

List environments:
<pre>
conda env list
</pre>


== Use Channels ==
== Use Channels ==


Add channels to get more software. We suggest to try the following 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
</pre>
</source>


Search in default and extra channel:
Search in default and extra channel:
<source lang="bash">
<pre>
conda search -c intel scipy
conda search -c conda-forge scipy
</pre>
</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 intel
conda config --add channels bioconda
conda config --add channels conda-forge
conda config --show channels
conda config --show channels
conda config --remove channels intel # remove again
conda config --remove channels bioconda # remove channel again
</pre>
</source>


== Use Intel Conda Packages ==
=== Use conda-forge Conda Packages ===


You can find the full list of Intel Python packages on the [https://software.intel.com/en-us/articles/complete-list-of-packages-for-the-intel-distribution-for-python Intel web site].
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 Intel Python stack:
You can install the core conda-forge Python stack:
<source lang="bash">
<pre>
conda install -c intel -n intelpython3 intelpython3_core
conda install -c conda-forge -n conda-forgepython3 conda-forgepython3_core
</pre>
</source>


... with a special Python version:
... with a "fuzzy" Python version (see [[#Versioning|Versioning]]):
<source lang="bash">
<pre>
conda install -c intel -n intelpython-3.6.5 intelpython3_core python=3.6.5
conda install -c conda-forge -n conda-forgepython-3.9.10 conda-forgepython3_core python=3.9.10
</pre>
</source>


... with a Intel update version:
... with an exact conda-forge OneApi version (see [[#Versioning|Versioning]]):


<source lang="bash">
<pre>
conda create -c intel -n intelpython-2018.0.3 intelpython3_core==2018.0.3
conda create -c conda-forge -n conda-forgepython-2022.1.0 conda-forgepython3_core==2022.1.0
</pre>
</source>


... or the full Intel Python stack:
... or the full conda-forge Python stack:
<source lang="bash">
<pre>
conda create -c intel -n intelpython-2018.0.3 intelpython3_full==2018.0.3
conda create -c conda-forge -n conda-forgepython-2022.1.0 conda-forgepython3_full==2022.1.0
</pre>
</source>


... or just some Intel MKL optimized scientific software for the newest Intel 2019 version:
... 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 intel -n scipy-1.1.0-np115py36_6 intelpython3_core=2019
conda create -c conda-forge -n scipy-1.7.3 scipy=1.7.3=py39h5c0f66f_1
</pre>
</source>


== Create Reproducible Conda Environments ==
= 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.1.0-np115py36_6 -f scipy-1.1.0-np115py36_6.yml
conda env export -n scipy-1.7.3 -f scipy-1.7.3.yml
</pre>
</source>


Re-create saved environment:
Re-create saved environment:
<source lang="bash">
<pre>
conda env create -f scipy-1.1.0-np115py36_6.yml
conda env create -f scipy-1.7.3.yml
</pre>
</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.1.0-np115py36_6 &gt;scipy-1.1.0-np115py36_6.txt
conda list --explicit -n scipy-1.7.3 >scipy-1.7-3.txt
</pre>
</source>


Install requirements file into environment:
Install requirements file into environment:
<source lang="bash">
<pre>
conda create --name scipy-1.1.0 --file scipy-1.1.0-np115py36_6.txt
conda create --name scipy-1.7.3 --file scipy-1.7.3.txt
</pre>
</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.1.0 --clone scipy-1.1.0-np115py36_6
conda create --name scipy-1.7.3-clone --clone scipy-1.7.3
</pre>
</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
</pre>
</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
</pre>
</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.1.0-np115py36_6 >scipy-1.1.0-np115py36_6.txt
conda list --explicit -n scipy-1.7.3 >scipy-1.7.3.txt
for f in $( grep -E '^http|^file' scipy-1.1.0-np115py36_6.txt ); do
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
</pre>
</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.1.0-np115py36_6.txt ); do
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
</pre>
</source>


Initialize channel:
Initialize channel:
<source lang="bash">
<pre>
conda index $( ws_find conda )/conda/channel/
conda index $( ws_find conda )/conda/channel/
</pre>
</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/
</pre>
</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 ===
== 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
</pre>
</source>


Pack activated environment:
Pack activated environment:
<source lang="bash">
<pre>
conda activate scipy-1.1.0-np115py36_6
conda activate scipy-1.7.3
(scipy-1.1.0-np115py36_6) $ conda pack
(scipy-1.7.3) $ conda pack
(scipy-1.1.0-np115py36_6) $ conda deactivate
(scipy-1.7.3) $ conda deactivate
</pre>
</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.1.0-np115py36_6
conda pack -p $( ws_find conda )/conda/envs/scipy-1.7.3
</pre>
</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.1.0-np115py36_6
mkdir -p external_conda_path/envs/scipy-1.7.3
tar -xf scipy-1.1.0-np115py36_6.tar.gz -C external_conda_path/envs/scipy-1.1.0-np115py36_6
tar -xf scipy-1.7.3.tar.gz -C external_conda_path/envs/scipy-1.7.3
conda activate scipy-1.1.0-np115py36_6
conda activate scipy-1.7.3
# Cleanup prefixes from in the active environment
# Cleanup prefixes from in the active environment
(scipy-1.1.0-np115py36_6) $ conda-unpack
(scipy-1.7.3) $ conda-unpack
(scipy-1.1.0-np115py36_6) $ conda deactivate
(scipy-1.7.3) $ conda deactivate
</pre>
</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 294:
|-
|-
|exact version
|exact version
|scipy==1.1.0
|scipy==1.7.3
|-
|-
|fuzzy version
|fuzzy version
|scipy=1.1
|scipy=1.7
|-
|-
|greater equal
|greater equal
|&quot;scipy&gt;=1.1&quot;
|&quot;scipy>=1.7&quot;
|}
|}


For more information see cheat sheet below.
For more information see the [[#Cheat_Sheet]].


Example:
Example:
<source lang="bash">
<pre>
conda create -c intel -n scipy-1.1.0 scipy==1.1.0=np115py36_6
conda create -c conda-forge -n scipy-1.7.3 scipy==1.7.3=py39h5c0f66f_1
</pre>
</source>


=== Pinning ===
=== Pinning ===
Line 375: Line 315:


Example:
Example:
<source lang="bash">
<pre>
echo 'scipy==1.1.0=np115py36_6' &gt;&gt; $( ws_find conda )/conda/envs/scipy-1.1.0-np115py36_6/conda-meta/pinned
echo 'scipy==1.1.0=np115py36_6' >> $( ws_find conda )/conda/envs/scipy-1.1.0-np115py36_6/conda-meta/pinned
</pre>
</source>


You can easily pin your whole environment:
You can easily pin your whole environment:
<source lang="bash">
<pre>
conda list -n scipy-1.1.0-np115py36_6 --export &gt;$( ws_find conda )/conda/envs/scipy-1.1.0-np115py36_6/conda-meta/pinned
conda list -n scipy-1.7.3 --export >$( ws_find conda )/conda/envs/scipy-1.7.3/conda-meta/pinned
</pre>
</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]


== PDF Document ==
= 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 13:03, 9 October 2024

-- Caution --
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
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

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
# Execute
$ sh Miniconda3-latest-Linux-x86_64.sh
# update .bashrc
$ 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.

Cheat Sheet

Conda official cheat sheet