Node Version Manager (NVM) Setup
Install Prerequisites
Essential packages for downloading and compiling software:
sudo apt update
sudo apt install curl build-essential git
Download and Install NVM
Install NVM using the installation script:
# Download and run installation script
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
# Alternative using wget
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
Configure Shell Environment
Add these lines to your shell profile (~/.bashrc, ~/.zshrc, etc.):
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_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
Verify Installation
Load NVM in current shell and verify:
# Reload shell configuration
source ~/.bashrc # or source ~/.zshrc for Zsh
# Verify NVM installation
nvm --version
Basic NVM Usage
Common commands for managing Node.js versions:
# List available Node.js versions
nvm ls-remote
# Install specific Node.js version
nvm install 18.19.0 # Install specific version
nvm install --lts # Install latest LTS version
nvm install node # Install latest version
# Switch Node.js versions
nvm use 18.19.0 # Use specific version
nvm use --lts # Use latest LTS version
# List installed versions
nvm ls
# Set default Node.js version
nvm alias default 18.19.0
Project-Specific Configuration
Create .nvmrc
file in project root:
# Create .nvmrc with Node.js version
echo "18.19.0" > .nvmrc
# Use project's Node.js version
nvm use
Best Practices
- Default Version Setup:
# Install and use LTS version by default
nvm install --lts
nvm alias default 'lts/*'
- Multiple Version Management:
# Install multiple versions
nvm install 16
nvm install 18
nvm install 20
# Switch between versions
nvm use 16 # For older projects
nvm use 18 # For current LTS
nvm use 20 # For latest features
Troubleshooting
Common issues and solutions:
# Command not found
source ~/.bashrc
# Reinstall NVM
rm -rf ~/.nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
# Clear NVM cache
nvm cache clear