Search code examples
bashnvmgoogle-cloud-shellgoogle-cloud-shell-editor

How to run nvm command from bash file in GCP Cloud Shell (global install)?


What we're trying to do

We are using Cloud Shell Editor (IDE) on GCP.
According to the documentation for Environment customization script:

Cloud Shell automatically runs the script, $HOME/.customize_environment, when your instance boots up. Unlike .profile or .bashrc, this script runs once when Cloud Shell boots (rather than once for each shell login).

This script runs as root and you can install any packages that you want to exist in each Cloud Shell session using Debian package management commands.

For example, if you'd like to have erlang installed on Cloud Shell, your .customize_environment file will look like this:

#!/bin/sh
apt-get update
apt-get -y install erlang

We are trying to pre-install node versions and automate configuration for our developers using nvm.


Where we have issues

We've looked into several articles on making nvm callable from within a bash script file (the .customize_environment file):

None of these answers seem to resolve our issue.

GCP nvm details

Cloud Shell pre-installs nvm (globally); it is located at /usr/local/nvm/nvm.sh.
There is no ~/.nvm folder.

I can run nvm without issue from Cloud Shell (command line) directly.
I cannot run nvm (nor using the for path) from a bash file.

Closest attempt

Running the following comes close, but I'm limited on privileges that you can't sudo around due to Cloud Shell limitations:

. /usr/local/nvm/nvm.sh
nvm install 14

Results in:

Downloading and installing node v14.15.4...
mkdir: cannot create directory ‘/usr/local/nvm/.cache/bin/node-v14.15.4-linux-x64’: Permission denied
creating directory /usr/local/nvm/.cache/bin/node-v14.15.4-linux-x64/files failed
Downloading https://nodejs.org/dist/v14.15.4/node-v14.15.4-linux-x64.tar.xz...
Warning: Failed to create the file
Warning: /usr/local/nvm/.cache/bin/node-v14.15.4-linux-x64/node-v14.15.4-linux-
Warning: x64.tar.xz: No such file or directory

curl: (23) Failed writing body (0 != 966)
Binary download from https://nodejs.org/dist/v14.15.4/node-v14.15.4-linux-x64.tar.xz failed, trying source.
grep: /usr/local/nvm/.cache/bin/node-v14.15.4-linux-x64/node-v14.15.4-linux-x64.tar.xz: No such file or directory
Provided file to checksum does not exist.
Binary download failed, trying source.
Detected that you have 4 CPU core(s)
Running with 3 threads to speed up the build
mkdir: cannot create directory ‘/usr/local/nvm/.cache/src’: Permission denied
creating directory /usr/local/nvm/.cache/src/node-v14.15.4/files failed
Downloading https://nodejs.org/dist/v14.15.4/node-v14.15.4.tar.xz...
Warning: Failed to create the file
Warning: /usr/local/nvm/.cache/src/node-v14.15.4/node-v14.15.4.tar.xz: No such
Warning: file or directory

curl: (23) Failed writing body (0 != 965)
Binary download from https://nodejs.org/dist/v14.15.4/node-v14.15.4.tar.xz failed, trying source.
grep: /usr/local/nvm/.cache/src/node-v14.15.4/node-v14.15.4.tar.xz: No such file or directory
Provided file to checksum does not exist.

Important lines: mkdir: cannot create directory ‘...’: Permission denied

I can't sudo around it, run usermod (e.g. usermod -a -G staff $(whoami)), even though Google's documentation says I should be able to:

When you set up a Cloud Shell session, you get a regular Unix user account with a username based on your email address. With this access, you have full root privileges on your allocated VM and can even run sudo commands, if you need to.


Question

How can we use nvm within a bash script file in GCP's Cloud Shell?


Solution

  • I managed to resolve this with the help of comments by @Kolban and @HarshManvar.

    I assume this solution would also work for any other scripts that aren't working with direct calls from .customize_environment.

    The example below is influenced by the answer found here. The code installs a specific node version.

    Note: nvm is globally installed in Cloud Shell by default.

    #!/usr/bin/bash
    sudo su -c '. /usr/local/nvm/nvm.sh && nvm install 14'