Conda Cheat Sheet: Create, Remove, and Export Environments

Conda cheat sheet for data science: conda create -n myenv python=3.10, conda remove --name myenv --all, install packages, manage channels, and export environment.yml.

A quick reference for the conda commands I use most, written for my data science students. It covers creating and removing environments, installing packages, channels, and sharing environments with environment.yml.

Install conda

Why use conda

  • Separate environments per project. Each project gets its own Python version and packages, so upgrading one project never breaks another.
  • Works the same on Windows, macOS, and Linux. An environment.yml file recreates the same setup on a teammate’s machine.
  • More than Python packages. Conda can also install R, system libraries, and compiled tools that pip can’t.

Key terms

  • Environment: an isolated folder with its own Python and packages.
  • Channel: a source of packages, such as defaults or conda-forge.
  • environment.yml: a text file listing an environment’s packages, used to recreate it.

Commands

Check your setup

conda info                    # Show conda configuration and paths
conda --version               # Show the installed conda version
conda update conda            # Update Conda itself

Environments

conda create --name myenv         # Create a new environment (-n is short for --name)
conda create -n myenv python=3.10 # Create an environment with a specific Python version
conda activate myenv              # Activate an environment
conda deactivate                  # Deactivate current environment
conda env list                    # List all environments
conda remove --name myenv --all   # Delete an environment

Packages

conda list                    # List installed packages
conda install packagename     # Install a package
conda update packagename      # Update a specific package
conda update --all            # Update all packages
conda remove packagename      # Remove a package

Channels (package sources)

conda config --show channels                # Show configured channels
conda config --add channels channelname     # Add a channel
conda install -c channelname packagename    # Install from specific channel

Share an environment

conda env export > environment.yml                    # Export full environment
conda env export --from-history > environment.yml     # Export Minimal environment
conda env create -f environment.yml                   # Import environment from file

Specific versions and cleanup

conda install python=3.10             # Install specific Python version
conda install "package>2.5,<3.2"      # Version range installation
conda clean --all                     # Remove unused packages and caches

Common Workflows

Starting a New Project

conda create --name projectname python=3.10   # Create environment
conda activate projectname                    # Activate it
conda install required-packages               # Install needs
conda env export > environment.yml            # Save configuration

Sharing a Project

conda env export --from-history > environment.yml   # For cross-platform sharing
conda env export > environment.yml                  # For exact reproduction

Troubleshooting Tips

  • Use conda list to verify installed packages
  • Check channel priorities with conda config --show
  • Clear package caches with conda clean --all
  • Review environment history with conda list --revisions
  • Restore previous states with conda install --revision

Best Practices & Tips

PyTorch no longer publishes conda packages on its own channel. Maintaining conda builds cost too much compared to how few people downloaded them versus PyPI. As part of the deprecation timeline, PyTorch will stop providing nightly builds for its core and domain libraries starting November 18, 2024 PyTorch Issue #138506.

To accommodate users affected by this change, PyTorch recommends switching to its official wheel packages available on download.pytorch.org or PyPI, which are actively supported. For users who prefer conda, PyTorch suggests transitioning to the pytorch-cpu or pytorch-gpu packages available through conda-forge. If you currently depend on the deprecated binaries, it is advised to migrate to pip wheels, which offer equivalent functionality and are more sustainable to maintain PyTorch Discussion: Deprecation of Conda Nightly Builds.

Source: [Twitter](https://x.com/PyTorch/status/1857500664831635882)
Source: Twitter
Feature Anaconda Distribution Miniconda
Created by Anaconda Anaconda
Has conda Yes Yes
Has Anaconda Navigator Yes No
Number of packages 300+ < 70
Install space required ~4.4 GB ~480 MB
  • Name Environments Meaningfully: Use descriptive names for easy identification
  • One Environment Per Project: Create separate environments for different projects
  • Export Regularly: Keep environment files in version control
  • Update Strategically: Test updates in a clone of your environment first (conda create --name myenv-test --clone myenv)
  • Use Environment Files: Store environment configurations in version control

My recommendation

Use Miniconda to create environments (it works well with VS Code), then install packages with pip inside the environment. Pick one installer per environment: mixing conda and pip installs of the same packages can cause conflicts.