Skip to content

Conda and mamba

Description

Conda and mamba are open source package manager and environment management systems for Python and other languages. They run on Windows, macOS, Linux and z/OS. These tools automate the process of installing, updating, configuring and removing software packages. They manage dependencies, ensuring that all required libraries and components are correctly installed and compatible. In our case, using package managers is specially useful because:

  • They provide a way to ensure reproducibility in our code
  • They allow a project's software tools to be portable
  • They allow user-level installation (i.e. do not require sudo permissions)

The difference between the two is that mamba is a fast, drop-in replacement for conda that highly improves package resolution speed and efficiency (and we recommend you use it instead of conda to make your life happier :)).

Info

The majority of the commands shown below are also supported by mamba

Installation

Conda

Conda has two possible installers: - Anaconda: already comes with installed packages for data science and Anaconda Navigator (GUI application). Follow the instructions. - Miniconda (recommended): minimal installer provided by Anaconda

Download any of the two installers and in your terminal run:

bash <conda-installer-name>-latest-Linux-x86_64.sh>

Detailed instructions here. To update conda, run:

conda update conda

Mamba

The installer for mamba is micromamba. To install it run:

"${SHELL}" <(curl -L micro.mamba.pm/install.sh)
To update mamba:
micromamba self-update

Get started

conda create -n <env name> <package[=<version>]>
conda activate <env name>
conda install <package[=version]>

Cheatsheet

Environments

Activate an environment:

conda activate <environment name>

Deactivates an environment. If in base, closes conda.

conda deactivate

List all environments:

conda env list
conda info --envs

Create a new virtual environment with

conda create --name <environment name> [<packages[=<version>]>]

Export active environment to a file

conda env export > environment.yml

Export all environments to its own file:

for env in $(conda env list | cut -d" " -f1); do 
   if [[ ${env:0:1} == "#" ]] ; then continue; fi;
   conda env export -n $env > ${env}.yml
done

Create environment from file

conda env create -f environment.yml

Clone an environment

conda create --name <environment name> --clone <original environment>

Remove an environment

conda env remove --name <environment name>
conda remove --name <environment name> --all

List all packages installed (in current environment)

conda list

List all packages installed with path

conda list --explicit

Show history of changes in packages

conda list --revisions

Packages

Install a package (use -f to force the installation)

conda install <package>

Install package(s) specified in a file (like a Python requirements file)

conda install --file <file>

Uninstall a package

conda remove <package>

Search for a package

conda search <package>

Configuration

Show configuration

conda config --show

Add channels (use add instead of append to put the channel on the top of the list)

conda config --append channels <channel name>

Building Packages

Install conda build

conda install conda-build

Build package

conda build <directory with the files>

Build for other platforms

conda convert --platform all <path to package>

Install built package

conda install --use-local <package>

From Pypi

Create files

conda skeleton pypi <package>

Build for different Python version

conda build --python <version> <directory with the files>

Custom Channel

Add channel

conda config --append channels file://<path to folder>

(re)build the index

conda index <channel folder>/<platform>

Reference

  • Jordi Deu Pons
  • Miguel Grau
  • Federica Brando
  • Carlos López-Elorduy
  • Raquel Blanco