0

I am trying to install NVM on AWS Ubuntu 18.04 AMI using a bash script. So I have created a bash script named before_install.sh and written this piece of code.

 #!/bin/bash
 if ! [ -x "$(command -v nvm)" ]; then
    echo "Error: 'nvm' is not installed." >&2
    echo "Installing nvm ..."
    wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.1/install.sh | bash
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
 else
    echo "'nvm' tool is already found"
 fi

But when I execute this script using bash before_install.sh and I checked whether nvm is installed or not by using commands such as command -v nvm or nvm --version, It shows that nvm that no package named nvm is available even when it is installed. However, through some research, I identified and added source .bashrc after nvm installation at the end in the bash script. But still, I have to run source .bashrc again manually in the terminal and then nvm works.

Why nvm is behaving like this or what is the cause of problem.

oguz ismail
  • 1
  • 16
  • 47
  • 69
marshmello
  • 71
  • 4
  • 10
  • Are you ssh into the instance using `ubuntu` user? If you let the EC2 `user_data` install nvm, it will installed using `root` user and, so that `"$HOME/.nvm` will be `/root/.nvm`. – Giorgio Cerruti Apr 28 '20 at 14:31
  • @GiorgioCerruti Yes I am doing ssh into the instance using ubuntu user – marshmello Apr 29 '20 at 05:30
  • @NicoHaase this can be solved by running ```source ~/.bashrc```. But still here is a catch. When I added the line `source ~/.bashrc` at the last this does not work. So I have to run `source ~/.bashrc` manually from the terminal and then it works. Refer: https://stackoverflow.com/questions/43659084/source-bashrc-in-a-script-not-working But I want to use this command inside bash script otherwise what's the point of automation if I have to run all things manually. – marshmello Apr 29 '20 at 08:40
  • Alright, `nmv` installation is designed to be installed for users not globally, which means that every user must have his own `$NVM_DIR` configured. As I said, user data installs `nvm` only for the `root` user (https://github.com/nvm-sh/nvm/issues/1533). You might found this helpful https://stackoverflow.com/questions/11542846/nvm-node-js-recommended-install-for-all-users – Giorgio Cerruti Apr 29 '20 at 10:43

0 Answers0