nvm command not available in bash script

I am trying to build a script in in which nvm and eventually node will get installed.
I have installed nvm with cURL. I see the modifications in the .profile or .bashrc file (both work) and when typing the nvm at the bash prompt, it shows the options available etc.
So nvm works. Manually I can install node, but as soon as I put the nvm command in a shell script:

nano test.sh


and run it with:

chmod 755 test.sh

I get:

./test.sh: line 2: nvm: command not found

If it can’t find nvm, I don’t even have to think of

nvm ls-remote 


nvm install ...

I got Ubuntu 14.04 installed and Bash is my shell.

Asked By: okidoki


nvm command is a shell function declared in ~/.nvm/nvm.sh.

You may source either of following scripts at the start of yours to make nvm() available:

. ~/.nvm/nvm.sh
. ~/.profile
. ~/.bashrc
. $(brew --prefix nvm)/nvm.sh  # if installed via Brew
Answered By: webknjaz

If you installed it via brew on OSX, then you can load the brew sourced script into the script env by sourcing it as it details on the install.

I have this in projects to bootstrap them :

brew install nvm
. $(brew --prefix nvm)/nvm.sh
nvm install
Answered By: IanVaughan

Small callout on top of answer provided by @webKnjaZ: I had to source shell function for every command to make it work in my bash script. Something like this:

. ~/.nvm/nvm.sh --version
. ~/.nvm/nvm.sh install 4.4.5
Answered By: rahulthakur319

Same thing happened to be
below is the thing which worked for me:

root@ubuntu:/usr/src/playground# n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local

root@ubuntu:/usr/src/playground# sudo npm install express
+ express@4.16.2
added 49 packages in 129.186s

The above command is a bit complicated, but all it’s doing is copying whatever version of node you have active via nvm into the /usr/local/ directory (where user installed global files should live on a linux VPS) and setting the permissions so that all users can access them.


From all the answers here and in this related nvm issue I created this script (gist) that you can copy or clone (documentation is inlined):

# shellcheck shell=sh
# https://gist.github.com/karfau/dcf98c6eefc2f2132c160f5c14d2112f

# needs to be sourced as part of your script
# 1. tries to configure nvm and run `nvm install`
# 2. checks if the node version is correct based on ./.nvmrc (`v` prefix not supported)
# if both doesn't work, exits with code 1 and some helpful messages

# https://unix.stackexchange.com/a/184512/194420
# https://github.com/nvm-sh/nvm/issues/1290
if [ -f ~/.nvm/nvm.sh ]; then
  echo 'sourcing nvm from ~/.nvm'
  . ~/.nvm/nvm.sh
elif command -v brew; then
  # https://docs.brew.sh/Manpage#--prefix-formula
  BREW_PREFIX=$(brew --prefix nvm)
  if [ -f "$BREW_PREFIX/nvm.sh" ]; then
    echo "sourcing nvm from brew ($BREW_PREFIX)"
    . $BREW_PREFIX/nvm.sh

if command -v nvm ; then
  nvm i
  echo "WARN: not able to configure nvm"

NODE_VERSION="v$(cat .nvmrc)"
which node
ACTIVE_VERSION=$(node --version)
GLOBAL_NPM=$(which npm || echo "not found on PATH")
# .nvmrc can contain only major or major.minor or full version
# so we replace active version with node version and anything afterwards
# if something is left, it's not a match
if [ "${ACTIVE_VERSION%%$NODE_VERSION*}" ] || [ ! -e "$GLOBAL_NPM" ]; then
  echo "expected node '$NODE_VERSION' and npm on path"
  echo "but was '$ACTIVE_VERSION' and npm:'$GLOBAL_NPM'"
  exit 1

Hope it helps
Feedback on the gist or here is welcome.

PS: In the meantime I have updated the gist to use the NVM_DIR environment variable instead of the user directory to support custom installation sections added support for calling nvm use instead of nvm install.

Answered By: karfau

nvm seems to require interactive shell invocation. 
One could do: bash -i -c 'nvm ls'.

Answered By: user536135