🇨🇳 中文

Conda Guide: How to Manage Multiple Python Versions and Environments

Learn how to use Conda for Python environment management. This guide covers Conda vs pip, Anaconda vs Miniconda, creating and managing environments, essential commands, and best practices for juggling multiple Python versions.

Bruce

PythonCondaAnacondaMinicondaEnvironment ManagementVersion Control

Python

1532  Words

2020-01-11


If you have ever worked on one project that requires Python 2.7 and another that needs Python 3.10, you know how painful version conflicts can be. Conda solves this problem once and for all. This guide walks you through everything you need to know about Conda — from installation to daily workflow.

What Is Conda?

Conda official logo, an open-source package and environment management system

Conda is an open-source package manager and environment manager. It was originally built for Python, but it actually supports any language. Its core capabilities include:

  • Package management — install, update, and remove software packages
  • Dependency resolution — automatically handle inter-package dependencies
  • Environment isolation — create fully independent development environments
  • Cross-platform — works on Windows, macOS, and Linux

Think of it this way: Conda = pip (package management) + virtualenv (virtual environments) + non-Python dependency management

Conda vs pip: Which Should You Use?

FeaturepipConda
PurposePython package installerGeneral-purpose package and environment manager
ScopePython packages onlyPython packages + other languages (e.g., C/C++ libraries)
Package sourcePyPIAnaconda repository / conda-forge
Package formatWheels / source distributionsPre-compiled binaries
Dependency handlingInstalls serially; conflicts possibleSAT solver checks all dependencies globally
Environment managementRequires virtualenv / venvBuilt-in
Python interpreterMust be pre-installedCan install any Python version directly

When to use what:

  • Data science or ML projects → Conda (complex native dependencies)
  • Pure Python web development → pip + venv is usually enough
  • Need to switch between multiple Python versions → Conda is more convenient
  • A package only exists on PyPI → use pip inside a Conda environment

Anaconda vs Miniconda: How to Choose

Anaconda: The Batteries-Included Distribution

Anaconda icon, a Python distribution with 250+ pre-installed packages

Anaconda is the full-size Conda distribution:

  • 250+ pre-installed packages: NumPy, Pandas, Matplotlib, Scikit-learn, Jupyter, and more
  • Graphical interface: Anaconda Navigator for visual environment and package management
  • Download size: roughly 500 MB – 3 GB

Best for:

  • Python / data science beginners
  • Anyone who wants a ready-to-go analytics environment
  • Users who prefer a GUI

Miniconda: The Minimal Install

Miniconda is the stripped-down Conda distribution:

  • Only the essentials: Conda + Python + a handful of dependencies
  • Download size: about 50 MB
  • Purely command-line driven

Best for:

  • Developers comfortable with the terminal
  • Custom environment setups
  • Server deployments (save disk space)
  • Anyone who wants full control over installed packages

Quick Comparison

ScenarioRecommendation
Just getting startedAnaconda
Personal machine with plenty of storageAnaconda
Server or CI deploymentMiniconda
Only need a few specific packagesMiniconda
Want full control over the environmentMiniconda

Commercial use note: Anaconda requires a paid license for organizations with more than 200 employees. To avoid licensing issues, use Miniconda with the conda-forge channel instead.

Installing Conda

macOS / Linux

# Download the installer (macOS Apple Silicon)
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh

# Download the installer (Linux x86_64)
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

# Run the installer
bash Miniconda3-latest-*.sh

# Follow the prompts — say yes when asked to initialize Conda

After installation, restart your terminal or run:

source ~/.bashrc   # Bash
source ~/.zshrc    # Zsh

Windows

  1. Download the Miniconda Windows installer
  2. Double-click the installer and follow the wizard
  3. Check “Add Miniconda to PATH” when prompted

Option 2: Install Anaconda

Visit the Anaconda website, download the installer for your OS, and follow the setup wizard.

Verify the Installation

conda --version
# Example output: conda 24.x.x

conda info
# Displays detailed Conda configuration

Environment Management: The Core Skill

Creating Environments

# Create an environment named myenv with a specific Python version
conda create -n myenv python=3.10

# Create an environment with packages pre-installed
conda create -n datascience python=3.11 numpy pandas jupyter

# Clone an existing environment
conda create -n myenv_copy --clone myenv

Activating and Deactivating

# Activate an environment
conda activate myenv

# Deactivate and return to base
conda deactivate

# Switch directly to a different environment
conda activate another_env

Once activated, your shell prompt shows the environment name, e.g., (myenv) $

Listing Environments

# List all environments
conda env list
# or
conda info --envs

# Example output:
# base                  *  /Users/bruce/miniconda3
# myenv                    /Users/bruce/miniconda3/envs/myenv
# datascience              /Users/bruce/miniconda3/envs/datascience

Removing Environments

# Remove an environment and all its packages
conda remove -n myenv --all

# Verify it's gone
conda env list

Exporting and Importing Environments

This is invaluable for team collaboration and machine migration:

