Skip to content

Instantly share code, notes, and snippets.

@hugronaphor
Last active February 15, 2020 11:13
Show Gist options
  • Save hugronaphor/7d346eba90730517bc1985811e7a1098 to your computer and use it in GitHub Desktop.
Save hugronaphor/7d346eba90730517bc1985811e7a1098 to your computer and use it in GitHub Desktop.
Install nvm for drupal-vm with post_provision_scripts
#!/bin/bash
# Keep in sync the version defined here with the one in .travis.yml
NODE_VERSION="8.17.0"
VAGRANT_USER="vagrant"
VAGRANT_HOME_PATH="/home/$VAGRANT_USER"
cd $VAGRANT_HOME_PATH || exit 1;
# Specify the NVM_DIR as the script would use the root directory otherwise.
# Also, we have to run the commands as VAGRANT_USER so the nvm installer
# sets everything up in a correct user's home dir.
NVM_PATH="$VAGRANT_HOME_PATH/.nvm"
if [[ ! -d $NVM_PATH ]]
then
export NVM_DIR=${NVM_PATH}
sudo -u $VAGRANT_USER -H bash -c "
mkdir $NVM_PATH && \
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash \
"
fi
# Append needed code to our bashrc so it's automatically sourced upon login.
if ! grep -c 'export NVM_DIR' $VAGRANT_HOME_PATH/.bashrc ; then
tee -a "$VAGRANT_HOME_PATH/.bashrc" << END
# Added from scripts/provision-node.sh
export NVM_DIR="\$HOME/.nvm"
# This loads nvm
[ -s "\$NVM_DIR/nvm.sh" ] && \. "\$NVM_DIR/nvm.sh"
# This loads nvm bash_completion
[ -s "\$NVM_DIR/bash_completion" ] && \. "\$NVM_DIR/bash_completion"
END
fi
# Sourcing the profile.
sudo -u $VAGRANT_USER -H bash -c "source $VAGRANT_HOME_PATH/.bashrc"
sudo -u $VAGRANT_USER -H bash -c "source $VAGRANT_HOME_PATH/.nvm/nvm.sh"
# Install our node version and set it as a default one.
sudo -u $VAGRANT_USER -H bash -c "
source $VAGRANT_HOME_PATH/.nvm/nvm.sh && \
nvm install ${NODE_VERSION} && \
nvm alias default ${NODE_VERSION} && \
nvm use \
"
# In case the node version is already installed, we'll get a non-0 response
# notifying about it so we have to explicitely return 0.
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment