Setting Up NVM, Node.js, and Yarn on WSL Ubuntu

Setting Up NVM, Node.js, and Yarn on WSL Ubuntu This guide will walk you through the complete process of setting up Node Version Manager (NVM), Node.js, and Yarn package manager on Windows Subsystem for Linux (WSL) with Ubuntu. Prerequisites WSL with Ubuntu installed and properly configured Internet connection Basic terminal knowledge Part 1: Installing NVM (Node Version Manager) NVM allows you to install and manage multiple versions of Node.js. # 1. Update your package lists sudo apt update # 2. Install dependencies required for NVM sudo apt install -y curl wget build-essential # 3. Download and run the NVM installation script curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash # 4. Close and reopen your terminal, or run this to apply changes immediately: export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion # 5. Verify NVM installation nvm --version If you don't see a version number after running the verification command, make sure NVM was added to your shell configuration files correctly: # Check if NVM configuration exists in .bashrc grep -i nvm ~/.bashrc # If not found, add it manually: echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.bashrc echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> ~/.bashrc echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' >> ~/.bashrc source ~/.bashrc Part 2: Installing Node.js with NVM Now that NVM is installed, you can easily install different Node.js versions. # 1. List available Node.js versions nvm ls-remote # 2. Install the latest LTS (Long Term Support) version nvm install --lts # 3. Alternatively, install a specific version (e.g., 18.15.0) # nvm install 18.15.0 # 4. Set a version as the default nvm alias default lts/* # 5. Verify Node.js and npm installation node --version npm --version Using Different Node.js Versions With NVM, you can switch between Node.js versions easily: # List installed Node.js versions nvm ls # Switch to another installed version nvm use 16.20.0 # Replace with any installed version # Install and switch to a specific version in one command nvm install 20.10.0 && nvm use 20.10.0 Part 3: Installing Yarn Yarn is a fast and reliable alternative to npm. There are multiple ways to install Yarn: Option 1: Install Yarn via npm (Recommended for NVM users) This method ensures Yarn works with your NVM-managed Node.js installation: # Install Yarn globally via npm npm install --global yarn # Verify installation yarn --version Option 2: Install Yarn via the official repository This method installs Yarn system-wide: # 1. Import the repository's GPG key curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - # 2. Add the Yarn repository echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list # 3. Update package lists sudo apt update # 4. Install Yarn without Node.js (since we're using NVM for Node.js) sudo apt install --no-install-recommends yarn # 5. Verify installation yarn --version Option 3: Install Yarn via Corepack (for Node.js 16.10+) If you're using Node.js 16.10 or later: # Enable Corepack (included with Node.js 16.10+) corepack enable # Verify installation yarn --version Part 4: Configuring Your Environment Setting Up Global Yarn Packages Configure Yarn to store global packages in your user directory: # Create a directory for global packages mkdir -p ~/.yarn-global # Configure Yarn to use this directory yarn config set prefix ~/.yarn-global # Add to PATH in your shell configuration file echo 'export PATH="$PATH:$HOME/.yarn-global/bin"' >> ~/.bashrc source ~/.bashrc Verifying Your Complete Setup Ensure everything is working properly: # Check versions echo "Node.js: $(node --version)" echo "npm: $(npm --version)" echo "Yarn: $(yarn --version)" echo "NVM: $(nvm --version)" # Verify global package installation npm install -g cowsay cowsay "npm works!" yarn global add figlet figlet "Yarn works!" Troubleshooting NVM not found after installation If you can't use NVM after installation: Ensure the NVM script is sourced in your .bashrc or .zshrc file Try source ~/.bashrc to reload your shell configuration Check if ~/.nvm directory exists Yarn global packages not found If you've installed global packages with Yarn but can't access them: Verify your PATH includes the Yarn global bin directory: echo $PATH | grep yarn If not found, add it: echo 'export PATH="$PATH:$HOME/.yarn-global/bin"' >> ~/.bashrc source ~/.bashrc Permission errors If you encounter permission errors while installing packages globa

May 7, 2025 - 08:18
 0
Setting Up NVM, Node.js, and Yarn on WSL Ubuntu

Setting Up NVM, Node.js, and Yarn on WSL Ubuntu

This guide will walk you through the complete process of setting up Node Version Manager (NVM), Node.js, and Yarn package manager on Windows Subsystem for Linux (WSL) with Ubuntu.

Prerequisites

  • WSL with Ubuntu installed and properly configured
  • Internet connection
  • Basic terminal knowledge

Part 1: Installing NVM (Node Version Manager)

