Search code examples
shellvagrantvagrantfilevagrant-provisionminiconda

Activate anaconda environment during vagrant shell provisioning


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.


Solution

  • 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