mamba 基本使用

Published

2026-01-22

1 mamba 介紹

Note

現代生物資訊工作流程(特別是 nf-core)已全面建議使用 mamba 取代 conda 進行求解,沒接觸過 conda 的人可參閱 conda教學

mamba 是一個高效、與 conda 完全相容的套件與環境管理工具,由 QuantStack 開發。它的目標是提供比 conda 更快、更穩定、更具可預測性的套件求解(dependency solving)能力,同時保留所有 conda 的使用方式與指令格式。

1.1 為什麼需要 mamba?

傳統的 conda 在進行環境建立或安裝套件時,常因依賴衝突(dependency conflicts)導致求解時間很長(數分鐘到數十分鐘)

mamba 使用 C++ 實作的高速依賴求解器(libsolv),大幅提升速度與穩定性,使許多原本需要 1–10 分鐘的 conda 操作縮短到 數秒完成

功能 conda mamba
依賴求解速度 慢(Python solver) 極快(libsolv)
下載方式 單線程 多線程並行下載
相容性 完全支援 100% 相容 conda
生信 pipeline 推薦度 普通 強烈推薦(nf-core / Snakemake)

1.2 mamba 的核心特色

  • 超高速的依賴求解器
  • 與 conda 指令相同,僅需將 conda 換成 mamba
  • 支援與 conda 共存(Miniforge 已預設內建 mamba)
  • 多線程下載(parallel downloads)
  • 更清楚的錯誤訊息

2 mamba 安裝(直接安裝 Miniforge)

直接安裝 Miniforge,即包含 mamba,也可查閱 mamba 官方下載指南

3 mamba –version (版本)

使用 --version 參數確認 conda 是否成功安裝,並顯示版本號。

mamba --version
2.4.0

4 mamba create (建立環境)

-n 代表創建環境名稱, -y 代表直接確認下載 (yes)

mamba create -n myenv python=3.10 -y

5 mamba activate (啟動環境)

mamba activate myenv

6 mamba install (安裝套件)

mamba install numpy -y

7 mamba remove (移除套件)

mamba remove numpy -y

8 mamba deactivate (離開環境)

mamba deactivate

9 mamba env (環境相關指令)

查看環境相關指令

mamba env --help
Click to expand
See `mamba/micromamba env --help` 


/home/benson/miniforge3/bin/mamba env [OPTIONS] [SUBCOMMANDS]


OPTIONS:
  -h,     --help              Print this help message and exit

Configuration options:
          --rc-file FILE1 FILE2...
                              Paths to the configuration files to use
          --no-rc             Disable the use of configuration files
          --no-env            Disable the use of environment variables

Global options:
  -v,     --verbose           Set verbosity (higher verbosity with multiple -v, e.g. -vvv)
          --log-level ENUM:value in {critical->5,debug->1,error->4,info->2,off->6,trace->0,warning->3} OR {5,1,4,2,6,0,3}
                              Set the log level
  -q,     --quiet             Set quiet mode (print less output)
  -y,     --yes               Automatically answer yes on prompted questions
          --json              Report all output as json
          --offline           Force use cached repodata
          --dry-run           Only display what would have been done
          --download-only     Only download and extract packages, do not link them into
                              environment.
          --experimental      Enable experimental features
          --use-uv            Whether to use uv for installing pip dependencies. Defaults to
                              false.

Prefix options:
  -r,     --root-prefix PATH  Path to the root prefix
  -p,     --prefix PATH       Path to the target prefix
          --relocate-prefix PATH
                              Path to the relocation prefix
  -n,     --name NAME         Name of the target prefix

SUBCOMMANDS:
  list                        List known environments
  create                      Create new environment (pre-commit.com compatibility alias for
                              'micromamba create')
  export                      Export environment
  remove                      Remove an environment
  update                      Update an environment

9.1 mamba env export (匯出環境YAML)

mamba env export > output.yaml

9.2 mamba env create (由YAML檔建立環境)

-f 指定檔名,創建環境名稱為 YAML 中 name 參數

mamba env create -f ./mamba/mamba-envs.yaml -y

9.3 mamba env remove (刪除環境)

mamba env remove -n myenv -y

9.4 mamba env list (列出所有環境)

* 為目前環境

mamba env list
  Name             Active  Path
─────────────────────────────────────────────────────────────────────────
  base                     /home/benson/miniforge3
  bio-tools        *       /home/benson/miniforge3/envs/bio-tools

10 mamba list

列出套件相關資源

10.1 mamba list (查看套件來源)

mamba list
List of packages in environment: "/home/benson/miniforge3/envs/bio-tools"

  Name                           Version            Build                 Channel    
───────────────────────────────────────────────────────────────────────────────────────
  _libgcc_mutex                  0.1                conda_forge           conda-forge
  _openmp_mutex                  4.5                2_gnu                 conda-forge
  _python_abi3_support           1.0                hd8ed1ab_2            conda-forge
  _r-mutex                       1.0.1              anacondar_1           conda-forge
  alsa-lib                       1.2.15.1           hb03c661_0            conda-forge
  annotated-types                0.7.0              pyhd8ed1ab_1          conda-forge
  anyio                          4.12.0             pyhcf101f3_0          conda-forge
  argon2-cffi                    25.1.0             pyhd8ed1ab_0          conda-forge

