Skip to content

Conda

During installation (Anaconda), conda creates a generic environment called base which is activated by default. Therefore, when installing a package with conda install, the package will be installed in that environment.

conda activate your_env
conda deactivate

To have a full in depth introduction to environment management with conda read the official documentation: Manage Environments.

Channels

To see the configured channels use the command conda info.

The defaults channel points to several channels at the repo.anaconda.com repository.

  • Defaults channels (https://repo.anaconda.com/pkgs/)
    • pkgs/main - Includes packages built by Anaconda, Inc. with the new compiler stack
    • pkgs/msys2 - Windows only. Included in conda's defaults channel. Necessary for Anaconda, Inc.'s R conda packages and some others in pkgs/main and pkgs/free
    • pkgs/r - Microsoft R Open conda packages and Anaconda, Inc.'s R conda packages.
  • Other channels
    • conda-forge - A community repository that provides conda packages for a wide range of software. (more channel info)
    • anaconda - A mirror of the packages available in main, free, and pro hosted on repo.anaconda.com

Common commands

search <package>
     Search a package within the configured channels.
install <package>
     Install a package. Use ``-c`` to specify a particular channel, e.g. ``conda install -c conda-forge <package>``.
env list
     List conda environments
list
     List installed packages in the current environment. Use ``-n`` to specify a particular environment.
info
     Print some information about conda. Example:
     active environment : None
       user config file : C:\Users\user.name\.condarc
 populated config files : C:\Users\user.name\.condarc
          conda version : 4.8.4
    conda-build version : 3.18.11
         python version : 3.8.3.final.0
       virtual packages : __cuda=10.2
       base environment : C:\ProgramData\Anaconda3  (read only)
           channel URLs : https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/win-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
          package cache : C:\ProgramData\Anaconda3\pkgs
                          C:\Users\user.name\.conda\pkgs
                          C:\Users\user.name\AppData\Local\conda\conda\pkgs
       envs directories : C:\Users\user.name\.conda\envs
                          C:\ProgramData\Anaconda3\envs
                          C:\Users\user.name\AppData\Local\conda\conda\envs
               platform : win-64
             user-agent : conda/4.8.4 requests/2.24.0 CPython/3.8.3 Windows/10 Windows/10.0.17134
          administrator : False
             netrc file : None
           offline mode : False
pip freeze | cut -d "@" -f1 | xargs pip uninstall -y

     Uninstall all pip packages

Configuration

Official docs about conda configuration: Configuration User-Guide

Conda configuration is stored in .condarc which is usually in your home directory. If that file isn't present, you can either create it manually or run conda config a first time to generate it. You can either edit it manually, or use the command bellow to set parameters:

     conda config --add channels conda-forge
config --get
     Get all configuration keys and values

Useful configuration

If you have a very fast SSD, you might increase the number of threads to shorten the time it takes for conda to create environments and install/remove packages (More config info).

conda config --set default_threads 6

Add conda-forge channel but with lowest priority:

conda config --append channels conda-forge

As of version 4.6.0, Conda has a strict channel priority feature. Strict channel priority can dramatically speed up conda operations and also reduce package incompatibility problems.

conda config --set channel_priority strict

Your .condarc file should looks like this:

ssl_verify: true
channels:
- defaults
- conda-forge
default_threads: 6
channel_priority: strict

Export an environment for sharing

conda env export > environment.yml

The newly created environment.yml file contains all packages installed within the current environment. The file contains the parameter prefix at the end of the file which points to your local installation. This information can be ignored or removed from the file without any impact when creating a new environment on a different machine.

Important

The command conda list --export > req.txt is not recommanded to be used to export an environment because if a package have been installed from a different channel than the default one, conda is unable to find that package when installing the environment with conda create --name myenv --file req.txt.

Create an environment from file

conda env create -f condaenv.yml
If you don't specify a name for the new environment with -n myenv, the name configured in the .yml will be used.

Update an environment from file

conda env update -f condaenv.yml
Without activating the environment use --n myenv.

Show conda environments in Jupyter notebook/lab

If you want all your conda environments to be listed in Jupyter notebook and lab, make sure you've installed the packages ipykernel and nb_conda_kernels in the environment from which you run Jupyter.

conda install -n myenv ipykernel nb_conda_kernels
Howerver, with many environments installed the view can gets messy: It is recommanded to add the kernel manually and give an explicit name. To do so, while in the environment, run the following command:
python -m ipykernel install --user --name=myenv

Rename an environment

You can't! You need to clone the environment and give a new name:

conda create --name new_name --clone old_name
conda remove --name old_name --all

Delete an environment

Ooh poor environment :(

conda env remove --name myenv

Packages installation revisions

Show all revisions:

conda list --revisions
Revert back to a specific revision:
conda install --revisions N

where N is the revision number