NEMO/Software/Conda: Difference between revisions

From bwHPC Wiki
< NEMO‎ | Software
Jump to navigation Jump to search
mNo edit summary
m (M Janczyk moved page NEMO/SW/Conda to NEMO/Software/Conda)
 
(No difference)

Latest revision as of 11:36, 6 December 2022

Use Conda Environments on NEMO

Conda can be used to easily install missing Python packages by yourself in different Python environments with different versions in their own workspaces.

On NEMO, you can simply load the Conda module and start creating environments.

Use the centrally installed Conda Module

The conda module is provided on NEMO, so you can easily load it and create a workspace.

Load conda module:

module load devel/conda

Create workspace:

ws_allocate conda <days>   # e.g. 100

The last step defines the newly created workspace as the download and installation path for your environments:

conda config --prepend envs_dirs $( ws_find conda )/conda/envs
conda config --prepend pkgs_dirs $( ws_find conda )/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).

Install Packages into Environments

You can create python environments and install packages into these environments or create them during install:

conda create -n scipy
conda activate scipy

Create a new environment and install packages into it with a single command:

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
conda activate scipy-py27


Activating Environments

To use the software in an environment, you must first activate it:

conda activate scipy

Disable the environment to load a different Python or software version:

conda deactivate


List packages and Environments

List packages of current environment:

conda list

List packages in given environment:

conda list -n scipy

List environments:

conda env list


Use Channels

Add channels to get more software. We suggest to try the following channels:

conda-forge
intel
bioconda

Search in default and extra channel:

conda search -c intel scipy

You can add channel to your channels, but than you'll search and install automatically from this channel:

conda config --add channels intel
conda config --show channels
conda config --remove channels intel   # remove channel again


Use Intel Conda Packages

The full list of Intel Python packages can be found in the conda channel.

You can install the core Intel Python stack:

conda install -c intel -n intelpython3 intelpython3_core

... with a "fuzzy" Python version (see Versioning):

conda install -c intel -n intelpython-3.9.10 intelpython3_core python=3.9.10

... with an exact Intel OneApi version (see Versioning):

conda create -c intel -n intelpython-2022.1.0 intelpython3_core==2022.1.0

... or the full Intel Python stack:

conda create -c intel -n intelpython-2022.1.0 intelpython3_full==2022.1.0

... or just some Intel MKL optimized scientific software for the newest Intel OneAPI version 2022:

conda search -c intel scipy
conda create -c intel -n scipy-1.7.3 scipy=1.7.3=py39h5c0f66f_1


Deleting environments

Example:

conda env remove -n scipy-1.7.3 --all




Create Reproducible Conda Environments

This section is intended for advanced users who want to secure environments and create reproducible environments.

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

Local channels and backup Conda packages

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 cheat sheet below.

Example:

conda create -c intel -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.7.3=py39h5c0f66f_1' >> $( ws_find conda )/conda/envs/scipy-1.7.3/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 intel -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