4. Anaconda Virtual Environments

Using Anaconda Virtual Environments

Conda is an open-source package management system and environment management system. Conda quickly installs, runs, and updates packages and their dependencies. Conda easily creates, saves, loads, and switches between environments on your local computer. It was created for Python programs but it can package and distribute software for any language.

Conda as a package manager helps you find and install packages. If you need a package that requires a different version of Python, you do not need to switch to a different environment manager because conda is also an environment manager. With just a few commands, you can set up a totally separate environment to run that different version of Python, while continuing to run your usual version of Python in your normal environment.

Getting Started With Anaconda

The following versions of Anaconda are the suggested versions available to be loaded as modules on the Engaging Cluster:

  • anaconda3/2021.11
  • anaconda3/2020.10

If you need another version of Anaconda that is not listed above, you will have to compile it yourself. Instructions for that can be found here.

To load one of these Anaconda modules, use the command module load. For example, to load anaconda3/2021.11, the command would be:

module load anaconda3/2021.11

Creating an Environment

Once you load a version of anaconda, you can create an environment with the command:

conda create --name [ENV_NAME] python=3.9

This will create a conda environment named whatever value you put in place for [ENV_NAME], using Python version 3.9. This Python version can be modified to a specific version of Python that you want your environment to use.

You can create a conda environment from a .yml file with the command:

conda env create --file envname.yml

or if the .yml file is in the current directory, you can use the command:

conda env create

You can export a current environment to a .yml file with the command:

conda env export --name ENV_NAME > envname.yml

You can create an exact copy of an existing environment with the command:

conda create --clone ENV_NAME --name NEW_ENV

You can create an environment based on exact package versions using a text file with the command:

conda create --name ENV_NAME --file packages.txt

You can list all current existing environments with the command:

conda env list

You can create a conda environment in a specific location by including the absolute path to said location with:

conda create --prefix=<path/to/env>

Activiating An Environment

Once you create an environment, you will have to activate it to use it. You can then activate it with the command:

source activate [ENV_NAME]

If your environment was created outside of the base anaconda directory, such as ~/.conda/envs, you will need to add the full path to your “source activate” command and not just the name of the environment.

Once your environment is activated, you will see your command line prompt change. It will start with your environment name in parenthesis at the start of the prompt like:

(ENV_NAME)[username@eofe7 ~]$

Installing Packages Inside An Environment

Once inside your environment, you can install packages with the conda install [PACKAGE] command. Some packages must be installed from a specific channel, such as conda-forge. To search for a package in the currently configured channels, use the command:

conda search PACKAGE

To install a package from a specific channel, such as conda-forge, use the command:

conda install -c conda-forge PACKAGE

To install a specific version of a package found by searching conda use the command:

conda install PACKAGE=VERSION

Ex.: conda install pytorch=1.8.1

To further specify a certain build of a certain version, as found in the search results, use:

Ex.: conda install pytorch=1.8.1=cpu_py39h60491be_0

To see all currently installed packages in the currently active environment use the command:

conda list

To see all the currently installed packages in a named environment, use the command:

conda list --name ENV_NAME

To add a channel to your conda config to access packages on that channel, use the command:

conda config --add channels CHANNELNAME

To view detailed information about a package, use the command:

conda search PACKAGE --info

To remove unused cache files and unused packages, use the command:

conda clean --all

To uninstall a package from an environment, use the command:

conda uninstall PACKAGE --name ENV_NAME

To update all packages in an environment, use the command:

conda update --all --name ENV_NAME

To run most commands without a user prompt, you can pass the YES flag to them with a command like:

conda install --yes PACKAGE1 PACKAGE2

To see your current conda config, use the commands:

conda config --show

conda config --show-sources

Deactivating An Environment

To deactivate a conda enviroment and get out of the enviroment, use the command:

source deactivate

Removing an Enviroment

To completely delete an enviroment, use the command:

conda remove --name ENV_NAME --all

For more useful conda commands, please see here.

If you have any questions or problems using Anaconda, please email orcd-help-engaging@mit.edu