AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

CondaError: Run 'conda init' Before 'conda activate' — Fix (2026)

By Luca Berton · Published 2024-01-01 · Category: installation

Fix CondaError: Run 'conda init' before 'conda activate' in bash, zsh, fish, PowerShell, Docker, and CI/CD. Step-by-step solution for every shell and OS.

CondaError: Run 'conda init' Before 'conda activate' — Fix (2026)

Introduction

If you're working in data science, machine learning, or any field involving Python programming, you might be familiar with Conda – a powerful package and environment management system. However, a common hurdle that many users face is an error when trying to activate a Conda environment: "conda error: run 'conda init' before 'conda activate'". This message indicates that Conda hasn't been properly initialized in your shell environment, but don't worry – it's a fixable issue!

See also: Ubuntu Resolving apt dpkg Lock Errors

Why Does This Error Occur

Conda environments need to be activated to switch between different Python versions or sets of packages. The conda activate command is essential for this, but it requires Conda to be initialized in your shell. Without initialization, your shell can't recognize the conda activate command, leading to the error.

How to Fix It

Initialize Conda for Your Shell: Run conda init. This command modifies your shell's startup file (like .bashrc for Bash, .zshrc for Zsh), integrating Conda into your shell environment. This is a one-time setup – once done, you won't need to repeat it for future sessions. Temporary Solution with eval "$(conda shell.bash hook)": If, for some reason, you prefer not to run conda init, there's a workaround. Use eval "$(conda shell.bash hook)". It's a temporary measure that initializes Conda for the current shell session without altering the startup file. Remember, this is a session-specific solution and needs to be repeated each time you open a new shell.

See also: How to Break a String Over Multiple Lines with Ansible and YAML

Example Usage

# Initialize Conda for your shell (just once)
conda init

# Alternatively, for a temporary session-specific solution eval "$(conda shell.bash hook)" conda activate fooocus

Keep in Mind

• Post conda init, a shell restart might be necessary for the changes to take effect. • eval "$(conda shell.bash hook)" offers a temporary fix and must be executed in every new shell session if conda init is not an option.

See also: Ansible yum Module: Manage RPM Packages on RHEL/CentOS (Guide)

The Error

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run: $ conda init <SHELL_NAME>

Fix for Bash

conda init bash
# Then restart shell or:
source ~/.bashrc

Fix for Zsh

conda init zsh
source ~/.zshrc

Fix for Fish

conda init fish

What conda init Does

Adds a block to your shell RC file:

# >>> conda initialize >>>
__conda_setup="$('/opt/conda/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/opt/conda/etc/profile.d/conda.sh" ]; then
        . "/opt/conda/etc/profile.d/conda.sh"
    fi
fi
# <<< conda initialize <<<

Alternative: Source Directly

# Without conda init (non-persistent)
source /opt/conda/etc/profile.d/conda.sh
conda activate myenv

Fix in Scripts

#!/bin/bash
# Scripts don't source .bashrc — activate manually
source /opt/conda/etc/profile.d/conda.sh
conda activate myenv
python train.py

Fix in Ansible

- name: Run in conda environment
  ansible.builtin.shell: |
    source /opt/conda/etc/profile.d/conda.sh
    conda activate myenv
    python train.py
  args:
    executable: /bin/bash

Fix in Docker

# Dockerfile
RUN conda init bash
SHELL ["/bin/bash", "--login", "-c"]
RUN conda activate myenv && pip install -r requirements.txt

Fix in CI/CD (GitHub Actions)

- name: Setup conda
  run: |
    eval "$(conda shell.bash hook)"
    conda activate myenv
    pytest

Disable Auto-Activation

# Stop conda from activating base on every new shell
conda config --set auto_activate_base false

Troubleshooting

conda command not found

# Add conda to PATH
export PATH="/opt/conda/bin:$PATH"
# Or Miniconda
export PATH="$HOME/miniconda3/bin:$PATH"

Wrong Python after activation

which python
# Should show: /opt/conda/envs/myenv/bin/python
# If not, the environment isn't activated properly

FAQ

Why did this start happening?

Conda 4.4+ changed activation to use conda activate instead of source activate. The new method requires shell initialization.

Can I use the old source activate?

It's deprecated but may still work. Use conda activate after running conda init.

Does this affect Jupyter notebooks?

Only if launching from terminal. Jupyter kernels have their own Python path configured separately via ipykernel.

The Error

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run:

