Salta ai contenuti

Conda

Conda is a cross-platform package and environment manager for Python, R, and other languages. Install packages and manage isolated Python environments.

Installation

Linux/macOS

# Download Miniconda (lightweight, recommended)
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
# or for macOS
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh

# Install
bash Miniconda3-latest-Linux-x86_64.sh

# Or download Anaconda (full distribution with GUI)
# https://www.anaconda.com/download

Windows

# Download Miniconda installer
# https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe

# Run installer and follow prompts
# Or use Chocolatey
choco install miniconda3

# Or use Winget
winget install Anaconda.Miniconda3

Basic Commands

CommandDescription
conda --versionShow conda version
conda --helpDisplay help information
conda infoShow system and environment info
conda listList packages in current environment
conda search [package]Search for packages
conda install [package]Install package in current environment
conda remove [package]Remove package from environment
conda update [package]Update package to latest version
conda clean --allRemove unused packages and cache

Creating & Managing Environments

# Create new environment with Python version
conda create -n myenv python=3.11

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

# Create environment with multiple packages
conda create -n myenv python=3.10 numpy pandas matplotlib

# Activate environment
conda activate myenv

# Deactivate environment (back to base)
conda deactivate

# List all environments
conda env list

# Remove environment
conda remove --name myenv --all

# Clone environment
conda create --clone myenv --name myenv_backup

# Display environment info
conda info myenv

Package Management

Installing Packages

# Install package in current environment
conda install numpy

# Install specific version
conda install numpy=1.21.0

# Install multiple packages
conda install numpy pandas scikit-learn

# Install package from specific channel
conda install -c conda-forge numpy

# Install with build specification
conda install numpy=1.21.0=py39_0

# Install with pip in conda environment
conda install pip
pip install package-name

# Update all packages in environment
conda update --all

# Update specific package
conda update numpy

Removing Packages

# Remove package
conda remove numpy

# Remove multiple packages
conda remove numpy pandas scipy

# Remove unused packages
conda clean --all

# Remove specific unused package
conda clean --tempfiles

Channels & Repositories

# List channels (sources)
conda config --show channels

# Add channel (conda-forge is popular)
conda config --add channels conda-forge

# Remove channel
conda config --remove channels conda-forge

# Set channel priority
conda config --set channel_priority strict

# Show all configuration
conda config --show

# Search in specific channel
conda search -c conda-forge numpy

# Install from specific channel
conda install -c bioconda biopython
# Use conda-forge (more packages, faster updates)
conda install -c conda-forge numpy

# Use defaults (official Anaconda channel)
conda install numpy

# Use bioconda (for bioinformatics)
conda install -c bioconda samtools

# Use pytorch (for machine learning)
conda install -c pytorch pytorch::pytorch torchvision

# Combine multiple channels
conda install -c conda-forge -c pytorch pytorch

Environment Files

Export Environment

# Export to environment.yml
conda env export > environment.yml

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

# Export with history
conda env export > environment.full.yml

Import Environment

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

# Create with specific name from file
conda env create -n myenv -f environment.yml

# Update existing environment
conda env update -f environment.yml --prune

# Prune removes packages not in file

Listing & Searching

# List installed packages
conda list

# List packages with version and build
conda list --explicit

# Show detailed package info
conda list numpy

# Search for package
conda search numpy

# Search for package with version
conda search "numpy=1.21"

# Show package info with dependencies
conda search --info numpy

# List updates available
conda list --outdated

# Search in all channels
conda search --all-channels numpy

Package Details

# Show package information
conda info numpy

# List dependencies of package
conda search --contains numpy

# Show package homepage
conda info --json numpy

# Export package list
conda list --export > requirements.txt

# View package dependencies
conda search --json numpy | grep '"depends"'

Virtual Environments Workflow

# Create development environment
conda create -n dev python=3.11 \
  numpy pandas scikit-learn jupyter matplotlib

# Activate environment
conda activate dev

# Install additional packages
conda install pytorch::pytorch -c pytorch

# List what's installed
conda list

# Deactivate environment
conda deactivate

# Remove when done
conda remove --name dev --all

Python Version Management

# Create environment with Python 3.9
conda create -n py39 python=3.9

# Create environment with Python 3.11
conda create -n py311 python=3.11

# Switch Python versions
conda activate py39
python --version
conda deactivate
conda activate py311
python --version

# Update Python in environment
conda install python=3.12

# Show available Python versions
conda search "python"

Conda Configuration

# Show current configuration
conda config --show

# Show all channels
conda config --show channels

# Show channel priority
conda config --show channel_priority

# Set auto-activate base environment
conda config --set auto_activate_base true

# Disable auto-activate base
conda config --set auto_activate_base false

# Set default Python version
conda config --set default_python 3.11

# Show configuration file location
conda config --show-sources

Troubleshooting

Solve Package Conflicts

# Check for conflicts
conda list --explicit

# Use strict channel priority (prevent conflicts)
conda config --set channel_priority strict

# Solve environment dependencies
conda install numpy --dry-run

# Use mamba for faster solves (if installed)
mamba install numpy

# Downgrade package if conflict
conda install numpy=1.20.0

# Clean up and retry
conda clean --all
conda install numpy

Fix Common Issues

# Environment not activating
conda init bash
source ~/.bashrc

# Packages not found
conda update conda
conda config --add channels conda-forge

# Permission denied
sudo chown -R $USER ~/anaconda3

# Remove corrupted environment
conda remove --name myenv --all --force

# Fix conda itself
conda update -n base -c defaults conda

# See what changed
conda env export > env_before.yml
# ... make changes ...
conda env update -f env_before.yml

Mamba (Optional: Faster Alternative)

# Install mamba in base environment
conda install -c conda-forge mamba

# Use mamba instead of conda (same syntax)
mamba create -n myenv python=3.11
mamba install numpy pandas
mamba env export > environment.yml

# Mamba is faster at dependency solving
mamba install pytorch::pytorch torchvision -c pytorch

Docker & Conda

# Create environment and export for Docker
conda env export --no-builds > environment.yml

# In Dockerfile
FROM continuumio/miniconda3
COPY environment.yml /tmp/
RUN conda env create -f /tmp/environment.yml
ENV PATH /opt/conda/envs/myenv/bin:$PATH

Useful Aliases

# Add to ~/.bashrc or ~/.zshrc
alias ca='conda activate'
alias cde='conda deactivate'
alias cl='conda list'
alias ce='conda env list'
alias cew='conda env export --no-builds > environment.yml'
alias cec='conda env create -f environment.yml'
alias ceu='conda env update -f environment.yml --prune'
alias cir='conda install -r requirements.txt'
alias cup='conda update --all'
alias ccc='conda clean --all'

Best Practices

  • Use conda-forge channel for latest packages and better compatibility
  • Create separate environments for different projects
  • Document dependencies in environment.yml files
  • Use specific versions for production environments
  • Regularly update packages with conda update --all
  • Pin major.minor versions in environment files
  • Use --no-builds when exporting for portability
  • Keep base environment minimal
  • Use mamba for faster dependency resolution on large projects
  • Version control your environment.yml files

Resources


Last updated: 2026-03-30