`command not found` when using Bash Script to Install & Run a Software - bash

I am trying to create a Bash script that downloads a .sh file, runs it to install a program (Anaconda for Python), then run the command conda init to activate the default Anaconda environment base
However, running my bash script as shown
# !#/bin/bash
wget https://repo.anaconda.com/archive/Anaconda3-2021.05-Linux-x86_64.sh
bash Anaconda3-*-Linux-x86_64.sh -b
echo 'export PATH=~/anaconda3/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
conda init
conda env create -f environment.yml
gives the error
./foo.sh: line 6: conda: command not found
./foo.sh: line 7: conda: command not found
Why is conda not found when trying to use it from within the Bash script, but is found if I relogin to the terminal?
How can we fix the Bash script so that using conda works?

Related

'Conda activate base' fails to load when executed from Matlab on Ubuntu (18.04). Error: conda: command not found

My Conda working perfectly from the ubuntu terminal. I can easily activate the environment I need using the following command
$ conda activate base
and it loads the base environment on the terminal.
But here, in MATLAB, when I try to activate conda environment using the following command it returns an error
>> system('conda activate base')
/bin/bash: conda: command not found
Based on other posts with similar discussions, I have tried the following but to no luck
>> system('/home/anirudha/anaconda3/bin/conda activate base')
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run
$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
>> system('conda init bash');
/bin/bash: conda: command not found
I have also tried solutions from this but to no success again.
My purpose is to execute a few gdal functions from the command line through a MATLAB script. I have tried doing this on my windows PC, and everything works perfectly. But not on my Linux station. Am I missing something?

anaconda: could not use shell script to switch environment

I have anaconda installed. I am using centOS 8.2, anaconda installation package name is: Anaconda3-2020.11-Linux-x86_64.sh ,
With the initialization, the environment is base. as it shows in the following command line:
(base) [sjj#localhost ~]$
I wrote a shell script named as env.sh as follows, the purpose it to switch environment:
#!/bin/sh
conda activate ray-tutorial
It always say:
(base) [sjj#localhost ~]$ sh env.sh
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run
$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
But if I directly type the command to switch the environment, it works: My question is why shell script does not work in this case?
(base) [sjj#localhost ~]$ conda activate ray-tutorial
(ray-tutorial) [sjj#localhost ~]$

Activating conda environment from bash script

I would like to change my conda environment from a bash script.
I want to run bash script_yxz, where 'script_xyz' is like:
#!/bin/bash
conda activate my_env
and switch to my_env.
This already works if I run source script_yxz.
But I have the problem that I am not able to 'source' on remote machines with 'sshpass'.
To better understand my purposes, my goal is to run on my terminal
sshpass -p "password" ssh -o user#server "bash script_xyz"
and changing the environment on the server.
This is why I need to use bash instead of source.
I have read a lot of solutions on various forums but none of them works.
seems like the conda script is not imported by default so this should fix it
source ~/anaconda3/etc/profile.d/conda.sh
conda activate <env>
#!/bin/bash
eval "$(conda shell.bash hook)"
source ~/anaconda3/etc/profile.d/conda.sh
conda create -n testing python=3.10 -y
conda activate testing
python --version
#output
You can use bash,zsh or any shell aliases for this purposes. You just add
alias my_conda='source /home/$USER/anaconda3/bin/activate && conda activate MyPy38'
line into the .bashrc,.zshrc or .any_other_shell_rc.
"N.B. My environment name is MyPy38". So,replace it according name as well as the path /home/$USER/anaconda3.
Also you can create separate file for aliases. Just create a file called .bash_aliases and add
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
lines to .bashrc,.zshrc or .any_other_shell_rc and keep the command
alias my_conda='source /home/$USER/anaconda3/bin/activate && conda activate MyPy38'
into the .bash_aliases. Now, source ~/.zshrc ~/.bashrc or just close and open a new terminal. Run the command my_conda and BOOM!
Also, you can add some other aliases for jupyter-notebook jupyter-lab spyder etc. like
# Just activate my conda
alias my_conda='source /home/$USER/anaconda3/bin/activate && conda activate MyPy38'
# Open Jupyter Notebook in my Env
alias my_jupn='source /home/$USER/anaconda3/bin/activate && conda activate MyPy38 && jupyter-notebook'
# Open Jupyter Lab in my Env
alias my_jupl='source /home/$USER/anaconda3/bin/activate && conda activate MyPy38 && jupyter-lab'
# Open Spyder in my Env
alias my_spyder='source /home/$USER/anaconda3/bin/activate && conda activate MyPy38 && spyder'
To confirm active environment name python code
import sys
print(sys.executable)

Activating conda env using shell script

Im trying to extract current working directory and activate conda environment using bash script. My shell script
app_path=$(dirname $(readlink -f $0))
source /conda/bin/activate $app_path;
This issue im facing is when I run the script as
sh script.sh
it is not activating the conda env
If i run with source script.sh
It is not able to recognise readlink command. How to get both of them working together?

Failed to activate virtualenv via shellscript

I'm trying to activate my pyenv-virtualenv environment through very simple bash script like this.
set -e
pyenv activate myenv
But I can't activate my env with below error:
Failed to activate virtualenv.
Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.
I can activate it in command line, so I think it's due to my shell environment.
I use fish shell, but it's launched after $PATH settings read in bash shell.
I show you my settings.
.bash_profile:
export LANG="ja_JP.UTF-8"
export XDG_CONFIG_HOME="$HOME/.config"
export PATH="/sbin"
export PATH="/usr/sbin:$PATH"
export PATH="/bin:$PATH"
export PATH="/usr/bin:$PATH"
export PATH="/usr/local/bin:$PATH"
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
exec fish
And my config.fish:
status --is-interactive; and source (pyenv init -|psub)
status --is-interactive; and source (pyenv virtualenv-init -|psub)
What's wrong?
I'm using:
OS: OS X 10.13.5
fish: v2.7.1
pyenv: v1.2.5
pyenv-virtualenv: v1.1.3
I believe the cause is that you are initializing PyEnv in the Fish shell environment but then calling PyEnv in the Bash shell environment (via your Bash script). To solve this problem, you could try either of the following potential solutions:
Initialize PyEnv in your Bash environment (in addition to your Fish environment)
Write your script in Fish instead of Bash
Since PyEnv seems so have better support for Bash than Fish, let's focus on option #1. Step 3 of the PyEnv installation guide suggests the following command for adding PyEnv initialization to your ~/.bash_profile:
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
After running that command, I suspect your script will function as you expect. If it does not, you could try adding the same PyEnv initialization snippet (that you just added to your ~/.bash_profile) to the top of your Bash script.

Resources