42

I am trying to write a shell script to automate my dev environment set-up (install python, nvm, node, mongo etc...). I am using nvm to install Node. It tells you to close and reopen your terminal to start using the nmv command. I tried to source .bashrc and .profile to make the command available right away so I can continue running the script with nvm install, but it doesn't work.

Here is the segment of my script related to installing NVM / Node:

#install nvm and latest node version
# sourcing profile and bashrc is not working here. nvm does not execute the next two lines to install node.

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.30.2/install.sh | bash
source ~/.profile
source ~/.bashrc
nvm install 5.0
nvm alias default node

I get these messages, but please note that I've already run the script and NVM / Node are already installed and working. I can also use nvm and node in the same terminal I run the script from after it completes. It just doesn't work in the script.

=> Downloading nvm from git to '/home/myDir/.nvm'
=> fatal: destination path '/home/myDir/.nvm' already exists and is not an empty directory.
fatal: Not a git repository (or any of the parent directories): .git
fatal: Not a git repository (or any of the parent directories): .git

=> Source string already in /home/myDir/.bashrc
=> Close and reopen your terminal to start using nvm
./install-programs.sh: line 27: nvm: command not found
./install-programs.sh: line 28: nvm: command not found
user137717
  • 2,005
  • 4
  • 25
  • 48
  • 1
    There is an equivalent question over at unix&linux: https://unix.stackexchange.com/questions/184508/nvm-command-not-available-in-bash-script – Motin Dec 12 '18 at 14:14

6 Answers6

69

if you have nvm running on the main shell, you just need to add:

export NVM_DIR=$HOME/.nvm;
source $NVM_DIR/nvm.sh;

in your script

MrE
  • 19,584
  • 12
  • 87
  • 105
  • 1
    You went to all of the trouble to create the NVM_DIR directory and then didn't use it! Might I suggest NVM_HOME and then source $NVM_HOME/nvm.sh. Worked great, by the way. Thanks. – Software Prophets Nov 30 '18 at 15:07
  • 1
    I didn't need to export `NVM_DIR`. Executing just the second line from inside my bash script was enough for nvm to be accessible. – Jannis Ioannou May 08 '20 at 13:19
  • 1
    Setting `NVM_DIR` causes problems if nvm isn't installed into your home dir. For example brew installs nvm into `/usr/local`. Inheriting `NVM_DIR` from the parent is probably a more sound approach. – Ryan Jenkins Apr 08 '21 at 22:04
  • 2
    source isn't available in POSIX... use the `.` if you care about portability to slim linux images like docker alpine or Android termux. This paired wit `if [ -f "$NVM_DIR/nvm.sh" ]; then` helped me use `.nvmrc` when available. Here is the gist https://gist.github.com/simov/cdbebe2d65644279db1323042fcf7624 – Ray Foss Jul 17 '21 at 05:03
  • nvm sets the ```NVM_DIR``` in the environment. So I don't need to export it inside the script. I just source it and then use it. ```source $NVM_DIR/nvm.sh;``` then ```nvm use 16``` – Bryan Apr 20 '23 at 15:56
29

Here is what worked for me.

First install nvm (once and separately) using SSH or console:

$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.30.2/install.sh | bash

Then in your script, loading your profile as follows:

. ~/.nvm/nvm.sh
. ~/.profile
. ~/.bashrc

And with some luck nvm should become available inside the script.

nvm install 4.4.2

Tada!

Steven de Salas
  • 20,944
  • 9
  • 74
  • 82
  • 1
    I put that stuff in the top of my script and voila, perfect. – Andy Dec 24 '16 at 19:05
  • 1
    adding . ~/.nvm/nvm.sh before using it worked for me, thanks! – tylermadison Jun 08 '17 at 16:36
  • already installed node versions are not accessible from within bash scripts? i can change and use nvm, but says that not installed even though the same command works in terminal directly. – trainoasis Dec 11 '20 at 11:42
8

Just put this on top of your script:

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

Worked like a charm here.

3

Nowadays, you can simply do this:

env NODE_VERSION=<dd> /home/<user>/.nvm/nvm-exec npm run front

Simply sourcing nvm.sh didn't work for me (from a systemd .service file), the PATH didn't include ~/.nvm...

Credit where credit is due: https://gist.github.com/joepie91/73ce30dd258296bd24af23e9c5f761aa#gistcomment-2215867

BobHy
  • 1,575
  • 10
  • 23
2

This script works fine for me:

#!/usr/bin/env bash

if [ ! -d ~/.nvm ]; then

  curl https://raw.githubusercontent.com/creationix/nvm/v0.11.1/install.sh | bash
  source ~/.nvm/nvm.sh
  source ~/.profile
  source ~/.bashrc
  nvm install 5.0
  npm install
  npm run front
fi
4444
  • 3,541
  • 10
  • 32
  • 43
Slavik Muz
  • 1,157
  • 1
  • 15
  • 28
0

Make sure NVM is installed in the server/your machine Modify your shell script in the following format, the shell script we have used uses two different node versions in client and in server. Please check whether this works for you?

echo "Removing build files..";
rm -rf server/public/build
echo "Generating New Build....";
source $NVM_DIR/nvm.sh;
cd client
nvm use 14.16.1;
echo "Building Application";
npm install --legacy-peer-deps;
npm run build
echo "Building App Ends";
cd ..
nvm use 16.13.0;
npm install --legacy-peer-deps
Sunilkumar A
  • 111
  • 1
  • 3