Conda Create Environment from YML

Complete guide to creating Conda environments from environment.yml files

Last updated: November 2024 8 min read

Introduction

Conda environments can be created from environment.yml files, which are YAML files that specify the packages and dependencies for a project. This allows you to easily recreate environments across different machines and share them with team members.

Basic Command

The basic command to create a Conda environment from a YML file:

# Create environment from environment.yml
conda env create -f environment.yml

# Or specify a custom name
conda env create -f environment.yml -n myenv

Command Options

  • -f, --file - Path to the environment.yml file
  • -n, --name - Name for the environment (overrides name in file)
  • --prefix - Full path to environment location
  • --force - Remove existing environment if it exists

environment.yml Structure

A typical environment.yml file structure:

name: myproject
channels:
  - defaults
  - conda-forge
dependencies:
  - python=3.9
  - numpy=1.21.0
  - pandas=1.3.0
  - pip
  - pip:
    - some-pip-package==1.0.0

Key Components

  • name - Name of the environment
  • channels - Conda channels to search for packages
  • dependencies - List of packages to install
  • pip - Subsection for pip-installable packages

Complete Example

name: data-science-env
channels:
  - defaults
  - conda-forge
  - pytorch
dependencies:
  - python=3.10
  - numpy=1.23.0
  - pandas=1.5.0
  - matplotlib=3.6.0
  - scikit-learn=1.1.0
  - jupyter
  - pip=22.3.0
  - pip:
    - tensorflow==2.10.0
    - opencv-python==4.6.0.66
    - requests==2.28.0

Step-by-Step Guide

Step 1: Create or obtain environment.yml

Create an environment.yml file in your project directory:

# Create environment.yml file
cat > environment.yml << EOF
name: myproject
channels:
  - conda-forge
dependencies:
  - python=3.9
  - numpy
  - pandas
EOF

Step 2: Create the environment

# Create environment from file
conda env create -f environment.yml

# Output will show progress and package installation

Step 3: Activate the environment

# Activate the environment
conda activate myproject

# On Windows, you may need:
# conda activate myproject
# Or: activate myproject

Step 4: Verify installation

# List installed packages
conda list

# Check Python version
python --version

# Verify specific packages
python -c "import numpy; print(numpy.__version__)"

Advanced Usage

Update Existing Environment

Update an existing environment from a modified YML file:

# Update environment from file
conda env update -f environment.yml --prune

# --prune removes packages not in the file

Export Current Environment

Create an environment.yml from your current environment:

# Export current environment
conda env export > environment.yml

# Export without build numbers (more portable)
conda env export --no-builds > environment.yml

# Export only explicitly installed packages
conda env export --from-history > environment.yml

Remove and Recreate

Remove existing environment and create fresh one:

# Remove existing environment
conda env remove -n myproject

# Create new environment
conda env create -f environment.yml

# Or use --force flag
conda env create -f environment.yml --force

Specify Custom Location

# Create environment at custom path
conda env create -f environment.yml --prefix /path/to/env

# Activate using prefix
conda activate /path/to/env

Common Issues and Solutions

Issue: Environment already exists

Remove existing environment first or use --force:

# Remove and recreate
conda env remove -n myproject
conda env create -f environment.yml

# Or force overwrite
conda env create -f environment.yml --force

Issue: Package conflicts

Resolve dependency conflicts:

# Use conda-forge channel for better compatibility
channels:
  - conda-forge
  - defaults

# Or specify compatible versions
dependencies:
  - python=3.9
  - numpy=1.21.*  # Use flexible version

Issue: YAML syntax errors

Validate YAML syntax before creating environment:

# Validate YAML syntax
python -c "import yaml; yaml.safe_load(open('environment.yml'))"

# Or use online YAML validators
# Check indentation and formatting

Best Practices

  • Pin major versions - Specify Python version and major package versions for reproducibility
  • Use conda-forge - Include conda-forge channel for better package availability
  • Separate pip packages - List pip-installable packages under the pip subsection
  • Export with --from-history - When sharing, export only explicitly installed packages
  • Version control - Commit environment.yml to your repository for team consistency

Related Articles