0

I am trying to make a bash script to activate conda. The commands work in my terminal but not inside bash. What might the reason be? It isn't fixed by conda init.

Minimal recreation steps, using bash -c instead of a bash file:

  • conda activate ./build/env

    works

  • bash -c "conda activate ./build/env"

    error: CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.

  • bash -c "conda init"

    no benefit: No action taken.

I am using miniconda.

My full script is:

#!/bin/bash --init-file

# Change to the current directory
cd "$(dirname "$(realpath "${BASH_SOURCE[0]}")")" || exit

if [ ! -d ./environment ]; then
  mamba env create -p ./environment -f ./environment.yml
fi
conda activate ./environment

The purpose of the script is to make setting up the environment simpler. It is intended for the project to be used by many users after cloning from source control. The user must have conda and mamba installed (it will say this in a text document). My goal is ease of use so I would like it to 'just work' as much as possible, for example when executed with bash my_script.sh and ./my_script.sh.

user38964
  • 1
  • 1
  • Try adding `source /etc/profile.d/conda.sh` to the top of your bash script – FlyingTeller Aug 10 '22 at 10:22
  • Thank you @FlyingTeller. The purpose of the script is to be portable between computers. Is there a way to do it without hard coding the miniconda dir? – user38964 Aug 10 '22 at 10:48
  • You could also source `.bashrc` – FlyingTeller Aug 10 '22 at 10:50
  • 2
    "*The purpose of the script is to be portable between computers.*" - please elaborate this *in the question body*. How do you ensure Conda is installed and on PATH on the machines? How is the existence of the same environment guaranteed? How is the script invoked? (give an example call). There are other answers that cover how environment activation works and what needs to be done in scripts. E.g., https://stackoverflow.com/a/72395091/570918, but again depends on how the script is called (e.g., `sh script.sh` or `./script.sh`) – merv Aug 10 '22 at 13:42

0 Answers0