تخطَّ إلى المحتوى

Micromamba Cheat Sheet

Overview

Micromamba is a lightweight, standalone implementation of the conda package manager written in C++. It provides the same environment and package management capabilities as conda but as a single static binary with no Python dependency. Micromamba is significantly faster than conda for solving dependencies and creating environments.

Micromamba is fully compatible with conda packages, channels, and environment files. It uses the same environment.yml format and can install packages from conda-forge, defaults, and custom channels. It is ideal for CI/CD pipelines, Docker containers, and situations where a minimal footprint is desired.

Installation

# Linux/macOS (official installer)
"${SHELL}" <(curl -L micro.mamba.pm/install.sh)

# macOS
brew install micromamba

# Windows (PowerShell)
Invoke-Expression ((Invoke-WebRequest -Uri https://micro.mamba.pm/install.ps1).Content)

# Manual download (Linux)
curl -Ls https://micro.mamba.pm/api/micromamba/linux-64/latest | tar -xvj bin/micromamba
sudo mv bin/micromamba /usr/local/bin/

# Initialize shell
micromamba shell init --shell bash --root-prefix ~/.local/share/mamba
source ~/.bashrc

# Verify
micromamba --version

Core Commands

CommandDescription
micromamba create -n <env>Create a new environment
micromamba activate <env>Activate an environment
micromamba deactivateDeactivate current environment
micromamba install <pkg>Install packages
micromamba remove <pkg>Remove packages
micromamba update <pkg>Update packages
micromamba listList installed packages
micromamba env listList environments
micromamba env remove -n <env>Remove environment
micromamba search <pkg>Search for packages
micromamba clean --allClean caches
micromamba infoShow configuration info

Environment Management

Create Environments

# Create with specific Python version
micromamba create -n myenv python=3.12 -c conda-forge

# Create with packages
micromamba create -n datascience python=3.12 numpy pandas scikit-learn -c conda-forge

# Create from environment.yml
micromamba create -n myenv -f environment.yml

# Create with specific channel priority
micromamba create -n myenv python=3.12 -c conda-forge -c defaults --strict-channel-priority

Activate and Use

# Activate
micromamba activate myenv

# Install additional packages
micromamba install jupyterlab matplotlib seaborn

# Update packages
micromamba update numpy
micromamba update --all

# Deactivate
micromamba deactivate

# Remove environment
micromamba env remove -n myenv

environment.yml

name: ml-project
channels:
  - conda-forge
  - pytorch
dependencies:
  - python=3.12
  - numpy>=1.26
  - pandas>=2.1
  - scikit-learn>=1.3
  - pytorch>=2.1
  - matplotlib>=3.8
  - jupyter>=1.0
  - pip:
    - transformers>=4.35
    - wandb>=0.16
# Create from file
micromamba create -f environment.yml

# Update existing env from file
micromamba update -f environment.yml -n ml-project

# Export environment
micromamba env export -n ml-project > environment.yml
micromamba env export -n ml-project --no-builds > environment-portable.yml

Configuration

.mambarc Configuration

# ~/.mambarc
channels:
  - conda-forge
channel_priority: strict
auto_activate_base: false
always_yes: false

Channel Management

# Add channel
micromamba config append channels conda-forge

# Set strict channel priority
micromamba config set channel_priority strict

# Show config
micromamba config list

Advanced Usage

Docker Integration

FROM mambaorg/micromamba:1.5

COPY --chown=$MAMBA_USER:$MAMBA_USER environment.yml /tmp/environment.yml
RUN micromamba install -y -n base -f /tmp/environment.yml && \
    micromamba clean --all --yes

COPY . /app
WORKDIR /app

CMD ["python", "app.py"]
# Multi-stage with micromamba
FROM mambaorg/micromamba:1.5 as build

COPY environment.yml .
RUN micromamba create -n app -f environment.yml && \
    micromamba clean --all --yes

FROM debian:bookworm-slim
COPY --from=build /opt/conda/envs/app /opt/conda/envs/app
ENV PATH=/opt/conda/envs/app/bin:$PATH

COPY . /app
CMD ["python", "/app/main.py"]

CI/CD Integration

# GitHub Actions
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: mamba-org/setup-micromamba@v1
        with:
          environment-file: environment.yml
          cache-environment: true
      - run: |
          micromamba activate test
          pytest tests/
        shell: micromamba-shell {0}

Package Pinning

# Pin packages in environment
echo "numpy==1.26.4" >> $CONDA_PREFIX/conda-meta/pinned
echo "python==3.12.*" >> $CONDA_PREFIX/conda-meta/pinned

# Or create a pinned file
cat > $CONDA_PREFIX/conda-meta/pinned << 'EOF'
python==3.12.*
numpy==1.26.*
EOF

Parallel Downloads

# micromamba supports parallel package downloads by default
# Configure max parallel downloads
micromamba config set fetch_threads 8

Running Commands Without Activation

# Run in environment without activating
micromamba run -n myenv python script.py
micromamba run -n myenv jupyter lab
micromamba run -n myenv pytest tests/

Troubleshooting

IssueSolution
Shell not initializedRun micromamba shell init --shell bash
Package not foundAdd conda-forge channel; check package name
Solver conflictUse --strict-channel-priority; relax version pins
Slow solvingUse fewer channels; pin more dependencies
Environment too largeUse micromamba clean --all to clear cache
Pip packages conflictInstall pip deps last; use --no-deps if needed
# Debug info
micromamba info

# Verbose install
micromamba install -v numpy

# Clean everything
micromamba clean --all --yes

# Reindex channels
micromamba clean --index-cache

# List environments
micromamba env list

# Show environment details
micromamba list -n myenv