Search code examples
node.jsamazon-web-servicesbashamazon-ec2nvm

EC2 Userdata script : nvm cannot find .bashrc


I have these four lines at the start of my ec2 user data script to install nvm and Node.js. This never seems to succeed.

touch ~/.bashrc
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install 16

The nvm installer cannot find the .bashrc and conks out:

Cloud-init v. 22.2.2 running 'modules:final' at Fri, 24 Jan 2025 07:28:19 +0000. Up 14.32 seconds.
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 16555  100 16555    0     0  54955      0 --:--:-- --:--:-- --:--:-- 55000
=> Downloading nvm as script to '/.nvm'

=> Profile not found. Tried ~/.bashrc, ~/.bash_profile, ~/.zprofile, ~/.zshrc, and ~/.profile.
=> Create one of them and run this script again
   OR
=> Append the following lines to the correct file yourself:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
/var/lib/cloud/instance/scripts/part-001: line 6: nvm: command not found

Not sure what I'm doing wrong. Any ideas?


Solution

  • This seems like a bug in nvm. I was able to reproduce the error on a local Ubuntu container, and then I spun up a new container, replaced nvm 0.39.7 with the latest 0.40.1, and it worked just fine:

    touch ~/.bashrc
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
    source ~/.bashrc
    nvm install 16