Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script to install conda leads to "conda: command not found" unless I run `bash` at the command line afterwards

I'm trying to write a script to install miniConda, then activate a conda environment.

Here's the relevant part of the code:

if ! command -v conda --version &> /dev/null
then
    wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O conda.sh
    bash conda.sh -b -p ~/local/miniconda3
    rm -f conda.sh
    ~/local/miniconda3/bin/conda init bash
    . ~/.bashrc
fi
conda config --set auto_activate_base false

conda create -yn my_env
eval "$(conda shell.bash hook)"
conda activate my_env

When I run the script, the output on my terminal looks as though conda was installed successfully. I get a long stream of messages, and, after reading through them all, it seems clear that everything was successful. (It's stuff like Solving environment: done, if that helps).

However, when I run conda --version at my terminal to test if I did install conda, I get the error conda: command not found

I can get conda --version to work as expected if I follow this StackOverflow answer and type bash into my terminal, then run conda --version from there.

I've read a lot of answers about shells and subshells and how they relate to conda. I have the vague idea that my issue has to do with something (?) being executed in a subshell instead of the parent shell (?), and maybe something about sourcing ~/.bashrc wrong (?). I need some major pointers on the details, though.

I apologize for not being able to form a better question. If I had a more specific idea of what to ask, I would just google it. :)

NOTE: Some of the stuff in my script (like ~/local/miniconda3/bin/conda init bash; . ~/.bashrc) was copied from other StackOverflow answers. I read the explanations on those SO questions very carefully, but I unfortunately didn't come away with much more than the general idea "It works if you do this". It would be great if anyone answering could also explain if those lines relate to my question.

like image 814
teddy Avatar asked Oct 28 '25 08:10

teddy


1 Answers

Probably the install script is adding something to $PATH and this is not picked up until the shell is restarted or the configuration file is re-sourced. You could try exec $SHELL at the end of the script to spawn a new shell which will pick up such changes.

like image 126
old greg Avatar answered Oct 29 '25 23:10

old greg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!