Conda Guide¶
Conda is a powerful package and environment management system, particularly popular in data science and scientific computing. It can manage both Python packages and system-level dependencies.
What is Conda?¶
Conda is an open-source package management system and environment management system that runs on Windows, macOS, and Linux. It can:
- Install packages and their dependencies
- Create isolated environments
- Manage multiple Python versions
- Handle binary packages (not just Python)
- Work with pip and other package managers
Installation¶
Install Miniconda (Recommended)¶
Miniconda is a minimal installer that includes conda, Python, and a few essential packages:
# Download Miniconda for macOS (Apple Silicon)
cd ~/Downloads
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh
# Download Miniconda for macOS (Intel)
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh
# Run installer
bash Miniconda3-latest-MacOSX-arm64.sh # or x86_64.sh for Intel
# Follow prompts:
# - Press Enter to review license
# - Type 'yes' to accept
# - Press Enter to confirm installation location
# - Type 'yes' to initialize conda
Install Anaconda (Full Distribution)¶
Anaconda includes conda plus 150+ pre-installed packages:
# Download from: https://www.anaconda.com/products/distribution
# Or use Homebrew:
brew install --cask anaconda
Initialize Conda¶
After installation, restart your terminal or run:
Verify Installation¶
Basic Usage¶
Creating Environments¶
# Create environment with Python 3.10
conda create -n myenv python=3.10
# Create environment with specific packages
conda create -n myenv python=3.10 numpy pandas matplotlib
# Create environment from file
conda env create -f environment.yml
Activating and Deactivating¶
# Activate environment
conda activate myenv
# Deactivate environment
conda deactivate
# Activate base environment
conda activate base
Installing Packages¶
# Install a package
conda install numpy
# Install multiple packages
conda install numpy pandas matplotlib
# Install from specific channel
conda install -c conda-forge package-name
# Install specific version
conda install numpy=1.24.0
Listing and Searching¶
# List all environments
conda env list
# or
conda info --envs
# List packages in current environment
conda list
# List packages in specific environment
conda list -n myenv
# Search for packages
conda search numpy
# Search in specific channel
conda search -c conda-forge package-name
Removing Environments and Packages¶
# Remove a package
conda remove numpy
# Remove environment
conda env remove -n myenv
# Remove all unused packages
conda clean --all
Best Practices¶
1. Use Environments for Each Project¶
Always create a separate environment for each project:
# Good: Project-specific environment
conda create -n project1 python=3.10
conda activate project1
conda install project-dependencies
# Bad: Installing everything in base
conda install everything
2. Use Environment Files¶
Create environment.yml files for reproducible environments:
name: my-project
channels:
- conda-forge
- bioconda
- defaults
dependencies:
- python=3.10
- numpy>=1.20.0
- pandas>=1.3.0
- matplotlib>=3.4.0
- pip
- pip:
- some-pip-only-package
Usage:
# Create from file
conda env create -f environment.yml
# Update existing environment
conda env update -f environment.yml --prune
# Export current environment
conda env export > environment.yml
# Export without build numbers (more portable)
conda env export --no-builds > environment.yml
3. Use Channels Wisely¶
Channels are repositories where conda looks for packages:
# Add channels (order matters - first checked first)
conda config --add channels conda-forge
conda config --add channels bioconda
# List channels
conda config --show channels
# Install from specific channel
conda install -c conda-forge package-name
# Set channel priority
conda config --set channel_priority strict
Recommended channel order: 1. conda-forge - Community-maintained packages 2. bioconda - Bioinformatics packages 3. defaults - Anaconda's default channel
4. Keep Base Environment Clean¶
Don't install packages in base environment:
# Good: Create new environment
conda create -n myproject python=3.10
conda activate myproject
# Bad: Installing in base
conda activate base
conda install packages # Don't do this!
5. Use Mamba for Faster Installs¶
Mamba is a faster drop-in replacement for conda:
# Install mamba
conda install mamba -n base -c conda-forge
# Use mamba instead of conda
mamba create -n myenv python=3.10
mamba install numpy pandas
mamba env update -f environment.yml
6. Regular Maintenance¶
# Update conda
conda update conda
# Update all packages in environment
conda update --all
# Clean cache and unused packages
conda clean --all
# Remove unused environments
conda env remove -n old-env
Working with pip¶
When to Use pip vs conda¶
Use conda for: - ✅ Packages available in conda (numpy, pandas, scipy, etc.) - ✅ Packages with C/C++ dependencies - ✅ System-level dependencies
Use pip for: - ✅ Packages only available on PyPI - ✅ Development versions - ✅ Packages not in conda channels
Best Practice: Install conda packages first, then pip¶
# Create environment
conda create -n myenv python=3.10
# Install conda packages first
conda install numpy pandas matplotlib
# Then install pip-only packages
pip install some-pip-only-package
Including pip packages in environment.yml¶
name: my-project
dependencies:
- python=3.10
- numpy
- pip
- pip:
- package-only-on-pypi
- another-pip-package
Common Workflows¶
Workflow 1: New Project Setup¶
# 1. Create environment
conda create -n myproject python=3.10
# 2. Activate
conda activate myproject
# 3. Install packages
conda install numpy pandas matplotlib jupyter
# 4. Install pip-only packages
pip install some-package
# 5. Export environment
conda env export > environment.yml
# 6. Add to git
git add environment.yml
Workflow 2: Sharing Environment¶
# Export environment
conda env export --no-builds > environment.yml
# Share file with team
# Others can recreate:
conda env create -f environment.yml
conda activate myproject
Workflow 3: Updating Environment¶
# Update specific package
conda update numpy
# Update all packages
conda update --all
# Update from environment file
conda env update -f environment.yml --prune
Workflow 4: Multiple Python Versions¶
# Create environments with different Python versions
conda create -n py39 python=3.9
conda create -n py310 python=3.10
conda create -n py311 python=3.11
# Switch between them
conda activate py39
# work with Python 3.9
conda activate py310
# work with Python 3.10
Advanced Usage¶
Cloning Environments¶
# Clone an environment
conda create --name newenv --clone oldenv
# Useful for testing updates
conda create --name test-update --clone production
conda activate test-update
conda update --all
Managing Channels¶
# Show channel configuration
conda config --show channels
# Add channel
conda config --add channels conda-forge
# Remove channel
conda config --remove channels channel-name
# Set channel priority
conda config --set channel_priority strict # Prefer higher priority
conda config --set channel_priority flexible # Try all channels
Environment Variables¶
# Set environment variable in environment
conda env config vars set MY_VAR=value -n myenv
# List environment variables
conda env config vars list -n myenv
# Unset variable
conda env config vars unset MY_VAR -n myenv
Installing from Git¶
# Install package from git
pip install git+https://github.com/user/repo.git
# Install editable (development mode)
pip install -e git+https://github.com/user/repo.git#egg=package
Troubleshooting¶
Issue: Environment Not Activating¶
Problem: conda activate doesn't work
Solution:
Issue: Package Not Found¶
Problem: PackagesNotFoundError
Solution:
# Search for package
conda search package-name
# Try different channels
conda install -c conda-forge package-name
conda install -c bioconda package-name
# Use pip if not in conda
pip install package-name
Issue: Dependency Conflicts¶
Problem: Conflicting package versions
Solution:
# Let conda solve dependencies
conda install package-name --solver=libmamba
# Or use mamba (faster solver)
mamba install package-name
# Create fresh environment
conda create -n fresh python=3.10 package-name
Issue: Slow Installation¶
Problem: Conda is slow
Solution:
# Use mamba (much faster)
conda install mamba -n base -c conda-forge
mamba install packages
# Use conda-forge channel (often faster)
conda install -c conda-forge package-name
Issue: Environment Takes Too Much Space¶
Problem: Environments are large
Solution:
# Clean unused packages
conda clean --all
# Remove old environments
conda env remove -n old-env
# Use miniconda instead of anaconda
Conda vs Other Tools¶
Conda vs pip¶
| Feature | Conda | pip |
|---|---|---|
| Python packages | ✅ | ✅ |
| Non-Python dependencies | ✅ | ❌ |
| Environment management | ✅ | ⚠️ (venv) |
| Binary packages | ✅ | ⚠️ (wheels) |
| Dependency solving | ✅ | ⚠️ (basic) |
Conda vs venv¶
| Feature | Conda | venv |
|---|---|---|
| Multiple Python versions | ✅ | ❌ |
| Non-Python packages | ✅ | ❌ |
| Cross-platform | ✅ | ✅ |
| Lightweight | ⚠️ | ✅ |
Best Practice: Use conda for scientific computing, use venv for simple Python projects.
Quick Reference¶
# Environments
conda create -n envname python=3.10
conda activate envname
conda deactivate
conda env list
conda env remove -n envname
# Packages
conda install package
conda install -c conda-forge package
conda list
conda search package
conda update package
conda remove package
# Environment files
conda env export > environment.yml
conda env create -f environment.yml
conda env update -f environment.yml
# Maintenance
conda update conda
conda update --all
conda clean --all
Related Guides¶
- Homebrew Guide - System package management
- Development Tools Overview - All development tools guides
- Development Environment Setup - Complete macOS setup guide
- Command Line Guide - Terminal basics
Last updated: November 23, 2025