How to Export a Conda Environment to a YAML File
Exporting a Conda environment to YAML is the standard way to share Python environments, reproduce analysis pipelines, and deploy consistent development setups across machines and teams. The exported YAML file captures every package, version, and channel in your environment — making it fully reproducible on any system with Conda installed.
The Basic conda env export Command
Activate the environment you want to export, then run:
conda activate myenv
conda env export > environment.yaml
This creates an environment.yaml file in your current directory containing the environment name, all channels, and every installed package with its exact version and build string. The file can be shared with teammates or committed to version control.
What the Exported YAML File Looks Like
name: myenv
channels:
- conda-forge
- defaults
dependencies:
- python=3.11.4=h7a1cb2a_0
- numpy=1.25.2=py311h64a7726_0
- pandas=2.0.3=py311h320fe9a_1
- pip:
- scikit-learn==1.3.0
- requests==2.31.0
The file includes both Conda-installed packages and pip-installed packages (under the pip: section). Channels are listed in priority order. Each package has its full version and build hash, ensuring exact reproducibility.
Export for Cross-Platform Sharing: –from-history Flag
The default export includes platform-specific build strings that will fail on a different operating system. If you are sharing an environment between Mac, Linux, and Windows, use the --from-history flag instead:
conda env export --from-history > environment.yaml
This exports only the packages you explicitly installed — not their full dependency tree — and omits platform-specific build strings. The resulting file is smaller and portable across operating systems. The trade-off is that exact sub-dependency versions are not pinned, so the recreated environment may differ slightly from the original.
When should I use –from-history vs the default export?
Use the default conda env export when you need exact reproducibility on the same platform — for example, pinning a data science environment used for a specific analysis or model training run. Use --from-history when sharing environments across different operating systems or when you want a simpler, more maintainable environment file that allows Conda to resolve the best compatible versions for the target platform.
Export Without Build Strings for Cleaner YAML
If you want version-pinned but platform-portable output, combine the --no-builds flag with the standard export:
conda env export --no-builds > environment.yaml
This keeps exact package versions but removes the build hash strings, producing a cleaner YAML file that works across platforms while still pinning all versions explicitly.
Recreating an Environment from the Exported YAML
conda env create -f environment.yaml
Conda will create a new environment with the name specified in the YAML file and install all listed packages. To update an existing environment from a YAML file instead of creating a new one:
conda env update -f environment.yaml --prune
The --prune flag removes packages that are in the existing environment but not in the YAML file, keeping the environment in sync with the specification.
Working with the Exported YAML File
The exported YAML is a standard YAML file you can open, edit, and validate with any text editor or YAML tool. Format Pilot’s converter can format and validate the YAML if it has indentation issues after manual editing. Common edits to the environment YAML before sharing include removing the prefix: line at the bottom (which contains your local installation path and is irrelevant on other machines) and adjusting channel priorities.
Frequently Asked Questions
Why does conda env export include so many packages I didn’t install?
Conda resolves and installs all dependencies automatically when you install a package. The full export includes every package in the environment — your explicitly installed ones plus all their dependencies. This ensures exact reproducibility. If you only want the packages you installed directly, use conda env export --from-history.
Can I export a Conda environment without activating it?
Yes. Use the -n flag to specify the environment by name without activating it: conda env export -n myenv > environment.yaml. This is useful in scripts and CI/CD pipelines where you cannot interactively activate environments.
Should the environment.yaml file be committed to version control?
Yes — committing environment.yaml to your Git repository is best practice for data science and ML projects. It ensures any team member or CI system can recreate the exact environment. For cross-platform projects, use --from-history or --no-builds to keep the file portable. Add the full-export file (with build strings) as a separate file like environment-lock.yaml if you also need platform-specific reproducibility.