conda-lock - Reproducible Conda Environment Locking Cheatsheet
conda-lock löst Conda’s Reproduzierbarkeit-Problem. Ein environment.yml listet angefordert Packages auf, sodass das Solving davon auf zwei Maschinen eine Woche auseinander unterschiedliche Versionen erzeugen kann — der Klassische “Works auf meiner Maschine.” conda-lock löst die Environment einmal auf und schreibt eine Lock-Datei mit Exakten Versionen, Builds und Hashes für jede Platform, die du targetest, sodass Installs Deterministisch und Schnell werden (kein Solving bei Install-Zeit).
Installation
| Methode | Befehl |
|---|
| Pipx (Empfohlen) | pipx install conda-lock |
| Conda | conda install -c conda-forge conda-lock |
| Pip | pip install conda-lock |
| Verifikation | conda-lock --version |
Generating eine Lock-Datei
# Lock für Multiple Platforms von einem environment.yml
conda-lock -f environment.yml -p linux-64 -p osx-arm64 -p win-64
| Flag | Zweck |
|---|
-f, --file | Input Spec (environment.yml, pyproject.toml, meta.yaml) |
-p, --platform | Target Platform (Wiederholbar) |
-k, --kind | Output Kind: lock (Unified), explicit, env |
--lockfile | Output Path (Standard conda-lock.yml) |
--check-input-hash | Skip Re-Solving wenn Inputs Unchanged |
Lock-Datei Kinds
| Kind | Produces | Nutze |
|---|
lock | Unified conda-lock.yml (alle Platforms) | Commit das zu Git |
explicit | Per-Platform .lock Dateien | conda create --file |
env | Rendered environment.yml | Tools, die ein Plain Env File erwarten |
# Render Per-Platform Explizite Dateien aus der Unified Lock
conda-lock render -p linux-64
Installieren aus einer Lock
# Erstelle eine Environment genau wie gelock
conda-lock install --name myenv conda-lock.yml
# Oder mit Micromamba (Schnell)
micromamba create -n myenv -f conda-linux-64.lock
| Befehl | Beschreibung |
|---|
conda-lock install -n NAME | Create/Update die Env aus einer Lock |
conda-lock install --mamba | Nutze Mamba als die Solver/Installer |
conda create --file X.lock | Installiere eine Explizite Lock direkt |
Updating Dependencies
# Re-Solve Alles (Neue Versionen erlaubt)
conda-lock -f environment.yml -p linux-64 -p osx-arm64
# Update Nur ein Package, keep der Rest Pinned
conda-lock --update numpy -f environment.yml -p linux-64
| Task | Befehl |
|---|
| Full Refresh | Re-Run conda-lock |
| Targeted Bump | --update PKG |
| Verify Inputs Unchanged | --check-input-hash |
Including Pip Dependencies
# environment.yml
name: myproject
channels: [conda-forge]
dependencies:
- python=3.12
- numpy
- pip
- pip:
- some-pypi-only-package==1.2.3
Conda-Lock löst beide Conda und Pip Dependencies in die selbe Lock-Datei, welche wichtig ist, weil das Mixing der zwei ad hoc eine Common Source von Gebrochenen Environments ist.
CI Pattern
# Deterministic, Fast CI Installs
- run: conda-lock install --name ci conda-lock.yml
- run: conda run -n ci pytest
| Praxis | Warum |
|---|
Commit conda-lock.yml | Die Lock ist die Source of Truth |
| Lock alle Target Platforms | Devs auf macOS, CI auf Linux |
| Regenerate Deliberately | Dependency Updates werden Review-able Diffs |
| Cache die Env | Locks machen Caching Reliable |
conda-lock vs Alternativen
| Aspekt | Conda-Lock | Environment.yml | Pixi | Uv |
|---|
| Reproducible | Exact Pins + Hashes | Nein | Built-in Lock | Built-in Lock |
| Conda Ecosystem | Ja | Ja | Ja | Nein (PyPI) |
| Multi-Platform Lock | Ja | N/A | Ja | Ja |
| Am besten für | Locking Existierender Conda Projekte | Simple/Ad-hoc Envs | Neue Conda-based Projekte | Pure-Python Projekte |
Für Neue Projekte consider Pixi (Locking Built-in) oder Uv für Pure-Python; conda-lock Retrofits Reproducibility zu Existierenden Conda Workflows.
Ressourcen