简体   繁体   中英

Installing NVM / NodeJs / EmberJS inside a Vagrant box

I am having all sorts of fun with this.

The initial issue is that the terminal needs to be restarted after installing NVM so that I can re-initialise with the .bashrc settings and then install NodeJS - so my thinking was that I would build a base box with NVM already installed. That way the terminal will already be initialised with the NVM stuff.

Not the case... Apparently packaging a basebox using Vagrant ignores everything in the /home/vagrant folder. ARRRRRRRGGGGHHHHH!!

REALLY?!!1one

Has anyone had any luck with this? Getting NVM installed inside a vagrant box? or even NodeJs without sudo? It's a horrid rabbit hole atm and I want out!

I suggest you go back to the shell provisioning strategy , I've also went trough hell with this one but is definitely doable. After a lot of googling I found that there are two very vaguely documented settings you require for this to work:

First and the most important part is that you need to enable the creation of symlinks on the VirtualBox instance with this line on your config.vm.provider block, without this NVM just won't work (look in here ):

config.vm.provider "virtualbox" do |vb|

  # (...)

  vb.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/vagrant","1"]
end

Next you'll have to separate your provisioning script in two parts, one that will run the normal apt/git/whatever stuff as root... and another that will run as the default 'vagrant' user:

$rootScript = <<SCRIPT
  # some rooty stuff (just don't forget to include git and curl here)
SCRIPT

## This is the script that will install nvm as the default 'vagrant' user
$userScript = <<SCRIPT
  cd /home/vagrant

  # 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
  nvm alias default 0.10.33

  # You can also install other stuff here
  npm install -g bower ember-cli
SCRIPT

Last, you need to tell vagrant to run the second script with just user privileges (as almost entirely undocumented here ):

config.vm.provision "shell", inline: $rootScript
config.vm.provision "shell", inline: $userScript, privileged: false

That should do. Is not pretty but it works.

Check out this working gist here, and good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM