Conda

Table of contents
  1. Advantage to other virtual environments
  2. Install Conda (miniforge)
  3. Typical usage
  4. Create environment
  5. Activate / Deactivate
  6. Install packages
  7. Uninstall packages
  8. List and export dependencies
  9. Clone environment
  10. Remove environment
  11. Installing Python 2.7 on Apple Silicon

Advantage to other virtual environments

Unlike some other virtual environments that are dependent on a preinstalled Python, conda is both a Python version manager and a virtual environment manager.

conda makes using different Python versions in different environments easier.


Install Conda (miniforge)

Conda can be installed through homebrew:

brew install miniforge
conda init "$(basename "${SHELL}")"

If you don’t want the base environment activated all the time,

conda config --set auto_activate_base false

Typical usage

To create an environment for a project:

conda create -n myenv python=3.x
conda activate myenv

Create environment

Simplest method is:

conda create -n myenv

To use a specific Python version:

conda create --name myenv python=3.8

Created environments are located in ~/anaconda3/env or ~/miniconda3/env.

If you installed conda via GUI installer, the conda folder may be in /opt.

Confirm environment creation via

conda env list
# OR
conda info --envs

Activate / Deactivate

conda activate myenv
conda deactivate

Install packages

To install packages in current active environment,

conda install pkg-name
# OR for a specific version
conda install pkg-name=1.0.0

To install packages in another environment,

conda install pkg-name -n myenv

Uninstall packages

conda remove pkg-name

List and export dependencies

conda list

To export dependencies (like pip3 freeze > requirements.txt),

conda list --export > requirements.txt

To create an environment with given requirements (like pip3 install -r requirements.txt),

conda create -n myenv --file requirements.txt

Clone environment

To clone an existing environment,

conda create --name <new-env> --clone <existing-env>

Remove environment

Make sure to conda deactivate before removing an environment.

conda env list  # Check the env name
conda env remove -n myenv

Installing Python 2.7 on Apple Silicon

In case you need to install Python 2.7 on Apple Silicon, use Rosetta 2:

# Create env with osx-64 subdir
CONDA_SUBDIR=osx-64 conda create -n py27 python=2.7
conda activate py27
# Always use osx-64 subdir for packages in this env
conda config --env --set subdir osx-64

References: