Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is nvm command installed as root and also not found during vagrant bootstrap.sh?

When I try to install nvm and test that it's installed in my vagrant shell provisioning script, using:

sudo wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.3/install.sh | bash
source ~/.bashrc
nvm

I get:

==> default: => Downloading nvm as script to '/root/.nvm'
==> default: => Appending source string to /root/.bashrc
==> default: => You can now install Node.js by running `nvm install`
==> default: /tmp/vagrant-shell: line 7: nvm: command not found

First I don't want it to install as root, I want to install it as the vagrant user in /home/vagrant to avoid permission issues.

Second why is the command not found when I ran source .bashrc as specified in the nvm installation instructions here https://github.com/creationix/nvm?

I have a solution which I'm adding now.

like image 540
tobuslieven Avatar asked Jul 24 '16 18:07

tobuslieven


People also ask

How do you know NVM is installed or not?

zip file extraction, because it is FREE.) Then to check if nvm is properly installed, open a new command prompt terminal and type nvm . Once it is verified that it is installed you can move on to the next step. The version can be a NodeJS version or "latest" (for the latest stable version).

Does NVM also install npm?

nvm manages node. js and npm versions. It's designed to be installed per-user and invoked per-shell. nvm works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), in particular on these platforms: unix, macOS, and windows WSL.


1 Answers

So first if you want to install somethnig as the vagrant user during a bootstrap.sh script, the easiest way to do that is to add another provisioner script to your Vagrantfile which is run in unprivileged mode:

config.vm.provision "ScriptRunAsRoot", type:"shell", path: "Vagrantdata/rootUserBootstrap.sh"
config.vm.provision "ScriptRunAsVagrantUser", privileged: false, type:"shell", path: "Vagrantdata/vagrantUserBootstrap.sh"

Then to get the nvm command in this situation, you have to be sure to run the nvm.sh script directly, ie not via the .bashrc file as stated in the nvm installation instructions. This is because .bashrc has a line which exits early if it is being run in a non interactive shell, which I guess vagrant is. From .bashrc:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

So instead of the source ~/.bashrc, you need:

source ~/.nvm/nvm.sh
like image 168
tobuslieven Avatar answered Nov 14 '22 23:11

tobuslieven