1

This code seems to work fine on 'vagrant up', but when I 'vagrant ssh' into the vm I have to redo everything in the "#doesn't take" block, even though I saw the successful output while the scripts were being run.

In Vagrantfile:

Vagrant.configure(2) do |config|

  config.vm.box = "ubuntu/trusty64"

  config.vm.network "forwarded_port", guest: 9000, host: 9000, auto_correct: true
  config.ssh.forward_agent = true

  config.vm.synced_folder ".", "/home/vagrant/www", create: true, group: "vagrant", owner: "vagrant"

  config.vm.provision :shell, path: "vagrant/rootScript.sh"
  config.vm.provision :shell, privileged: false, path: "vagrant/userScript.sh" 
end

rootScript.sh:

sudo apt-get update
sudo apt-get install -y git
sudo apt-get install python-software-properties python g++ make

sudo apt-get update

userScript.sh:

cd www           
touch test.txt    # this is successfully created in the correct folder

whoami            # 'vagrant'
echo "${PWD}"     # 'home/vagrant/www'

# # Installing nvm
wget -qO- https://raw.github.com/creationix/nvm/master/install.sh | sh

# # This enables NVM without a logout/login
export NVM_DIR="/home/vagrant/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm

# Install a node and alias
nvm install 0.10.33

# ************** doesn't take **************    
nvm use 0.10.33
npm install -g npm@latest
npm install -g bower grunt-cli
npm install
bower install
# *******************************************

whoami                   # still 'vagrant'
echo "${PWD}"            # still 'home/vagrant/www'

Why do these commands need to be re-run in ssh? And is there anything I can do to make them 'take' while running as a script? Driving me nuts. Thanks for the help.


UPDATE -- just discovered that the npm and bower commands DID take -- only command that does not seem to be working in the script is 'nvm use 0.10.33'

4

1 回答 1

1

nvm use only sets the node version for that shell session.

To make it work for any new shells you should instead use nvm alias default 0.10.33. This is mentioned briefly in the nvm README:

To set a default Node version to be used in any new shell, use the alias 'default':

nvm alias default stable

This will only work on new shells though so if you want to perform any commands at the same time you will need to use nvm use 0.10.33 as well.

In your case you probably just want to add the nvm alias default 0.10.33 command right at the end of your node configuration in userScript.sh.

于 2015-04-09T18:10:12.790 回答