4

So I am trying to install conda on a linux server. For this, I am running a bash script as a root user and I have made a new user which is going to install conda. The new user is "ags". Added below are lines from my shell script.

echo "Getting the conda installer"
su - ags -c "wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O /${install_directory}/ags/miniconda.sh"
echo "Installing conda"
su - ags -c "bash /${install_directory}/ags/miniconda.sh -b -p /${install_directory}/ags/miniconda"


###ERROR PART UNDERNEATH ####
su - ags -c "/${install_directory}/ags/miniconda/condabin/conda init bash"

su - ags -c "export ARCGISHOME=/${install_directory}/ags/arcgis/server; conda activate

However, my output is asking me to restart shell:

root@my_server:~# su - ags -c "/data/ags/miniconda/condabin/conda init bash"
no change     //data/ags/miniconda/condabin/conda
no change     //data/ags/miniconda/bin/conda
no change     //data/ags/miniconda/bin/conda-env
no change     //data/ags/miniconda/bin/activate
no change     //data/ags/miniconda/bin/deactivate
no change     //data/ags/miniconda/etc/profile.d/conda.sh
no change     //data/ags/miniconda/etc/fish/conf.d/conda.fish
no change     //data/ags/miniconda/shell/condabin/Conda.psm1
no change     //data/ags/miniconda/shell/condabin/conda-hook.ps1
no change     //data/ags/miniconda/lib/python3.7/site-packages/xontrib/conda.xsh
no change     //data/ags/miniconda/etc/profile.d/conda.csh
modified      //data/ags/.bashrc

==> For changes to take effect, close and re-open your current shell. <==

root@my_server:~# su - ags -c "conda activate"
-sh: 1: conda: not found

Is there a way to restart my shell (ags) and still keep the script running after that?

vc2310
  • 131
  • 1
  • 1
  • 7
  • What are you expecting `conda activate` to do in this context? Is there more to the script you are trying to run? – merv Jun 17 '20 at 19:53
  • Yes after this I will be making a new conda environment and install some services on it. Conda activate should just activate the base environment. – vc2310 Jun 17 '20 at 19:55
  • In such programmatic cases, that is usually better done with [a YAML environment definition](https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#create-env-file-manually) for the environment, e.g., `/data/ags/miniconda/condabin/conda env create -f environment.yaml`. – merv Jun 17 '20 at 19:59

1 Answers1

3

Conda defines conda activate as a shell function and conda init puts code in init files (here .bashrc) to ensure the function gets defined at the start of interaction shell sessions. An alternative to restarting the session is to instead source ~/.bashrc.

It might be worth noting that a (usually minor) concern with manually re-sourcing an init file in an already active section is that some of the code in .bashrc could be non-idempotent (i.e., running it multiple times has a different effect than running it just once). Fatih Arslan has a nice blog post with tips about writing bash scripts that are idempotent by design.

merv
  • 67,214
  • 13
  • 180
  • 245
  • 2
    Thanks for the advice. It works fine when I am in the terminal as ags user. However since I am trying to run the entire script as root, I am getting this error: Commad: `root@my_machine:~# su - ags -c "source ~/.bashrc"` Ouput: `-sh: 1: source: not found` @merv – vc2310 Jun 17 '20 at 19:48