Activate anaconda environment during vagrant shell provisioning - shell

I am using vagrant (1.9.1 on MacOSX Sierra) to provision ubuntu/xenial64 box on VirtualBox to run a python app. I am unable to activate a conda environment using the normal shell command source while provisioning. In my bootstrap.sh, I have the following lines for creation of a new environment and then switching to it.
#!/usr/bin/env bash
set -e # Exit script immediately on first error.
set -x # Print commands and their arguments as they are executed.
/home/ubuntu/miniconda3/bin/conda create --name envmycondaenvironment python=3.5 # environment with python3.5
source activate envgatherurls
I receive the following error from vagrant.
==> default: + source activate envmycondaenvironment
==> default: /tmp/vagrant-shell: line 21: activate: No such file or directory
Why is activate not found by the shell script? I verified that /home/ubuntu/miniconda3/bin/ where activate can be found has been added to PATH in the .bashrc file.

The command activate is provided by conda and is not automatically added to the PATH environment variable. Please note that the bootstrap.sh script runs as root and not the vagrant user. So you need to make sure that the .bashrc for the root user has /home/ubuntu/miniconda3/bin in it's path. If I were you, I had rather do this:
#!/usr/bin/env bash
set -e # Exit script immediately on first error.
set -x # Print commands and their arguments as they are executed.
export PATH=/home/ubuntu/miniconda3/bin:$PATH
conda create --name envmycondaenvironment python=3.5 # environment with python3.5
source activate envgatherurls

Related

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

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?

'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?

bash script to run jupyter notebook in virtualenv

To speed up launching projects I created a small bash script which does the following:
takes an argument (project name)
moves to the directory of that project
starts a virtual environment
starts a jupyter notebook
#!/bin/bash
if [ "$1" == "k3" ]; then
project_path="tau-code/k3-analysis/"
fi
codepath="/media/peter/somedrive/code"
full_path="$codepath/$project_path"
# Go to directory of project
cd $full_path
# Start environment & notebook if available
pipenv shell
jupyter notebook --ip=0.0.0.0
It activates the environment, but does not run the jupyter command. When I exit the environment I see the error:
line 16: jupyter: command not found
I can manually type jupyter notebook --ip=0.0.0.0 in my newly created environment and that does work.
What may be the problem?
pipenv shell starts a new shell which must be deactivated by using exit. In your script any commands following the call to pipenv shell are not executed in that new shell. Instead they are executed in the same bash shell after the virtual environment shell is closed. You should use pipenv run jupyter notebook --ip=0.0.0.0
See pipenv documentation:
shell will spawn a shell with the virtualenv activated. This shell can be deactivated by using exit.
run will run a given command from the virtualenv, with any arguments forwarded (e.g. $ pipenv run python or $ pipenv run pip freeze).
Hi you need to add this
pipenv run jupyter notebook

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)

Cannot run source activate with conda in Fish-shell

I follow conda_PR_545, conda issues 4221 and still not working on Ubuntu.
After downloading conda.fish from here, and mv it to anaconda3/bin/.
Add "source /home/phejimlin/anaconda3/bin/conda.fish" at the end of ~/.config/fish/config.fish.
conda activate spark_env
Traceback (most recent call last):
File "/home/phejimlin/anaconda3/bin/conda", line 6, in
sys.exit(conda.cli.main())
File "/home/phejimlin/anaconda3/lib/python3.6/site-packages/conda/cli/main.py", line 161, in main
raise CommandNotFoundError(argv1, message)
TypeError: init() takes 2 positional arguments but 3 were given
or
activate spark_env
Error: activate must be sourced. Run 'source activate envname'
instead of 'activate envname'.
Do I miss something?
As of fish 2.6.0 conda 4.3.27: the following steps may change as issue is addressed
update config
Take note of your conda's location
conda info --root
/Users/mstreeter/anaconda # this is my <PATH_TO_ROOT>
Add line to ~/.config/fish/config.fish
source <PATH_TO_ROOT>/etc/fish/conf.d/conda.fish
update convention
Typically you'd run the following from bash
source activate <environment>
source deactivate <environment>
Now you must run the following from fish
conda activate <environment>
conda deactivate <environment>
issues
so after doing this I'm not able to set fish as my default shell and have it still work properly with conda. Currently, I must first enter my default shell, and enter fish and the shell works as expected. I'll update this after I find out how to get it working completely without the need to explicitly choose fish each time I log into my terminal
If you follow https://github.com/conda/conda/issues/2611, the steps are (from start):
[root#6903a8d80f9b ~]# fish
root#6903a8d80f9b ~# echo $FISH_VERSION
2.4.0
root#6903a8d80f9b ~# bash Miniconda2-4.3.11-Linux-x86_64.sh -b -p /conda
root#6903a8d80f9b ~# source /conda/etc/fish/conf.d/conda.fish
root#6903a8d80f9b ~# conda activate root
root#6903a8d80f9b ~# conda create -yn fishtest (root)
Fetching package metadata .........
Solving package specifications:
Package plan for installation in environment /conda/envs/fishtest:
#
# To activate this environment, use:
# > source activate fishtest
#
# To deactivate this environment, use:
# > source deactivate fishtest
#
root#6903a8d80f9b ~# conda activate fishtest (root)
root#6903a8d80f9b ~# (fishtest)
root#6903a8d80f9b ~# conda deactivate fishtest (fishtest)
Adding conda's bin directory to PATH isn't recommended as of conda 4.4.0
https://github.com/conda/conda/blob/master/CHANGELOG.md#440-2017-12-20
All you need to do is adding
source <path-to-anaconda>/etc/fish/conf.d/conda.fish
to config.fish.

Resources