Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use nvm from bash script

Tags:

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 
like image 710
user137717 Avatar asked Feb 04 '16 16:02

user137717


People also ask

Does NVM require admin rights?

NVM (Node Version Manager) is the best way to run multiple versions of NodeJS on the same machine. It's not only for Windows but in this post I will talk about my experience to install NVM on a Windows machine without Admin Rights.


2 Answers

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

like image 156
MrE Avatar answered Sep 28 '22 02:09

MrE


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!

like image 35
Steven de Salas Avatar answered Sep 28 '22 03:09

Steven de Salas