Search code examples
google-cloud-platformgoogle-compute-enginestartup

GCP install conda on startup script


I'm trying to install conda on the first startup of the VM but am encountering error after error. The startup script is the following:

#!/bin/bash -i
sleep 5
yes Y | sudo apt-get update

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -P /home/user/
bash /home/user/Miniconda3-latest-Linux-x86_64.sh -b
rm /home/user/Miniconda3-latest-Linux-x86_64.sh  
/home/user/miniconda3/bin/conda init bash
source /home/user/miniconda3/etc/profile.d/conda.sh
eval "$(cat /home/user/.bashrc | tail -n +10)"

yes Y | conda create -n test python=3.8

conda activate test

If I run the above directly it works fine. However when it's run as a startup script I get the following in the log:

Aug 29 15:49:51 testconda google_metadata_script_runner[850]: startup-script: /home/user/Miniconda3-latest-Linux-x86_64.sh: line 29: HOME: unbound variable
Aug 29 15:49:51 testconda google_metadata_script_runner[850]: startup-script: bash: /home/user/miniconda3/bin/conda: No such file or directory
Aug 29 15:49:51 testconda google_metadata_script_runner[850]: startup-script: bash: /home/user/miniconda3/etc/profile.d/conda.sh: No such file or directory
Aug 29 15:49:51 testconda google_metadata_script_runner[850]: startup-script: bash: conda: command not found
Aug 29 15:49:51 testconda google_metadata_script_runner[850]: startup-script: bash: conda: command not found
Aug 29 15:49:51 testconda google_metadata_script_runner[850]: startup-script exit status 127
Aug 29 15:49:51 testconda google_metadata_script_runner[850]: Finished running startup scripts.

Could somebody help please? Really struggling to make a proper startup script in GCP work.


Solution

  • After a lot of trial and error I've found the answer here: Boto3 UserData script having unexpected behavior for $HOME variable when installing Miniconda in EC2 instance

    The following startup script works! (user is my username)

    #!/bin/bash
    
    sudo -u user bash <<EOF
    cd /home/user/
    
    wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O /tmp/miniconda3.sh 2> log_miniconda
    bash /tmp/miniconda3.sh -b -p /home/user/miniconda3 2> log_minicondash
    
    /home/user/miniconda3/bin/conda init bash
    source /home/user/miniconda3/etc/profile.d/conda.sh
    source /home/user/.bashrc
    
    conda create -y -n test python=3.8
    
    conda activate test
    EOF