Skip to content

Instantly share code, notes, and snippets.

Last active April 10, 2024 13:53
Show Gist options
  • Save ankurk91/8f107ef490f40f74a1cf to your computer and use it in GitHub Desktop.
Save ankurk91/8f107ef490f40f74a1cf to your computer and use it in GitHub Desktop.
Install node-js, npm and yarn on Ubuntu/Mac using nvm
# Install node and npm via nvm -
# Run this script like - bash
# Define versions
# You can pass argument to this script like --version 8
if [ "$1" = '--version' ]; then
echo "==> Using specified node version - $2"
echo "==> Make sure bash profile exists and writable"
touch ~/.zshrc
touch ~/.bashrc
echo "==> Installing node version manager version $INSTALL_NVM_VER"
# Removed if already installed
rm -rf ~/.nvm
# Unset exported variable
export NVM_DIR=
# Install nvm
curl -o-$INSTALL_NVM_VER/ | bash
# Make nvm command available to terminal
source ~/.nvm/
echo "==> Installing node js version $INSTALL_NODE_VER"
nvm install $INSTALL_NODE_VER
echo "==> Make this version system default"
nvm alias default $INSTALL_NODE_VER
nvm use default
#echo -e "==> Update npm to latest version, if this stuck then terminate (CTRL+C) the execution"
#npm install -g npm
echo "==> Installing Yarn package manager"
rm -rf ~/.yarn
curl -o- -L | bash
# Yarn configurations
export PATH="$HOME/.yarn/bin:$PATH"
yarn config set prefix ~/.yarn -g
echo "==> Installing pnpm package manager"
npm install -g pnpm
# npm configs
npm config set update-notifier false
echo "==> Checking for versions"
nvm --version
node --version
npm --version
yarn --version
pnpm --version
echo "==> Print binary paths"
which npm
which node
which yarn
which pnpm
echo "==> List installed node versions"
nvm ls
nvm cache clear
echo "==> Please Logout and Login back to take changes effect"
echo ""
# Tested on Ubuntu, MacOS, WSL

How to update nvm itself ?

cd ~/.nvm

Wants to try something else ?

💡 Quick install - Run this script in terminal

curl -o- | bash

# specify your node version, for example 
curl -o- | bash -s -- --version 14 
Copy link

ankurk91 commented Jul 6, 2019

Reserved commit

Copy link

rishabhrpg commented Sep 22, 2021

curl -o- | bash

Just tested this works on WSL 2 Ubuntu Image.

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