# Export the current environment to a YAML file
conda activate myenv
conda env export > environment.yml

# Recreate an environment from a YAML file
conda env create -f environment.yml

# Update an existing environment from a YAML file
conda env update -f environment.yml

Example environment.yml:

name: myenv
channels:
  - conda-forge
  - defaults
dependencies:
  - python=3.10
  - numpy=1.24
  - pandas>=2.0
  - pip
  - pip:
    - requests
    - beautifulsoup4

Package Management: Daily Operations

Searching for Packages

# Search for a package
conda search numpy

# Show detailed package info
conda search numpy --info

Installing Packages

# Install into the current environment
conda install numpy

# Install a specific version
conda install numpy=1.24.0

# Install multiple packages at once
conda install numpy pandas matplotlib

# Install from a specific channel
conda install -c conda-forge pytorch

# Install into a named environment without activating it
conda install -n myenv numpy

Updating Packages

# Update a specific package
conda update numpy

# Update all packages in the current environment
conda update --all

# Update Conda itself
conda update conda

Removing Packages

# Remove a package
conda remove numpy

# Remove from a specific environment
conda remove -n myenv numpy

Listing Installed Packages

# List everything in the current environment
conda list

# Filter for a specific package
conda list | grep numpy

# Export the package list
conda list --export > packages.txt

Configuration Tips

Add conda-forge as the Default Channel

The community-driven conda-forge channel is often more up-to-date than the default Anaconda channel:

# Add conda-forge with highest priority
conda config --add channels conda-forge

# Always show channel URLs (useful for debugging)
conda config --set show_channel_urls yes

# Check current channel configuration
conda config --show channels

You can also edit ~/.condarc directly:

channels:
  - conda-forge
  - defaults
show_channel_urls: true

Other Useful Settings

# Don't activate the base environment on shell startup (recommended)
conda config --set auto_activate_base false

# Set a custom path for environments (optional)
conda config --add envs_dirs /path/to/custom/envs

# Free up disk space by clearing cached packages
conda clean --all

Practical Example: Switching Between Python Versions

Suppose you maintain projects that target both Python 2.7 and Python 3.10:

# Create a Python 2.7 environment (note: Python 2 is end-of-life)
conda create -n py27 python=2.7

# Create a Python 3.10 environment
conda create -n py310 python=3.10

# Switch to Python 2.7
conda activate py27
python --version  # Python 2.7.18

# Switch to Python 3.10
conda activate py310
python --version  # Python 3.10.x

# Check which Python binary is active
which python

Mixing Conda and pip

Some packages are only available on PyPI. Here is the recommended way to use pip inside a Conda environment:

# 1. Install as much as you can with Conda first
conda install numpy pandas scikit-learn

# 2. Use pip for packages not in Conda channels
pip install some-pypi-only-package

# 3. Export the full environment (includes pip packages)
conda env export > environment.yml

Important: Avoid installing the same package with both conda install and pip install — this can lead to version conflicts and broken environments.

Command Cheat Sheet

TaskCommand
Check Conda versionconda --version
Show configurationconda info
Create environmentconda create -n ENV python=3.10
Activate environmentconda activate ENV
Deactivate environmentconda deactivate
List all environmentsconda env list
Remove environmentconda remove -n ENV --all
Export environmentconda env export > env.yml
Import environmentconda env create -f env.yml
Install packageconda install PACKAGE
Update packageconda update PACKAGE
Remove packageconda remove PACKAGE
List installed packagesconda list
Search for packageconda search PACKAGE
Clear cacheconda clean --all

Troubleshooting

Conda Is Slow

  1. Use conda-forge as your primary channel (see the Configuration section above)
  2. Try Mamba, a drop-in replacement for Conda with much faster dependency solving:
conda install -c conda-forge mamba
mamba install numpy  # Same syntax, up to 10x faster

Dependency Conflicts

# Preview what would change without actually installing
conda install package --dry-run

# When in doubt, start with a fresh environment
conda create -n fresh_env python=3.10 package1 package2

conda activate Does Not Work

Make sure Conda’s shell integration is set up:

conda init bash   # or zsh / fish / powershell
# Then restart your terminal

Reclaiming Disk Space

# Remove cached packages and index files
conda clean --all

# Delete environments you no longer use
conda remove -n old_env --all

Summary

Conda is the go-to tool for managing multiple Python versions and isolated environments. Here are the key takeaways:

  1. Conda vs pip — Conda shines for data science projects and multi-version Python management
  2. Anaconda vs Miniconda — beginners benefit from Anaconda; experienced developers prefer Miniconda
  3. Environment managementcreate, activate, and export are your most-used commands
  4. Channel configuration — adding conda-forge gives you access to a larger, more current package catalog
  5. Mixing pip — always install Conda packages first, then fill gaps with pip

For more tips on building an efficient terminal workflow, check out the Oh My Zsh configuration guide.

References

Comments

Join the discussion — requires a GitHub account