Locking Anaconda Environments with environment.yml for Reproducible Builds
Learn how to lock an Anaconda environment using environment.yml see a worked example and understand the limits of this reproducibility method.
27 Aug 2025, 15:19 UTC

Problem: Ensuring the Same Packages Across Machines
When you share a project or move to a new workstation reinstalling packages with conda install can lead to different versions or builds breaking reproducibility.
How Conda Locks an Environment
Running conda env export writes an environment.yml file that records each package name version build string and the channel order active at export time. The build string (for example py38h0b83bbe_0) ties the package to a specific compile so reinstalling from the file attempts to fetch that exact build.
Worked Example: Locking and Restoring a NumPy Version
- Create a fresh environment and install a specific NumPy version:
conda create -n lock-demo python=3.9 conda activate lock-demo conda install numpy=1.21.0
- Export the locked specification:
conda env export > environment.yml
- Inspect the file you should see a line similar to:
- numpy=1.21.0=py38h0b83bbe_0
- Remove the environment and recreate it from the file:
conda deactivate conda env remove -n lock-demo conda env create -f environment.yml
- Activate the new environment and compare the installed builds:
conda activate lock-demo conda list numpy
The output should show the same build string as in the exported file.
Trade‑off: Platform Changes and Strict Channel Priority
If you move to a different operating system or a newer version of a dependency the exact build recorded may not be available. Conda will then try to find a compatible build which can introduce subtle differences. Enabling strict channel priority with conda config --set channel_priority strict reduces the chance of silently pulling a package from a lower‑priority channel but it can also cause the environment creation to fail when a required package exists only in a lower‑priority channel.
Actionable Closing
To get the most reliable reproducibility:
- Always export from a clean environment that contains only the packages you need.
- Commit the generated environment.yml to version control.
- When sharing recreate with conda env create -f environment.yml and verify with conda list or conda env export against the original file.
- If you encounter missing builds edit the file to remove the problematic line or use --prune to drop packages that cannot be satisfied.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.