Skip to content

How to Set Up and Manage Conda Environments

DodaTech 2 min read

In this tutorial, you'll learn about How to Set Up and Manage Conda Environments. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

You work on multiple Python projects with conflicting dependencies. One project needs Python 3.10 and Django 4.2, another needs Python 3.12 and FastAPI. Without isolated environments, package versions collide and break projects.

Quick Fix

Step 1: Create a new Conda environment

conda create --name myproject python=3.11

This creates a new environment named myproject with Python 3.11 and its core packages. Confirm with y when prompted.

Step 2: Activate the environment

conda activate myproject

Your shell prompt changes to (myproject) $. All subsequent conda install and pip install commands affect only this environment.

Step 3: Install packages in the environment

conda install numpy pandas scipy
# or with a specific version
conda install django=4.2

Step 4: Install pip packages

pip install requests flask

Conda environments can use pip alongside conda. Mixing them is fine for most use cases, but prefer conda install when available.

Step 5: List all environments

conda env list

Expected:

base                  /opt/conda
myproject           * /opt/conda/envs/myproject

The * marks the active environment.

Step 6: List installed packages

conda list

Expected:

# packages in environment at /opt/conda/envs/myproject:
#
# Name                    Version                   Build  Channel
python                    3.11.9               h955ad1f_1
numpy                     1.26.4                   pypi_0    pypi
django                    4.2.14                   pypi_0    pypi

Step 7: Export the environment to a file

conda env export > environment.yml

This creates environment.yml listing every package and version. Share this file so others can recreate the exact environment.

Step 8: Create an environment from a file

conda env create -f environment.yml

This recreates the environment on a different machine or after a clean install.

Step 9: Clone an environment

conda create --name myproject-backup --clone myproject

Step 10: Remove an environment

conda env remove --name myproject

Step 11: Deactivate the current environment

conda deactivate

Returns to the base environment.

Alternative Solutions

Use venv (built-in Python module) for lightweight environments:

python -m venv .venv
source .venv/bin/activate

Conda is preferred when you need non-Python dependencies (C libraries, CUDA toolkits).

Common Errors

conda: command not found: Conda is not in the PATH. Re-run the installer's init command: ~/miniconda3/bin/conda init or add export PATH=~/miniconda3/bin:$PATH to ~/.bashrc.

Environment creation takes too long: If conda is solving the environment slowly, use libmamba solver: conda install -n base conda-libmamba-solver && conda config --set solver libmamba.

Disk space exhausted: Conda environments can grow large (multiple GB). Remove unused environments: conda env remove --name old-env. Cache cleanup: conda clean --all.

pip packages not found after conda activate: If pip install fails with "externally managed environment" in Python 3.12+, use pip install --break-system-packages or create environments with pip pre-installed.

Prevention

  • Name environments after the project name for clarity.
  • Export environment.yml and commit it to version control.
  • Use conda clean --all periodically to free disk space from cached packages.
  • Pin Python and major dependency versions in environment.yml for reproducibility.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro