# Using Conda and Anaconda

Conda is a package manager system for Python and other tools and is widely used in some areas such as bioinformatics and data science. On personal computers it is a useful way to install a stack of tools.

The full documentation can be found at [https://docs.conda.io/projects/conda/en/latest/user-guide/index.html](https://docs.conda.io/projects/conda/en/latest/user-guide/index.html)

<span style="background-color: rgb(251, 238, 184); color: rgb(224, 62, 45);">***Warning: Conda can be used freely for research purposes but pay attention to never use the "default" channel since it is not free in a research context like UNIL ([<span style="background-color: rgb(251, 238, 184);">https://www.anaconda.com/blog/is-conda-free</span>)](https://www.anaconda.com/blog/is-conda-free)). As a replacement to "default" channel, please use "conda-forge". If you have any doubt about that please contact us at <helpdesk@unil.ch> (and start the subject with DCSR).***</span>

A tutorial video on using Conda on the cluster is available [here](https://formations.unil.ch/course/view.php?id=511).


#### Setting up Conda

First load the appropriate modules

```bash
dcsrsoft use 20241118
module load miniforge3/24.11.3-2
conda_init
```

Please ignore any messages about updating to a newer version of conda!

#### Configuring Conda

By default Conda will put everything including downloads in your home directory. Due to the limited space available this is probable not what you want.

We strongly recommend that you create a `.condarc` file in your home directory with the following options:

```yaml
pkgs_dirs:
  - /work/path/to/my/project/space/conda_pkgs
auto_activate_base: false
channels:
  - conda-forge
```

where the path is the path to your project space on /work - we do not recommend installing things in /scratch as they might be automatically deleted.

You may also wish to add a non standard `env_dirs`

```yaml
envs_dirs:
  - ~/myproject-envs
```

Please see the full `condarc` documentation for all the possible configuration options

[https://docs.conda.io/projects/conda/en/latest/user-guide/configuration/use-condarc.html](https://docs.conda.io/projects/conda/en/latest/user-guide/configuration/use-condarc.html)


#### Using Conda virtual environments

The basic commands for creating conda environments are:

##### Creation

```bash
conda create --name $MY_CONDA_ENV_NAME
```

##### Activation

```bash
conda activate $MY_CONDA_ENV_NAME
```

##### Deactivation

```bash
conda deactivate
```


##### Environment in specific location

If you need to create an environment in a non standard location:

```bash
conda create --prefix $MY_CONDA_ENV_PATH

conda activate $MY_CONDA_ENV_PATH

conda deactivate
```

##### Installing packages

The base commands are:

```
conda search $PACKAGE_NAME
conda install $PACKAGE_NAME
```

#### Running Slurm jobs with conda

Since Conda needs some initialization before being used, a Sbatch script must explicitly ask to run bash in *login* mode. This can be performed by adding `--login` option to the shebang. Here is an example of Sbatch script using Conda:

```shell
#!/bin/bash --login

#SBATCH --time 00-00:05:00
#SBATCH --cpus-per-task 1
#SBATCH --mem 4G

dcsrsoft use 20241118
module load miniforge3
conda_init
conda activate $MY_CONDA_ENV_PATH
…
```