Development/Conda: Difference between revisions

From bwHPC Wiki
Jump to navigation Jump to search
(Created page with "= Install and use Conda = [https://conda.io/docs/index.html Conda] can be used to easily install missing Python packages on your own into different Python environments with d...")
 
mNo edit summary
 
(33 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] 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
! 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:
{|style="background:#ffdeee; width:100%;"
<pre>
|style="padding:5px; background:#f2cece; text-align:left"|
ws_allocate conda &lt;days&gt; # e.g. 100
[[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.continuum.io/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 ===


You will need to add this line to your jobscripts such that the environments are available on the compute nodes:
Optional: Add source path in your local <syntaxhighlight style="border:0px" inline=1>~/.bashrc</syntaxhighlight> (edit file):


<source lang="bash">
<pre>
source $( ws_find conda )/conda/etc/profile.d/conda.sh
source $HOME/miniconda3/etc/profile.d/conda.sh
conda activate <env_name>
</pre>
</source>


== Create Environments and Install Software ==
Optional: Update conda (usually base enviroment):
<pre>
conda update -n base conda
</pre>


An environment is an isolated space that allows you to manage a custom constellation of software packages and versions.
Optional: Removing installation script:
<pre>
m miniconda.sh
</pre>


If you want, you can set a specific installation directory for your environments, for example a workspace:
== (B) Use a centrally installed Conda Module ==
<source lang="bash">

conda config --prepend envs_dirs /path/to/conda/envs
If a Conda Module is provided you can just load it and create environments a workspace.
conda config --prepend pkgs_dirs /path/to/conda/pkgs

Load conda module:
<pre>
module avail tools/conda # list Conda modules
module load tools/conda # load module
</pre>

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:
<pre>
conda config --append envs_dirs $( ws_find conda )/conda/envs
conda config --append pkgs_dirs $( ws_find conda )/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
source activate scipy
conda install scipy
conda activate scipy
# Install software into this environment
</pre>
(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
source 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 activate scipy_py27
</source>
</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>
source 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>
source deactivate
conda deactivate
</pre>
</source>


== List packages and Environments ==
Deleting Environments:
<source lang="bash">
conda env remove -n scipy-1.7.3 --all
</source>


== List Environments and Packages ==
List packages of current environment:


List environments:
<pre>
<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:
<source lang="bash">
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 delete an environment:
<pre>
conda env remove -n scipy-1.1.0
</pre>


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

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>:
<source lang="bash">
conda install conda-build
</source>

Create local channel directory for <syntaxhighlight style="border:0px" inline=1>linux-64</syntaxhighlight>:
<source lang="bash">
mkdir -p $( ws_find conda )/conda/channel/linux-64
</source>

Create dependency file list and copy files to channel:
<source lang="bash">
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
</source>

Optional: If packages are missing in the cache download them:
<source lang="bash">
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
</source>

Initialize channel:
<source lang="bash">
conda index $( ws_find conda )/conda/channel/
</source>

Add channel to the channels list:
<source lang="bash">
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.

== Backup whole Environments ==

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>:
<source lang="bash">
conda install -c conda-forge conda-pack
</source>

Pack activated environment:
<source lang="bash">
conda activate scipy-1.7.3
(scipy-1.7.3) $ conda pack
(scipy-1.7.3) $ conda deactivate
</source>

Pack environment located at an explicit path:
<source lang="bash">
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.

Just create a directory and unpack the package:
<source lang="bash">
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
</source>


== Versioning ==
== Versioning ==
Line 218: Line 303:
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.
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.


{| class="wikitable"
{|
!Constraint
!Constraint
!Specification
!Specification
|-
|-
|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 244: Line 329:


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

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:
<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:
<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.


== Cheat Sheet ==
= Cheat Sheet =


[https://conda.io/docs/_downloads/conda-cheatsheet.pdf Conda official cheat sheet]
[https://docs.conda.io/projects/conda/en/latest/user-guide/cheatsheet.html Conda official cheat sheet]

Latest revision as of 17:03, 8 November 2024

Attention.svg

The licensing situation with Anaconda is currently unclear. To be on the safe side, make sure to only use open source channels!
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 Python.

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

Attention.svg

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.

Cheat Sheet

Conda official cheat sheet