Skip to content

Instantly share code, notes, and snippets.

@simbo
Last active July 13, 2022 14:24
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simbo/24f50efbb1f941b65e07 to your computer and use it in GitHub Desktop.
Save simbo/24f50efbb1f941b65e07 to your computer and use it in GitHub Desktop.
setup node.js with nvm within vagrant
##
# add this to your provision.sh
#
# [!] run as 'vagrant' like this:
# su vagrant -c "source ${PROVISION_DIR}/provision-node.sh"
#
# [!] please keep the echo messages. those actions could take some more time without any output, so let the user know.
##
# install latest nvm
git clone https://github.com/creationix/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`
source ~/.nvm/nvm.sh
echo "source ~/.nvm/nvm.sh" >> ~/.bashrc
# install latest stable node.js
echo "Installing node.js... (please be patient)"
nvm install stable &> /dev/null
nvm alias default stable
# install global node packages
echo "Installing global node.js packages... (please be patient)"
# change 'gulp' to 'grunt' depending on project setup
npm install -g gulp bower npm-check-updates
# install project dependencies and build
cd /vagrant/
echo "Installing local node.js packages... (please be patient)"
npm install
bower install
# see package.json for respective build task
npm run build-dev
@alexserver
Copy link

What do you mean by : ?

# [!] run as 'vagrant' like this:
# su vagrant -c "source ${PROVISION_DIR}/provision-node.sh"

I usually would put this script in Vagrantfile as:

config.vm.provision "shell", path: "./provision-node.sh"

How can I tell from Vagrantfile to execute as vagrant ?

@gauteh
Copy link

gauteh commented Jul 8, 2019

privileged: false

@cristiancmello
Copy link

cristiancmello commented May 14, 2020

It can be simpler without having to create another script!

config.vm.provision "shell", privileged: false, inline: <<-SHELL
  # Install NVM
  git clone https://github.com/creationix/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`
  source ~/.nvm/nvm.sh
  echo "source ~/.nvm/nvm.sh" >> ~/.bashrc

  # Install Node
  echo "Installing Node.js (please be patient)"
  nvm install stable &> /dev/null
  nvm alias default stable

  # install global node packages
  echo "Installing global node.js packages... (please be patient)"
  # change 'gulp' to 'grunt' depending on project setup
  npm install -g gulp bower npm-check-updates

  # install project dependencies and build
  cd /vagrant/
  echo "Installing local node.js packages... (please be patient)"
  npm install
  bower install
  # see package.json for respective build task
  npm run build-dev
SHELL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment