Reputation: 836
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?
Upvotes: 3
Views: 5308
Reputation: 3
Thanks for this post! That give me hint to debug same issue. I think everyone is different situdation but the idea is same: there is a command like export $PATH
change the path in your zshrc
or .bashrc
which will affect the computer to find the path you want.
Upvotes: 0
Reputation: 27588
This is related to the Bash init files. By default, ~/.bashrc
is used in an interactive, non-login shell. It won't be sourced in a login shell. Tmux uses a login shell by default. Hence, shells started by tmux skip ~/.bashrc
.
default-command
shell-commandThe default is an empty string, which instructs tmux to create a login shell using the value of the
default-shell
option.
Init files for Bash,
/etc/profile
~/.bash_profile
, ~/.bash_login
, ~/.profile
(only first one that exists)/etc/bash.bashrc
(some Linux; not on Mac OS X)~/.bashrc
$BASH_ENV
The weird interactive, non-login loading requirement confuses people in other situations as well. The best solution is to change the loading requirement of ~/.bashrc
as interactive only, which is exactly what some distros, like Ubuntu, are doing.
# write content below into ~/.profile, or ~/.bash_profile
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
This should be the solution you desire. And I recommend every Bash user setup this in the profile.
References
man tmux
Upvotes: 3
Reputation: 836
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:
~/.profile
It seems as if one cannot configure TMux to source only ~/.bashrc
(reference 1, reference 2), albeit some workarounds exist here too.
Upvotes: 2