$ conda init <SHELL_NAME>

Quick Fix

# Initialize conda for your shell
conda init bash    # For Bash
conda init zsh     # For Zsh
conda init fish    # For Fish
conda init powershell  # For PowerShell

# Then restart your shell source ~/.bashrc # Or close and reopen terminal

What conda init Does

It adds initialization code to your shell profile:

# Added to ~/.bashrc or ~/.zshrc
# >>> conda initialize >>>
__conda_setup="$('/path/to/conda/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/path/to/conda/etc/profile.d/conda.sh" ]; then
        . "/path/to/conda/etc/profile.d/conda.sh"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

Alternative: Manual Activation

# If you don't want conda init to modify your profile
source /path/to/conda/etc/profile.d/conda.sh
conda activate myenv

Fix for Different Shells

Bash

conda init bash
source ~/.bashrc

Zsh

conda init zsh
source ~/.zshrc

Fish

conda init fish
# Restart fish

PowerShell

conda init powershell
# Restart PowerShell

Fix in Docker/CI

# Dockerfile
RUN conda init bash && \
    echo "conda activate myenv" >> ~/.bashrc

# Or use shell form SHELL ["/bin/bash", "--login", "-c"] RUN conda activate myenv && python script.py

# GitHub Actions
- name: Setup Conda
  run: |
    eval "$(conda shell.bash hook)"
    conda activate myenv
    python --version

Fix in Ansible

- name: Run command in conda environment
  shell: |
    source /opt/conda/etc/profile.d/conda.sh
    conda activate myenv
    python script.py
  args:
    executable: /bin/bash

Fix in Cron Jobs

# Cron doesn't load shell profiles
# Use full path or source conda first
* * * * * /bin/bash -c 'source /opt/conda/etc/profile.d/conda.sh && conda activate myenv && python /path/script.py'

Disable Auto-Activation

# Prevent conda from activating base on every terminal
conda config --set auto_activate_base false

Common Issues

| Problem | Solution | |---------|----------| | conda activate doesn't work | Run conda init | | Wrong Python after activate | Check which python inside env | | base activates every time | conda config --set auto_activate_base false | | conda not found | Add conda to PATH: export PATH="/opt/conda/bin:$PATH" | | Permission denied | Don't use sudo conda — install in user space | | SSH session conda fails | Source conda.sh in ~/.bashrc or ~/.profile |

Uninstall conda init Changes

# Remove conda init block from shell profile
conda init --reverse bash

FAQ

Why can't I just use source activate?

source activate is the old (deprecated) way. conda activate is the modern approach but requires shell initialization via conda init.

Does this affect pip/virtualenv?

No — this is specific to Conda environments. pip and virtualenv work independently.

Miniconda vs Anaconda — same fix?

Yes — both use the same conda command and conda init process.

The Error

CondaError: Run 'conda init' before 'conda activate'

Quick Fix

# Initialize conda for your shell
conda init bash    # or zsh, fish, powershell
source ~/.bashrc   # Reload shell config

# Now activate works conda activate myenv

Fix for Scripts

#!/bin/bash
# Option 1: Source conda.sh directly
source ~/miniconda3/etc/profile.d/conda.sh
conda activate myenv

# Option 2: Use eval eval "$(conda shell.bash hook)" conda activate myenv

Fix for Docker

# In Dockerfile
RUN conda init bash
SHELL ["/bin/bash", "--login", "-c"]
RUN conda activate myenv && python script.py

# Or use run form RUN /bin/bash -c "source activate myenv && python script.py"

Fix for CI/CD

# GitHub Actions
- run: |
    eval "$(conda shell.bash hook)"
    conda activate myenv
    python -m pytest

# Jenkins sh ''' source ~/miniconda3/etc/profile.d/conda.sh conda activate myenv python script.py '''

Fix with Ansible

- name: Run command in conda environment
  ansible.builtin.shell: |
    source {{ conda_path }}/etc/profile.d/conda.sh
    conda activate {{ env_name }}
    python script.py
  args:
    executable: /bin/bash
  vars:
    conda_path: /opt/miniconda3
    env_name: myenv

Alternative: Use conda run

# Skip activation entirely
conda run -n myenv python script.py
conda run -n myenv pip install requests

Conda Not Found

# If conda command not found after install
export PATH="$HOME/miniconda3/bin:$PATH"
conda init bash
source ~/.bashrc

Multiple Shell Support

conda init bash zsh fish  # Initialize for multiple shells

