# Conda Environments

[Conda](<https://docs.conda.io/projects/conda/en/latest/index.html>) is an open-source package and environment manager that, like [spack](<https://in.nau.edu/arc/spack-package-installations/>), makes it easy to install, update, and run software. It works with packages in many languages, though it is most commonly associated with Python. Conda environments allow you to isolate packages between environments to ensure that the correct dependency versions are used for your programs or scripts. Additionally, environments ensure that there are no dependency and dependency version collisions created due to having a monolithic set of dependencies for everything.

## Loading and Unloading the Miniforge3 Module

In order to run any `conda` commands on Monsoon, you must first load the `conda` executable into your `PATH` by executing the following command:

```bash
module load miniforge3
```

Once you have loaded the `miniforge3` module, you can check that `conda` is now available by using the following command:

```bash
conda --version
```

Once you are finished using the `miniforge3` module, you can either exit your shell or run the following command:

```bash
module unload miniforge3
```

Once you have ran this command, the `conda` command will no longer be executable as it will be removed from your `PATH`.

Something to keep in mind is that running this command is that it will NOT automatically deactivate your conda environments. If you unloaded the `miniforge3` module before you deactivated the environment, you must first load the `miniforge3` module, deactivate the conda environment, then unload the `miniforge3` module again. Additionally, deactivating an environment after reloading the `miniforge3` module may remove the environment shell prompt. This can be fixed by activating another environment and deactivating it.

## Creating and Deleting an Environment

To create a basic environment, use the `conda create` command:

```bash
conda create -n [env_name]
```

You can also install packages while creating a conda environment by specifying them as arguments:

```bash
conda create -n [env_name] [pkg_name ...]
```

For example, to create an environment with the name `demo_environment` with the `tree` package, a `conda create` command would look like:

```bash
conda create -n demo_environment tree
```

Additionally, you can specify where the environment should be located on the filesystem by replacing the `-n [env_name]` flag with `‐‐prefix [directory]`:

```bash
conda create --prefix /scratch/abc123/conda_envs/demo_environment tree
```

> [!NOTE]
>
> In order to perform actions on an environment in a custom directory such as activating and removing such environment, you must provide the full path to the environment in place of the environment name.

To remove an environment, use the `conda remove` command:

```bash
conda remove -n [env_name] --all
```

This command will remove the specified environment along with all the packages that were installed inside of it.

If you installed your environment in a custom directory, use the following command instead:

```bash
conda remove --prefix [env_dir] --all
```

To verify that the environment was removed, you can use the following command and check that your environment is no longer in the list:

```bash
conda info --envs
```

## Activating and Deactivating an Environment

Once an environment is created, it still needs to be activated in order to use it. This can be done using the `conda activate` command.

```bash
conda activate [env_name]
```

To see what conda environments are currently available to you, you can use the following command:

```bash
conda info --envs
```

For example, to **activate** the `demo_environment` created in the previous section, a `conda activate` command would look like:

```bash
conda activate demo_environment
```

Once you have activated your environment, your shell prompt will now display the name of the active environment before your regular prompt.

```bash
(demo_environment) [abc123@wind]:~$
```

If you recall earlier when creating the `demo_environment` conda environment, we installed the `tree` package so we have the `tree` command available:

```bash
(demo_environment) [abc123@wind]:~$ tree
```

To deactivate an environment, use the following command:

```bash
conda deactivate
```

Once you have ran this command, all the programs within the environment that were executable from your `PATH` will no longer be accessible, and any Python scripts ran will no longer use the environment-specific libraries that were previously accessible.

Additionally, your shell prompt will return back to normal.

```bash
[abc123@wind]:~$
```

## Exporting and Importing an Environment

Exporting environments is particularly useful if you need to move your environment from one location to another, particularly as conda environments can take up a lot of disk space. One such action would be moving an environment from your `/home` to your `/scratch` directory.

To export an environment, use the `conda list` command and write the output into a file:

```bash
conda list --explicit > [env_file]
```

> [!NOTE]
>
> You must first activate your target conda environment. Otherwise, the default conda environment will be exported, which probably isn’t what you are trying to do.

For example, to export the active conda environment to a file called `demo_environment_export.txt`, the commands would look like:

```bash
conda activate demo_environment
conda list --explicit > demo_environment_export.txt
```

> [!NOTE]
>
> Exported conda environments are platform-dependent. To identify what platform your exported environment will work on, view the 3<sup>rd</sup> line in your export file containing the `platform` keyword.
>
> ```text
> # platform: linux-64
> ```

> [!NOTE]
>
> This export process will only export the installed packages and does not include conda environment configuration.

To import an environment, use the `conda create` command:

```bash
conda create -n [env_name] --file [env_file]
```

For example, to import an environment with the target name `imported_environment` from the file `demo_environment_export.txt`, the command would look like:

```bash
conda create -n imported_environment --file ./demo_environment_export.txt
```

> [!NOTE]
>
> Exported conda environments are platform-dependent. To identify what platform your exported environment will work on, view the 3<sup>rd</sup> line in your export file containing the `platform` keyword.\*
>
> ```text
> # platform: linux-64
> ```

> [!NOTE]
>
> This import process will only import the packages listed in the import file and does not include conda environment configuration.

In order to specify where the conda environment should be imported to, replace the `-n` flag along with the name with `‐‐prefix [path]`:

```bash
conda create --prefix /scratch/$USER/conda_envs/demo_environment --file ./demo_environment_export.txt
```

> [!NOTE]
>
> In order to perform actions on an environment in a custom directory such as activating and removing such environment, you must provide the full path to the environment in place of the environment name.

## Setting the Default Environment Location

When installing conda packages, you may notice that environments take up a lot of space in your `/home`. To fix this, you will need to define a custom location for your conda environments. You can see where environments and downloaded packages are stored with the `conda config –show` command:

```bash
conda config --show envs_dirs
conda config --show pkgs_dirs
```

You can change the location of new environments and cached package downloads with the `conda config –add` command:

```bash
conda config --add envs_dirs "/scratch/$USER/conda/envs"    # create new environments here
conda config --add pkgs_dirs "/scratch/$USER/conda/pkgs"    # cache package downloads here
```

Once the file is present, you should be able to run your `conda` commands without error. If you get any errors when running `conda` commands, make sure the syntax is the same as in the example above.
