Search code examples
bashanacondacondatmux

Conda does not set paths when activating environment


When starting a new shell, the PATH environment variable is not configured properly. The directories anaconda3/bin or miniconda3/bin are at second position only, not at first position in the PATH variable. This can be resolved by conda deactivate and activating it again.

This question was asked several times already (e.g. here and here) but the existing questions are either very old or concentrate on the use of source activate env-name. All in all, I found no answer that resolves my problem.

When I start a new shell, the base environment is activated. The relevant snippet from my .bashrc reads like this:

condaexe="/home/$USER/.miniconda3/bin/conda"
condash="/home/$USER/.miniconda3/etc/profile.d/conda.sh"
__conda_setup="$($condaexe 'shell.bash' 'hook' 2> /dev/null)"
# shellcheck disable=SC2181
if [[ $? -eq 0 ]]
then
    eval "$__conda_setup"
elif [[ -f "$condash" ]]
then
    source "$condash"
fi
unset __conda_setup condaexe condash

Then, the PATH variables are defined as follows:

(base)$ echo $PATH
/home/user/.local/bin:/home/user/.miniconda3/bin:/home/user/.miniconda3/condabin:/home/user/workspace/my-project/:/home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
(base)$ conda deactivate && echo $PATH
/home/user/.local/bin:/home/user/.miniconda3/condabin:/home/user/workspace/my-project/:/home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
$ conda activate base && echo $PATH
/home/user/.miniconda3/bin:/home/user/.local/bin:/home/user/.miniconda3/condabin:/home/user/workspace/my-project/:/home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
(base)$

Note that /home/user/.local/bin is contained twice; once before and once after the Miniconda3 directories.

I tried to debug the problem by appending the following snippet to .bashrc:

echo $CONDA_PROMPT_MODIFIER
echo $PATH

This yields

(base)
/home/user/.miniconda3/bin:/home/user/.miniconda3/condabin:/home/user/workspace/my-project:/home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

which would be perfectly fine but, somehow, is modified after .bashrc. Note that here /home/user/.local/bin is contained only once.

What goes on here? How can I setup Bash/Conda to get a properly defined PATH environment variable?


Solution

  • There are three components I missed in the original question which are key to the solution.

    First, I run all my shells inside TMux. Second, TMux sources the .profile. Third, in .profile some local directories like the aformentioned ~/.local/bin are blindly prepended to the PATH.

    Taken this altogether all the weird behaviour above makes sense. Especially that PATH is correct at the end of ~/.bashrc but not in the shell is obvious now; it got modified in between by ~/.profile.

    There are three solutions for this:

    1. deactivating and activating conda manually (temporary workaround)
    2. being more picky about which shells are started inside TMux (very unhandy)
    3. commenting out the manipulations of PATH in ~/.profile

    It seems as if one cannot configure TMux to source only ~/.bashrc (reference 1, reference 2), albeit some workarounds exist here too.