10.2 mamba list –explicit (詳細套件url)

列出詳細 url

mamba list --explicit
List of packages in environment: "/home/benson/miniforge3/envs/bio-tools"

https://conda.anaconda.org/conda-forge/linux-64/_libgcc_mutex-0.1-conda_forge.tar.bz2
https://conda.anaconda.org/conda-forge/linux-64/_openmp_mutex-4.5-2_gnu.tar.bz2
https://conda.anaconda.org/conda-forge/noarch/_python_abi3_support-1.0-hd8ed1ab_2.conda
https://conda.anaconda.org/conda-forge/noarch/_r-mutex-1.0.1-anacondar_1.tar.bz2
https://conda.anaconda.org/conda-forge/linux-64/alsa-lib-1.2.15.1-hb03c661_0.conda
https://conda.anaconda.org/conda-forge/noarch/annotated-types-0.7.0-pyhd8ed1ab_1.conda
https://conda.anaconda.org/conda-forge/noarch/anyio-4.12.0-pyhcf101f3_0.conda
https://conda.anaconda.org/conda-forge/noarch/argon2-cffi-25.1.0-pyhd8ed1ab_0.conda
https://conda.anaconda.org/conda-forge/linux-64/argon2-cffi-bindings-25.1.0-py314h5bd0f2a_2.conda
https://conda.anaconda.org/conda-forge/noarch/arrow-1.4.0-pyhcf101f3_0.conda
https://conda.anaconda.org/conda-forge/noarch/asttokens-3.0.1-pyhd8ed1ab_0.conda
https://conda.anaconda.org/conda-forge/noarch/async-lru-2.0.5-pyh29332c3_0.conda

11 環境資訊

完整記錄當前執行的 Conda 環境,有助於分析重現、版本追蹤與錯誤排查。

echo "======================================="
echo "Mamba Environment: $(mamba env list | grep '*' | awk '{print $1}')"
echo "======================================="
mamba list
=======================================
Mamba Environment: bio-tools
=======================================
List of packages in environment: "/home/benson/miniforge3/envs/bio-tools"

  Name                           Version            Build                 Channel    
───────────────────────────────────────────────────────────────────────────────────────
  _libgcc_mutex                  0.1                conda_forge           conda-forge
  _openmp_mutex                  4.5                2_gnu                 conda-forge
  _python_abi3_support           1.0                hd8ed1ab_2            conda-forge
  _r-mutex                       1.0.1              anacondar_1           conda-forge
  alsa-lib                       1.2.15.1           hb03c661_0            conda-forge
  annotated-types                0.7.0              pyhd8ed1ab_1          conda-forge
  anyio                          4.12.0             pyhcf101f3_0          conda-forge
  argon2-cffi                    25.1.0             pyhd8ed1ab_0          conda-forge
  argon2-cffi-bindings           25.1.0             py314h5bd0f2a_2       conda-forge

12 mamba –help

顯示 mamba 的主要參數與使用說明,適合作為快速查詢用途。

mamba --help
Click to expand
Version: 2.4.0 



/home/benson/miniforge3/bin/mamba [OPTIONS] [SUBCOMMAND]


OPTIONS:
  -h,     --help              Print this help message and exit
          --version

Configuration options:
          --rc-file FILE1 FILE2...
                              Paths to the configuration files to use
          --no-rc             Disable the use of configuration files
          --no-env            Disable the use of environment variables

Global options:
  -v,     --verbose           Set verbosity (higher verbosity with multiple -v, e.g. -vvv)
          --log-level ENUM:value in {critical->5,debug->1,error->4,info->2,off->6,trace->0,warning->3} OR {5,1,4,2,6,0,3}
                              Set the log level
  -q,     --quiet             Set quiet mode (print less output)
  -y,     --yes               Automatically answer yes on prompted questions
          --json              Report all output as json
          --offline           Force use cached repodata
          --dry-run           Only display what would have been done
          --download-only     Only download and extract packages, do not link them into
                              environment.
          --experimental      Enable experimental features
          --use-uv            Whether to use uv for installing pip dependencies. Defaults to
                              false.

Prefix options:
  -r,     --root-prefix PATH  Path to the root prefix
  -p,     --prefix PATH       Path to the target prefix
          --relocate-prefix PATH
                              Path to the relocation prefix
  -n,     --name NAME         Name of the target prefix

SUBCOMMANDS:
  shell                       Generate shell init scripts
  create                      Create new environment
  install                     Install packages in active environment
  update                      Update packages in active environment
  repoquery                   Find and analyze packages in active environment or channels
  remove, uninstall           Remove packages from active environment
  list                        List packages in active environment
  package                     Extract a package or bundle files into an archive
  clean                       Clean package cache
  config                      Configuration of micromamba
  info                        Information about micromamba
  constructor                 Commands to support using micromamba in constructor
  env                         See `mamba/micromamba env --help`
  activate                    Activate an environment
  run                         Run an executable in an environment
  ps                          Show, inspect or kill running processes
  auth                        Login or logout of a given host
  search                      Find packages in active environment or channels
                              This is equivalent to `repoquery search` command
Back to top