Conda 基本使用
1 Conda 介紹
Conda 是一個跨平台的套件管理與環境管理系統(package & environment manager),廣泛用於安裝和管理 Python、R、生物資訊工具(如 samtools、fastp、bwa)以及資料科學套件。 它能為每個專案建立獨立的軟體環境,避免不同工具之間的版本衝突,並大幅提升分析流程的 可重現性(reproducibility)。
Conda 本身是一個 CLI 工具(Command-Line Interface tool),可透過指令執行套件安裝、環境建立、版本管理與環境切換。本篇文章將整理常用的 Conda 指令與實務操作,詳細指令請見 官方文檔。
此外,Conda 也有一個速度更快、完全相容的替代指令工具 —— mamba。 mamba 使用 C++ 撰寫的求解器(solver),在處理大型生物資訊工具依賴時速度可達到 Conda 的 10–100 倍。本篇內容主要以 Conda 命令為基礎,但實務上強烈建議在生物資訊流程中使用 mamba 執行環境建立與套件安裝,詳細介紹請見 mamba 教學 。
2 Conda Channels 介紹
現代生物資訊分析最佳實踐為:直接使用 conda-forge 與 bioconda 兩個 Channels。
Conda 透過 channel(套件來源庫) 取得各種可安裝的工具與套件。對生物資訊分析而言,最重要的三個 channel 如下:
| Channel 名稱 | 來源/維護者 | 主要用途 | 特點 | 是否建議使用 |
|---|---|---|---|---|
| conda-forge | 社群維護 | Python、R、生信依賴、系統函式庫 | 套件最多、更新快、依賴最乾淨 | ⭐ 強烈推薦(第一順位) |
| bioconda | Bioconda 專案 | 生物資訊工具(samtools/fastp/bwa…) | 建立在 conda-forge 之上、專門為生信設計 | ⭐ 推薦(第二順位) |
| defaults | Anaconda 官方 | 一般 Python/R 套件 | 更新慢、相依性常與生信工具衝突 | ❌ 不建議使用 |
3 Conda 發行版整理
現代生信最佳實踐為:直接使用 Miniforge(內含 mamba)。
| 名稱 | 來源/維護者 | 是否包含 mamba | 預設 Channel | 特點 | 適用情境 |
|---|---|---|---|---|---|
| Anaconda | Anaconda 官方 | ✖ | defaults | 體積大,預裝大量 Python 科學套件;更新慢、依賴較複雜,不適合生信 | 初學者入門、教學示範。生信不推薦 |
| Miniconda | Anaconda 官方 | ✖ | defaults | 精簡 Conda,但需手動設定 conda-forge/bioconda;易與 defaults 衝突 | 想自行管理 channels 的使用者 |
| Miniforge(推薦) | conda-forge 社群 | ✔(內建 mamba) | conda-forge | 輕量、乾淨、無 defaults;包含 mamba,適合大型生信環境 | ⭐ 生信最佳選擇、Snakemake/nf-core、重現性要求高的專案 |
4 conda –version (查看版本)
使用 --version 或 -V 參數確認是否成功安裝,並顯示版本號。
conda --version
conda -Vconda 25.11.0
conda 25.11.0
5 conda create (建立新環境)
-n 代表創建環境名稱, -y 代表直接確認下載 (yes), -f 是透過指定 YAML file 建立環境
conda create -n myenv python=3.10 -y6 conda activate (啟動環境)
需先進入環境才能使用該環境安裝之套件
conda activate myenv7 conda install (安裝套件)
於該環境內安裝套件會獨立於其他環境
conda install numpy -y8 conda remove (移除套件)
移除該環境的套件,-y 表示直接確認,就不用再終端確認並輸入 yes
conda remove numpy -y9 conda deactivate (離開環境)
回到 base 環境
conda deactivate10 conda env (環境相關指令)
與 conda 環境相關的指令,-h 有詳細解釋相關用法
conda env -h10.1 conda env export (匯出環境至YAML)
將環境 YAML 匯出可以進入版本控制,也能夠後續透過該 YAML 重建一樣的環境
conda env export > output.yaml10.2 conda env create (透過YAML建立環境)
環境名稱會是 YAML 的 name 參數
conda env create -f ./conda/envs.yaml10.3 conda env remove (刪除環境)
-n 指定待刪除環境名稱
conda env remove -n myenv -y10.4 conda env list (列出所有環境)
* 表示當前環境
conda env list# conda environments:
#
# * -> active
# + -> frozen
base /home/benson/miniforge3
myenv /home/benson/miniforge3/envs/myenv
quarto-test-env * /home/benson/miniforge3/envs/quarto-test-env
11 conda list (查看下載之套件)
11.1 conda list –show-channel-urls (查看套件的來源)
會匯出該環境套件之版本號、build string 與 Channel來源。 build string 表示該套件是用什麼編譯器、什麼設定、在什麼平台下編譯出來的版本。
conda list --show-channel-urlsClick to expand
# packages in environment at /home/benson/miniforge3/envs/quarto-test-env:
#
# Name Version Build Channel
_libgcc_mutex 0.1 conda_forge conda-forge
_openmp_mutex 4.5 2_gnu conda-forge
_r-mutex 1.0.1 anacondar_1 conda-forge
alsa-lib 1.2.15.1 hb03c661_0 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 py310h7c4b9e2_2 conda-forge
arrow 1.4.0 pyhcf101f3_0 conda-forge
asttokens 3.0.1 pyhd8ed1ab_0 conda-forge
async-lru 2.0.5 pyh29332c3_0 conda-forge
11.2 conda list –explicit (查看套件的詳細url)
詳細來源也可以使用 --explicit 輸出 url,基本上能 100% 重建環境
conda list --explicitClick to expand
# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: linux-64
# created-by: conda 25.11.0
@EXPLICIT
https://conda.anaconda.org/conda-forge/linux-64/_libgcc_mutex-0.1-conda_forge.tar.bz2
https://conda.anaconda.org/conda-forge/noarch/_r-mutex-1.0.1-anacondar_1.tar.bz2
https://conda.anaconda.org/conda-forge/noarch/ca-certificates-2025.11.12-hbd8a1cb_0.conda
https://conda.anaconda.org/conda-forge/linux-64/dart-sass-1.59.1-ha770c72_0.conda
https://conda.anaconda.org/conda-forge/linux-64/esbuild-0.27.2-hfc2019e_0.conda
https://conda.anaconda.org/conda-forge/noarch/font-ttf-dejavu-sans-mono-2.37-hab24e00_0.tar.bz2
https://conda.anaconda.org/conda-forge/noarch/font-ttf-inconsolata-3.000-h77eed37_0.tar.bz2
https://conda.anaconda.org/conda-forge/noarch/font-ttf-source-code-pro-2.038-h77eed37_0.tar.bz2
https://conda.anaconda.org/conda-forge/noarch/font-ttf-ubuntu-0.83-h77eed37_3.conda
https://conda.anaconda.org/conda-forge/noarch/kernel-headers_linux-64-5.14.0-he073ed8_3.conda
11.3 Tips: 輸出當前環境
完整記錄當前執行的 Conda 環境,有助於分析重現、版本追蹤與錯誤排查。
echo "======================================="
echo "Conda Environment: $(conda env list | grep '*' | awk '{print $1}')"
echo "======================================="
conda list=======================================
Conda Environment: #
quarto-test-env
=======================================
# packages in environment at /home/benson/miniforge3/envs/quarto-test-env:
#
# Name Version Build Channel
_libgcc_mutex 0.1 conda_forge conda-forge
_openmp_mutex 4.5 2_gnu conda-forge
_r-mutex 1.0.1 anacondar_1 conda-forge
alsa-lib 1.2.15.1 hb03c661_0 conda-forge
anyio 4.12.0 pyhcf101f3_0 conda-forge
12 conda –help (詳細指令)
顯示 conda 的主要參數與使用說明,適合作為快速查詢用途。
conda --helpClick to expand
usage: conda [-h] [-v] [--no-plugins] [-V] COMMAND ...
conda is a tool for managing and deploying applications, environments and packages.
options:
-h, --help Show this help message and exit.
-v, --verbose Can be used multiple times. Once for detailed output, twice for INFO logging, thrice for DEBUG logging, four times for TRACE logging.
--no-plugins Disable all plugins that are not built into conda.
-V, --version Show the conda version number and exit.
commands:
The following built-in and plugins subcommands are available.
COMMAND
activate Activate a conda environment.
clean Remove unused packages and caches.
commands List all available conda subcommands (including those from plugins). Generally only used by tab-completion.
compare Compare packages between conda environments.
config Modify configuration values in .condarc.
create Create a new conda environment from a list of specified packages.
deactivate Deactivate the current active conda environment.
doctor Display a health report for your environment.
env Create and manage conda environments.
export Export a given environment
info Display information about current conda install.
init Initialize conda for shell interaction.
install Install a list of packages into a specified conda environment.
list List installed packages in a conda environment.
menuinst A subcommand for installing and removing shortcuts via menuinst.
notices Retrieve latest channel notifications.
package Create low-level conda packages. (EXPERIMENTAL)
remove (uninstall) Remove a list of packages from a specified conda environment.
rename Rename an existing environment.
repoquery Advanced search for repodata.
run Run an executable in a conda environment.
search Search for packages and display associated information using the MatchSpec format.
update (upgrade) Update conda packages to the latest compatible version.