3 min read

Mamba: a robust alternative to Anaconda package manager

Mamba: a robust alternative to Anaconda package manager

Did you ever try to install a complicated mix of conda packages only to find yourself in an infinite "Solving environment" and "Retrying with flexible solve" loop? Or maybe your installation succeeded, but later you found out that some package versions are out of date? As good as Anaconda ecosystem is, and as much as I advocate using conda at all times, sometimes the default dependency resolver just doesn't cut it.

The other day I was yet again struggling with conda not being able to install the packages I needed. But this time instead of just giving up and using pip, I decided to do a little research. Imagine my surprise when I found a great drop-in replacement for conda while digging in the bowels of some obscure GitHub issue.

What's mamba?

Mamba is an alternative package manager for conda ecosystem. It has a much better dependency resolver and it supports almost all the commands conda does. You can do your ususal routines, but just type mamba instead of conda:

mamba create -n test python numpy
mamba activate test
mamba install notebook

The minor inconvenience is that you still need to use conda config to manage configuration from command line. For all other things use mamba!

Installing mamba

Since mamba is just a binary that relies on conda ecosystem, you can simply add it to your existing Anaconda or Miniconda installation:

conda install mamba -n base -c conda-forge

It's important to install mamba into base environment. After that you can start using it!

New installation with mambaforge

When you are bootstrapping a new machine, you can install mambaforge instead of Miniconda or full Anaconda distribution. Mambaforge is similar to Miniconda in size, but comes with preconfigured conda-forge channel and preinstalled mamba binary.

As per GitHub documentation, install for Unix-like systems:

wget https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-$(uname)-$(uname -m).sh
bash Mambaforge-$(uname)-$(uname -m).sh

And for Windows just download and run the installer.

I also strongly suggest to turn on strict channel priority:

conda config --set channel_priority strict

Do I need it, or should I just stick with conda?

If you are not convinced that mamba resolves dependencies better than conda, let's do a little experiment.

Warning! The following experiment is valid at the time of writing, but things may change in the future, when packages evolve or conda dependency resolver is improved.

So let's do something really simple: create a new environment with pytorch and mlflow. We will use pytorch and conda-forge channels. First, let's try to create a new environment with channel_priority option set to strict (a recommended way of doing things with conda-forge). Here is my .condarc:

channels:
  - pytorch
  - conda-forge
  - defaults
channel_priority: strict

Let's try creating a new environment with plain old conda:

conda create -n test pytorch mlflow

It would seem like we are getting a sensible install plan: pytorch is v. 1.9.0 and mlflow is 1.18.0 which are the latest versions at the time of writing. But let's look more closely at Python version that's going to be installed:

The following NEW packages will be INSTALLED:

  alembic            conda-forge/osx-64::alembic-0.9.5-py36_0
  mlflow             conda-forge/osx-64::mlflow-1.18.0-py36h6d49074_0
  python             conda-forge/osx-64::python-3.6.13-h7728216_0_cpython
  pytorch            pytorch/osx-64::pytorch-1.9.0-py3.6_0
  
  # Other packages omitted

Yes, we are getting Python 3.6.13, while Python 3.9.5 is available and compatible with Pytorch and MLFlow! I will omit some cumbersome dependency analysis, but it looks like alembic dependency is the culprit. Despite the fact that MLFlow lists alembic <=1.4.1 as its dependency, we are getting some ancient version 0.9.5, which seems to force Python to downgrade to 3.6.13.

Fine, but maybe the problem is with strict channel priority? Let's try removing this option from .condarc.

channels:
  - pytorch
  - conda-forge
  - defaults
conda create -n test pytorch mlflow

OK, this time it's a little better: we are getting Python 3.7.10. Oh, come on!

The following NEW packages will be INSTALLED:

  alembic            pkgs/main/osx-64::alembic-1.0.9-py37_0
  mlflow             conda-forge/osx-64::mlflow-1.18.0-py37h712bc25_0
  python             conda-forge/osx-64::python-3.7.10-h7728216_100_cpython
  pytorch            pytorch/osx-64::pytorch-1.9.0-py3.7_0

  # Other packages omitted

Trying it with mamba

Let's turn strict priority back on and try the same thing with mamba.

channels:
  - pytorch
  - conda-forge
  - defaults
channel_priority: strict
mamba create -n test pytorch mlflow
  alembic        1.4.1  py_0                conda-forge/noarch     Cached
  mlflow         1.18.0  py39h8ac9d56_0      conda-forge/osx-64     Cached
  python         3.9.5  hd187cdc_0_cpython  conda-forge/osx-64     Cached
  pytorch        1.9.0  py3.9_0             pytorch/osx-64         Cached

We are finally getting up-to-date versions for all packages! And a nice poisonous kill-you-in-a-blink-of-an-eye snake in your console! Can this day get any better?

Yes, you need it.

So if you are using conda ecosystem there is absolutely no reason not to install mamba and use it for all of your package management.