Skip to content

How to Create Conda Environment: Complete Guide with Examples

Kanaries Team
Name
Kanaries Team

Updated on

Managing Python dependencies across multiple projects can quickly become a nightmare. Different projects require different package versions, and installing everything in a single environment leads to version conflicts and broken code. Conda environments solve this problem by creating isolated spaces for each project's dependencies.

This guide covers everything you need to know about creating Conda environments, from basic commands to advanced techniques like cloning and YAML-based configuration.

Want to quickly create Data Visualization from Python Pandas Dataframe with No code?

PyGWalker is a Python library for Exploratory Data Analysis with Visualization. PyGWalker (opens in a new tab) can simplify your Jupyter Notebook data analysis and data visualization workflow, by turning your pandas dataframe (and polars dataframe) into a tableau-alternative User Interface for visual exploration.

PyGWalker for Data visualization (opens in a new tab)

What is a Conda Environment?

A Conda environment is an isolated directory containing a specific collection of packages and their dependencies. Each environment operates independently, allowing you to:

  • Use different Python versions for different projects
  • Install conflicting package versions without issues
  • Share reproducible development setups with teammates
  • Keep your base installation clean and stable

Quick Reference: Conda Environment Commands

TaskCommand
Create basic environmentconda create --name myenv
Create with Python versionconda create --name myenv python=3.11
Create from YAML fileconda env create -f environment.yml
Clone environmentconda create --name newenv --clone oldenv
List all environmentsconda env list
Activate environmentconda activate myenv
Deactivate environmentconda deactivate
Remove environmentconda env remove --name myenv

Creating a Basic Conda Environment

The simplest way to create a Conda environment uses the conda create command:

conda create --name myenv

This creates an empty environment named myenv. To create and activate it in one workflow:

conda create --name myenv
conda activate myenv

After activation, your terminal prompt changes to show the active environment name.

Creating an Environment with a Specific Python Version

Most projects require a specific Python version. Specify it during creation:

conda create --name myenv python=3.11

You can also specify a version range:

# Python 3.10 or higher
conda create --name myenv python>=3.10
 
# Python 3.9.x (any patch version)
conda create --name myenv python=3.9

Supported Python Versions

Python VersionStatusRecommended For
3.12LatestNew projects, cutting-edge features
3.11StableProduction applications
3.10StableMost libraries compatible
3.9MatureLegacy project support
3.8End of lifeMaintenance only

Creating an Environment with Packages

Install packages during environment creation to save time:

conda create --name dataenv python=3.11 numpy pandas matplotlib scikit-learn

Specify package versions for reproducibility:

conda create --name dataenv python=3.11 numpy=1.24 pandas=2.0 matplotlib=3.7

Creating an Environment from a YAML File

For complex projects, define environments in a YAML file. This approach enables version control and easy sharing.

Creating the environment.yml File

name: myproject
channels:
  - conda-forge
  - defaults
dependencies:
  - python=3.11
  - numpy=1.24
  - pandas=2.0
  - scikit-learn=1.3
  - matplotlib=3.7
  - jupyter
  - pip
  - pip:
    - pygwalker
    - some-pip-only-package

Creating the Environment from YAML

conda env create -f environment.yml

This command reads the file and creates an environment with the specified name and packages.

Updating an Existing Environment from YAML

conda env update -f environment.yml --prune

The --prune flag removes packages not listed in the YAML file.

Cloning an Existing Environment

Duplicate an environment to experiment without affecting the original:

conda create --name newenv --clone oldenv

This copies all packages and their exact versions. Useful scenarios include:

  • Testing package upgrades safely
  • Creating project-specific variations
  • Backing up a working environment before major changes

Creating Environments in Custom Locations

By default, Conda stores environments in the envs directory. Create an environment elsewhere using --prefix:

conda create --prefix ./envs/myenv python=3.11

Activate it with the full path:

conda activate ./envs/myenv

This approach keeps project dependencies within the project folder.

Best Practices for Conda Environments

1. Use Descriptive Names

Name environments after projects or purposes:

# Good
conda create --name webapp-backend python=3.11
conda create --name data-analysis python=3.10
 
# Avoid
conda create --name env1
conda create --name test

2. Always Specify Python Version

Explicit versions prevent unexpected behavior:

conda create --name myenv python=3.11

3. Export Environments for Sharing

Create reproducible environment files:

# Full export (platform-specific)
conda env export > environment.yml
 
# Cross-platform export
conda env export --from-history > environment.yml

4. Use conda-forge Channel

The conda-forge channel offers more packages and faster updates:

conda create --name myenv -c conda-forge python=3.11 numpy pandas

5. Keep Environments Minimal

Install only necessary packages. Remove unused environments regularly using the conda env remove command.

Troubleshooting Common Issues

Environment Creation Fails

Problem: ResolvePackageNotFound error

Solution: Try using conda-forge channel or check package name spelling:

conda create --name myenv -c conda-forge python=3.11 package-name

Slow Environment Creation

Problem: Solving environment takes too long

Solution: Use mamba, a faster drop-in replacement:

conda install -n base -c conda-forge mamba
mamba create --name myenv python=3.11 numpy pandas

Conda Not Found After Installation

Problem: conda: command not found

Solution: Initialize Conda for your shell:

conda init bash  # or zsh, fish, powershell

Then restart your terminal.

Managing Your Environments

After creating environments, you'll need to manage them effectively:

  • List environments: conda env list shows all environments and their locations
  • Remove environments: See our guide on how to remove Conda environments for detailed instructions
  • Update packages: Use conda update --all within an activated environment

Conda Create vs Conda Env Create

Two commands exist for creating environments:

Featureconda createconda env create
Create empty environmentYesNo
Specify packages inlineYesNo
Create from YAMLNoYes
Clone environmentsYesNo

Use conda create for quick, inline environment creation. Use conda env create when working with YAML files.


FAQ

How do I create a Conda environment with a specific Python version?

Use conda create --name myenv python=3.11 replacing 3.11 with your desired version. Conda downloads and installs the specified Python version in the new isolated environment.

What is the difference between conda create and conda env create?

conda create creates environments with packages specified inline on the command line. conda env create creates environments from YAML specification files. Use conda create for quick setups and conda env create for reproducible, version-controlled environments.

How do I create a Conda environment from a requirements.txt file?

First create the environment, then install from requirements.txt: conda create --name myenv python=3.11 followed by pip install -r requirements.txt after activating the environment. For better compatibility, convert requirements.txt to environment.yml format.

Can I create a Conda environment in a specific directory?

Yes, use the --prefix flag: conda create --prefix ./myenv python=3.11. This creates the environment in the specified path instead of the default Conda envs directory. Activate it using the full path.

Related Guides