Skip to content
Latchkey

conda: Create and Activate an Env in CI

conda create -n <env> builds an environment, but conda activate only works after conda's shell hook is loaded; in CI use "conda run" or source the hook.

conda manages Python (and other) environments. The recurring CI failure is that conda activate is a shell function, so a non-interactive runner shell cannot activate until conda init has run or the hook is sourced.

What it does

conda creates isolated environments with their own Python and packages. conda activate <env> switches the shell into one, but that command is provided by conda's shell integration; without it, only conda run -n <env> works. conda init writes the hook into your shell rc files.

Common usage

bash
conda create -n ci python=3.12 -y
# activation needs the hook in a non-interactive shell:
source "$(conda info --base)/etc/profile.d/conda.sh"
conda activate ci
python --version
# or skip activation entirely:
conda run -n ci python --version

Options

Command / flagWhat it does
conda create -n <env>Create a named environment
conda activate <env>Switch the shell into an env (needs hook)
conda run -n <env> <cmd>Run a command in an env, no activation
conda env create -f environment.ymlCreate from a spec file
-yAssume yes (non-interactive)
source .../conda.shLoad the activation hook in CI

In CI

Either source $(conda info --base)/etc/profile.d/conda.sh before conda activate, or avoid activation entirely with conda run -n <env> <cmd>, which is the most robust CI pattern. On GitHub Actions, shell: bash -el {0} (a login shell) runs conda init for you. Always pass -y.

Common errors in CI

"CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'. To initialize your shell, run $ conda init" means the hook is not loaded; source conda.sh or use conda run. A job that hangs at create without -y is waiting on a prompt. "PackagesNotFoundError" means a package is not in the configured channels.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →