19

my anaconda (4.5.4) works fine as long as I just use it via a linux terminal (bash shell). However, running conda commands in a bash script does not work at all.

The script test.sh containes these lines:

#!/bin/bash
conda --version
conda activate env

Now, running bash test.sh results in the error test.sh: line 2: conda: command not found test.sh: line 3: conda: command not found

As recommended for anaconda version > 4.4 my .bashrc does not contain

export PATH="/opt/anaconda/bin:$PATH",

but

. /opt/anaconda/etc/profile.d/conda.sh

Thank you.

merv
  • 67,214
  • 13
  • 180
  • 245
randomwalker
  • 1,573
  • 1
  • 9
  • 14
  • 2
    This is related: https://unix.stackexchange.com/questions/65751/how-to-get-functions-propagated-to-subshell. Since conda 4.4, the `conda` is defined as a bash function, no longer an executable. – darthbith Oct 12 '18 at 16:05
  • 1
    Your hint guided me towards the right direction. Thank you! – randomwalker Oct 15 '18 at 09:41

4 Answers4

27

I solved the problem thanks to @darthbith 's comment.

Since conda is a bash function and bash functions can not be propagated to independent shells (e.g. opened by executing a bash script), one has to add the line

source /opt/anaconda/etc/profile.d/conda.sh

to the bash script before calling conda commands. Otherwise bash will not know about conda.

randomwalker
  • 1,573
  • 1
  • 9
  • 14
  • This worked great! Thanks. Mine was in `/home/ubuntu/miniconda3/etc/profile.d/conda.shonda.sh`. – SaTa Jul 03 '19 at 16:58
  • 1
    is there a way to propagate this? my bash script runs other bash scripts that wants to use conda too, do I need to include that call in every single one of them? – jangorecki Aug 07 '19 at 18:36
  • As in this github issue: https://github.com/conda/conda/issues/7980. Functions are not exported by default to be made available in subshells. I'd recommend you do: source ~/anaconda3/etc/profile.d/conda.sh conda activate my_env – Yuchao Jiang Sep 11 '20 at 05:41
  • @SaTa they should've spelled it "shawnda" – wordsforthewise Aug 23 '23 at 17:10
2

If @randomwalker's method doesn't work for you, which it won't any time your script is run in a more basic shell such as sh, then you have two options.

  1. Add this to your script: eval $(conda shell.bash hook)

  2. Call your script with: bash -i <scriptname> so that it runs in your interactive environment.

abalter
  • 9,663
  • 17
  • 90
  • 145
  • 1
    "... which it won't any time your script is run in a more basic shell such as sh" - baller! Perfect answer, btw. Works very well. – CodingInCircles Jun 10 '20 at 22:19
  • 3
    There's potential for subtle bugs if you don't explicitly quote the expansion whose result is being passed to `eval`. Thus, it should be more like `eval "$(conda shell.bash hook)"` so any glob expressions aren't replaced with matching filenames before `eval` gets its argument list. (That's not the only side effect of failing to quote that can impact this use case, but it's the easiest one to explain in the span of a comment). – Charles Duffy Oct 11 '20 at 03:44
1

Let's say you try to access user name with "miky" @ "server" address.First when you login to your user ; learn conda path with "which conda" then probably you will get a path such as "/home/miky/anaconda3/bin/conda" then put your conda commands as follow (in my example i use conda to install a mysql plugin forexample.): shh miky@server -t "/home/miky/anaconda3/bin/conda install -y -c anaconda mysql-connector-python" thats all.

RedArrow
  • 588
  • 8
  • 18
  • `which` can't see functions, aliases, or shell builtins in bash, as it's an external command rather than a shell built-in. Use `type conda` instead. – Charles Duffy Oct 10 '20 at 01:43
-1

do sudo ln -s /home/<user>/miniconda3/etc/profile.d/conda.sh /etc/profile.d/conda.sh and try again. This should activate conda for all users permenantly

source

Sharku
  • 1,052
  • 1
  • 11
  • 24
  • Thanks, but I am not an administrator on the remote server I am working on. And conda works fine in terminal, but not in a bash script. Via `. /opt/anaconda/etc/profile.d/conda.sh` I already activated conda for myself. – randomwalker Oct 12 '18 at 13:14
  • its just that `conda: command not found` suggests that it is not activated. probably the conda.sh is not doing its job. Ah and their should be no space between /opt/... and the point in the beginning. – Sharku Oct 12 '18 at 13:43
  • Only a script that's started by a process with a parent that is a login shell will see environment variables set by `profile.d`, `.bash_profile`, etc. Not all scripts fall into this category. – Charles Duffy Aug 19 '21 at 16:23