20

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.

2
  • 2
    This is related: unix.stackexchange.com/questions/65751/…. Since conda 4.4, the conda is defined as a bash function, no longer an executable. Commented Oct 12, 2018 at 16:05
  • 1
    Your hint guided me towards the right direction. Thank you! Commented Oct 15, 2018 at 9:41

4 Answers 4

30

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This worked great! Thanks. Mine was in /home/ubuntu/miniconda3/etc/profile.d/conda.shonda.sh.
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?
As in this github issue: 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
@SaTa they should've spelled it "shawnda"
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.

2 Comments

"... 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.
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).
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.

1 Comment

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.
-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

3 Comments

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.
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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.