How to Create Conda Environment: Complete Guide with Examples
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.
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
| Task | Command |
|---|---|
| Create basic environment | conda create --name myenv |
| Create with Python version | conda create --name myenv python=3.11 |
| Create from YAML file | conda env create -f environment.yml |
| Clone environment | conda create --name newenv --clone oldenv |
| List all environments | conda env list |
| Activate environment | conda activate myenv |
| Deactivate environment | conda deactivate |
| Remove environment | conda 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 myenvThis creates an empty environment named myenv. To create and activate it in one workflow:
conda create --name myenv
conda activate myenvAfter 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.11You 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.9Supported Python Versions
| Python Version | Status | Recommended For |
|---|---|---|
| 3.12 | Latest | New projects, cutting-edge features |
| 3.11 | Stable | Production applications |
| 3.10 | Stable | Most libraries compatible |
| 3.9 | Mature | Legacy project support |
| 3.8 | End of life | Maintenance 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-learnSpecify package versions for reproducibility:
conda create --name dataenv python=3.11 numpy=1.24 pandas=2.0 matplotlib=3.7Creating 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-packageCreating the Environment from YAML
conda env create -f environment.ymlThis 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 --pruneThe --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 oldenvThis 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.11Activate it with the full path:
conda activate ./envs/myenvThis 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 test2. Always Specify Python Version
Explicit versions prevent unexpected behavior:
conda create --name myenv python=3.113. 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.yml4. 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 pandas5. 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-nameSlow 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 pandasConda Not Found After Installation
Problem: conda: command not found
Solution: Initialize Conda for your shell:
conda init bash # or zsh, fish, powershellThen restart your terminal.
Managing Your Environments
After creating environments, you'll need to manage them effectively:
- List environments:
conda env listshows all environments and their locations - Remove environments: See our guide on how to remove Conda environments for detailed instructions
- Update packages: Use
conda update --allwithin an activated environment
Conda Create vs Conda Env Create
Two commands exist for creating environments:
| Feature | conda create | conda env create |
|---|---|---|
| Create empty environment | Yes | No |
| Specify packages inline | Yes | No |
| Create from YAML | No | Yes |
| Clone environments | Yes | No |
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
- How to Remove Conda Environment - Learn how to properly delete Conda environments
- Conda Official Documentation (opens in a new tab) - Complete environment management reference