NVM allows you to install and manage multiple versions of Node.js.

# 1. Update your package lists
sudo apt update

# 2. Install dependencies required for NVM
sudo apt install -y curl wget build-essential

# 3. Download and run the NVM installation script
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

# 4. Close and reopen your terminal, or run this to apply changes immediately:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

# 5. Verify NVM installation
nvm --version

If you don't see a version number after running the verification command, make sure NVM was added to your shell configuration files correctly:

# Check if NVM configuration exists in .bashrc
grep -i nvm ~/.bashrc

# If not found, add it manually:
echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.bashrc
echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> ~/.bashrc
echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' >> ~/.bashrc
source ~/.bashrc

Part 2: Installing Node.js with NVM

Now that NVM is installed, you can easily install different Node.js versions.

# 1. List available Node.js versions
nvm ls-remote

# 2. Install the latest LTS (Long Term Support) version
nvm install --lts

# 3. Alternatively, install a specific version (e.g., 18.15.0)
# nvm install 18.15.0

# 4. Set a version as the default
nvm alias default lts/*

# 5. Verify Node.js and npm installation
node --version
npm --version

Using Different Node.js Versions

With NVM, you can switch between Node.js versions easily:

# List installed Node.js versions
nvm ls

# Switch to another installed version
nvm use 16.20.0  # Replace with any installed version

# Install and switch to a specific version in one command
nvm install 20.10.0 && nvm use 20.10.0

Part 3: Installing Yarn

Yarn is a fast and reliable alternative to npm. There are multiple ways to install Yarn:

Option 1: Install Yarn via npm (Recommended for NVM users)

This method ensures Yarn works with your NVM-managed Node.js installation:

# Install Yarn globally via npm
npm install --global yarn

# Verify installation
yarn --version

Option 2: Install Yarn via the official repository

This method installs Yarn system-wide:

# 1. Import the repository's GPG key
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -

# 2. Add the Yarn repository
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

# 3. Update package lists
sudo apt update

# 4. Install Yarn without Node.js (since we're using NVM for Node.js)
sudo apt install --no-install-recommends yarn

# 5. Verify installation
yarn --version

Option 3: Install Yarn via Corepack (for Node.js 16.10+)

If you're using Node.js 16.10 or later:

# Enable Corepack (included with Node.js 16.10+)
corepack enable

# Verify installation
yarn --version

Part 4: Configuring Your Environment

Setting Up Global Yarn Packages

Configure Yarn to store global packages in your user directory:

# Create a directory for global packages
mkdir -p ~/.yarn-global

# Configure Yarn to use this directory
yarn config set prefix ~/.yarn-global

# Add to PATH in your shell configuration file
echo 'export PATH="$PATH:$HOME/.yarn-global/bin"' >> ~/.bashrc
source ~/.bashrc

Verifying Your Complete Setup

Ensure everything is working properly:

# Check versions
echo "Node.js: $(node --version)"
echo "npm: $(npm --version)"
echo "Yarn: $(yarn --version)"
echo "NVM: $(nvm --version)"

# Verify global package installation
npm install -g cowsay
cowsay "npm works!"

yarn global add figlet
figlet "Yarn works!"

Troubleshooting

NVM not found after installation

If you can't use NVM after installation:

  1. Ensure the NVM script is sourced in your .bashrc or .zshrc file
  2. Try source ~/.bashrc to reload your shell configuration
  3. Check if ~/.nvm directory exists

Yarn global packages not found

If you've installed global packages with Yarn but can't access them:

  1. Verify your PATH includes the Yarn global bin directory:
   echo $PATH | grep yarn
  1. If not found, add it:
   echo 'export PATH="$PATH:$HOME/.yarn-global/bin"' >> ~/.bashrc
   source ~/.bashrc

Permission errors

If you encounter permission errors while installing packages globally:

# Using npm
mkdir -p ~/.npm-global
npm config set prefix ~/.npm-global
echo 'export PATH="$PATH:$HOME/.npm-global/bin"' >> ~/.bashrc
source ~/.bashrc

# Using Yarn
# Follow the "Setting Up Global Yarn Packages" section above

Best Practices

  1. Use NVM to manage Node.js versions, especially when working on multiple projects
  2. Add your NVM, npm, and Yarn configurations to your dotfiles repository if you have one
  3. Consider using project-specific .nvmrc files to automatically switch Node versions
  4. Use Yarn's lock files to ensure dependency consistency across environments
  5. Regularly update NVM with nvm upgrade to get the latest features and fixes

Additional Resources