FAQ

Why does this happen?

conda activate requires shell hooks installed by conda init. Without them, conda can't modify the shell environment to switch environments.

conda activate vs source activate?

conda activate is the modern way (requires conda init). source activate is legacy and may be removed. Use conda activate.

How to undo conda init?

conda init --reverse bash
# Removes conda hooks from ~/.bashrc

If you've hit the activation error, you'll likely encounter these neighbouring Conda issues. Each one has a quick diagnosis and a copy-paste fix.

PackagesNotFoundError: The following packages are not available from current channels

Conda can't find a package in the channels it's configured to search. The fix is almost always to add conda-forge:

# Add conda-forge once, globally
conda config --add channels conda-forge
conda config --set channel_priority strict

# Then retry the install conda install -n myenv <package>

# One-shot install from conda-forge without changing config conda install -c conda-forge <package>

# Last resort: pip inside the env conda activate myenv pip install <package>

CondaHTTPError: HTTP 000 CONNECTION FAILED for url

Network, proxy, or SSL issue while reaching repo.anaconda.com. Common fixes:

# 1. Disable SSL verification temporarily (corporate proxies)
conda config --set ssl_verify false

# 2. Set proxies explicitly conda config --set proxy_servers.http http://proxy.corp:8080 conda config --set proxy_servers.https https://proxy.corp:8080

# 3. Switch to conda-forge mirror conda config --add channels conda-forge conda config --remove channels defaults

# 4. Update conda itself (often resolves outdated TLS) conda update -n base -c defaults conda

EnvironmentNotWritableError: The current user does not have write permissions

You installed Conda as root or in a system-wide path. Don't sudo conda — fix permissions or install in user space:

# Fix ownership of an existing install
sudo chown -R $USER:$USER /opt/conda

# Or install Miniconda in your home directory bash Miniconda3-latest-Linux-x86_64.sh -b -p $HOME/miniconda3 $HOME/miniconda3/bin/conda init bash

CondaError: KeyError 'pkgs_dirs'

Corrupt ~/.condarc or stale package cache. Reset and clean:

# Backup and remove .condarc
mv ~/.condarc ~/.condarc.bak

# Clear caches conda clean --all -y

# Re-init conda init bash

prefix already exists

You're trying to create an env that already exists. Either remove it first or update in place:

# Remove existing env
conda env remove -n myenv

# Or recreate from scratch conda create -n myenv python=3.11 -y

# Or update an existing env from environment.yml conda env update -n myenv -f environment.yml --prune

command not found: conda

Conda isn't on your PATH. Either run the init or export the path manually:

# Find your conda install
ls $HOME/miniconda3/bin/conda /opt/conda/bin/conda 2>/dev/null

# Add to PATH for the current session export PATH="$HOME/miniconda3/bin:$PATH"

# Persist via conda init $HOME/miniconda3/bin/conda init bash exec $SHELL -l

conda activate not working in shell scripts

Shell scripts don't source ~/.bashrc, so conda init's hooks aren't loaded. Source conda.sh directly:

#!/usr/bin/env bash
set -euo pipefail

# Required: load the conda function inside scripts source "$(conda info --base)/etc/profile.d/conda.sh"

conda activate myenv python train.py

base environment auto-activates on every new shell

Conda activates base by default on shell start. Disable it:

# Stop auto-activating base
conda config --set auto_activate_base false

# Reload shell exec $SHELL -l

conda activate fails inside a Makefile

Make spawns a non-interactive /bin/sh per recipe line. Use the && chain inside one shell invocation:

SHELL := /bin/bash
CONDA_ACTIVATE = source $$(conda info --base)/etc/profile.d/conda.sh ; conda activate

train: $(CONDA_ACTIVATE) myenv && python train.py

Solving environment: failed with initial frozen solve hangs forever

The classic SAT solver is slow. Switch to libmamba:

conda install -n base conda-libmamba-solver -y
conda config --set solver libmamba

Conclusion

By following these steps, you should be able to seamlessly activate your Conda environments, paving the way for a smoother workflow in your Python projects. Remember, proper environment management is key in Python programming, especially when juggling multiple projects with varying dependencies. Happy coding! 🐍💻

Related Articles

AAP 2.6 Troubleshooting Guide Common Issues SolutionsAnsible Best Practices ignore_errors in Ansible PlaybooksAnsible Conflicting Action Statements Error Causes Fixes Guide

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home