2

This seems like a question that must have been asked a hundred times, but I can't seem to find the answer:

I'm installing miniconda on a remote machine with this script:

#!/bin/bash

set -xeu

mkdir -p miniconda3/
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -f
~/miniconda3/bin/conda init bash
echo 'export PATH="~/miniconda3/bin/:$PATH"' >> ~/.bash_profile

I then log out of the machine and back in and try running the following script:

#!/bin/bash

set -eux

# Test that conda is installed for interactive use
echo "conda --version" | bash -i

# Test the conda is installed for script use
conda --version

For the second call (script use) I get an error conda: command not found.

I get the same behaviour when appending to ~/.bashrc rather than ~/.bash_profile in the last line of my install script or if I don't write to any of those.

According to the bash man page, I should be able to set the name of a config file in the BASH_ENV environment variable, but how would I set that variable?

I seem to be missing something but I don't understand what.

Ingo
  • 1,103
  • 8
  • 17
  • How do you run the script? Why not add a `-i` or `-l` to the shebang? – merv Jul 06 '23 at 16:31
  • Of course that would be an option for this particular script, but the script is intended to test that conda can *generally* be run in scripts and I can't ensure that all of those would have the correct flags. – Ingo Jul 07 '23 at 07:01
  • Oh, @merv, I just understood the point with the shebang. That's a clever idea, but I think my previous comment still somewhat applies. – Ingo Jul 07 '23 at 07:04
  • Conda can't be run in scripts without setup - that's what interactive shell session provides. For programmatic execution of code in specific environments, there is `conda run`. Otherwise, you need to include in the script some code to setup Conda (e.g., as in [this thread](https://stackoverflow.com/q/34534513/570918), or manually sourcing the `conda.sh` indicated in Quiescent's answer in the shell script). Last resort, you can manually add the `~/miniconda3/condabin/` folder to PATH (you should *not* add the `bin/` folder manually). – merv Jul 07 '23 at 15:22
  • Thanks @merv, I will then try to somehow enforce the `-i`, which should work. What's wrong with adding the `bin/` folder manually? – Ingo Jul 07 '23 at 20:38
  • Conda environment activation should manage PATH and you don’t want to leave the base environment on PATH permanently because it can give you unreproducible behavior. E.g., you might run code in an environment and export it, but turns out the code depended on something in base. The preference is to only add `condabin` which is a select subset of binaries/scripts from base that makes the `conda` command available in the shell. – merv Jul 08 '23 at 13:54
  • That makes a lot of sense. Thank you for the explanation. I usually use plain virtual envs and I always felt that this behaviour was one of the odd things about conda. So you resolved that point. – Ingo Jul 10 '23 at 07:49

1 Answers1

-1

Try with auto_activate_base: true to ~/.condarc in case it is not activated by default.

echo "auto_activate_base: true" >> ~/.condarc

Another way is to source the environment in the script.

$ echo ". $HOME/miniconda3/etc/profile.d/conda.sh" >> ~/.bashrc
Quiescent
  • 1,088
  • 7
  • 18
  • thank you for your answer. Unfortunately, that doesn't really answer my question as both would only take effect in "interactive" bash invocations, which work for me. The problem is about "non-interactive" bash invocations. – Ingo Jul 07 '23 at 07:03