Environment lifecycle
This document describes how to create and manage conda environments from conda packages.
Specification
Creating a conda environment
An empty directory $CONDA_PREFIX can be turned into a conda environment by creating an empty conda-meta/history file. The conda client MAY register this location into a central registry of environments, such as ~/.conda/environments.txt.
The command used to create that environment MAY be recorded in conda-meta/history, along with the version, timestamp and details of the transaction.
Installing packages
For each package to be installed, conda clients:
- SHOULD download or copy the artifact to the cache location using the "filenames" syntax described in CEP 26.
- SHOULD, if available, verify the integrity of the artifact with its checksum (
sha256is preferred overmd5). - MUST extract and aggregate the artifact to a central location. The extracted directory SHOULD be named as a distribution string (without subdir), as described in CEP 26.
- SHOULD write an
./info/repodata_record.jsonfile in the extracted directory, whose contents MUST be populated with one of:- The
RepodataRecordinformation available in the lockfile, if relevant. - The
RepodataRecordinformation available in the repodata source, if relevant. - The contents of
./info/index.json, as a fallback.
- The
Once extracted, the packages MUST be installed in the target prefix $CONDA_PREFIX by following these steps:
- Execute the relevant
pre-linkscripts. - Link or copy the non-
info/contents of the package into$CONDA_PREFIX. Tools SHOULD follow the manifest file atinfo/paths.json. This means that:- For non-
noarch: pythonpackages, place the contents of the artifact into$CONDA_PREFIX.- If the file contains a prefix placeholder, replace it with the value of
$CONDA_PREFIXand copy the file. - Otherwise, place the file in
$CONDA_PREFIX, as instructed by thepaths.jsonmetadata. Tools MAY offer settings to override this operation (e.g. prefer copies to hardlinks).
- If the file contains a prefix placeholder, replace it with the value of
noarch: pythonpackages follow some extra rules. In particular, they no longer follow a 1:1 correspondence between the path in the artifact and the linked path in$CONDA_PREFIX. The target path depends on variables like the Python version, OS and Python ABI modes. Details are discussed in CEP 17 and CEP 20.
- For non-
- Handle the
./Menu/*.jsondocuments. - Execute the relevant
post-linkscripts. - Record the package metadata at
$CONDA_PREFIX/conda-meta/{name}-{version}-{build}.json, as described in Installed package records.
While linking paths into their targets, the conda client MAY run into clobbering conflicts (two or more packages wanting to write to the same path). Tools SHOULD at least warn the user about the conflicts and provide ways to handle the situation.
Pre- and post-link/unlink scripts MUST follow the script runtime requirements. Execution SHOULD be performed in topological order.
Removing a package
Removing a package from the environment SHOULD follow these instructions:
- MUST execute the relevant
pre-unlinkscripts. - Handle the
./Menu/*.jsondocuments. - Remove all the files recorded in its
conda-meta/*.jsonfile (underpaths_data). - MAY execute deprecated
post-unlinkscripts. - Remove its
conda-meta/*.jsonrecord. - If there were clobbering conflicts, restore the relevant path from the clobbered sources.
Removing an environment
Once an environment contains no packages, the conda client MAY remove it. This process involves clearing the conda-meta/ folder and any condarc files, and deregistering the environment path from the central manifest, if applicable (e.g. ~/.conda/environments.txt). If there were any additional files in the environment directory, the conda client SHOULD report that to the user and offer to leave them in place or to proceed and clear all the contents.
Activating and deactivating an environment
Environment management tools SHOULD implement a mechanism to activate an environment. They MAY also implement logic to deactivate it.
Given a target environment located at $CONDA_PREFIX, the activation logic MUST involve the following tasks:
- Temporarily modifying the
PATHenvironment variable to include directories that usually contain executables, as discussed in CEP 34:- On Unix systems, it MUST include
$CONDA_PREFIX/bin. - On Windows systems, it MUST include (in this order):
$CONDA_PREFIX.- The first directory that exists in this list, if any:
$CONDA_PREFIX/Library/ucrt64/bin,$CONDA_PREFIX/Library/clang64/bin,$CONDA_PREFIX/Library/mingw64/bin,$CONDA_PREFIX/Library/clangarm64/bin. $CONDA_PREFIX/Library/mingw-w64/bin.$CONDA_PREFIX/Library/usr/bin.$CONDA_PREFIX/Library/bin.$CONDA_PREFIX/Scripts.$CONDA_PREFIX/bin.
- On Unix systems, it MUST include
- Handling the environment variables and scripts in
$CONDA_PREFIX/etc/conda/, as described below.
For deactivation, the effects of the actions above MUST be reverted.
The following files and directories MUST be handled by the conda client:
$CONDA_PREFIX/etc/conda/env_vars.d/: Directory containing JSON documents, each providing a dictionary that maps strings to strings, which are understood as environment variables. The JSON documents SHOULD be loaded alphabetically, with later documents overriding earlier ones. These environment variables SHOULD be exported on environment activation, and unset on deactivation. When bothenv_vars.d/andconda-meta/stateare present, the latter is loaded last and can override the previously defined variables.$CONDA_PREFIX/etc/conda/activate.d/: Directory containing shell scripts. They SHOULD be executed on environment activation, in alphabetical order.$CONDA_PREFIX/etc/conda/deactivate.d/: Directory containing shell scripts. They SHOULD be executed on environment deactivation, in reverse alphabetical order.
Alphabetical order MUST be understood as the ascending lexicographical order provided by Unicode Code Point comparisons, just like Python would sort such a list.