In brief: the simplest and safest way to get the latest Node.js (version 22+ and above) on Ubuntu 24.04 and in WSL is to add the official NodeSource repository or install it via the version manager nvm. This article covers both options, as well as common mistakes and useful settings.
📋 Requirements
- Ubuntu 24.04 (Native or WSL)
- sudo privileges
- Internet access
Option 1 — NodeSource (quick for production)
Step 1. Update the system
sudo apt update && sudo apt upgrade -y
Step 2. Basic dependencies
sudo apt install -y ca-certificates curl gnupg
Step 3. Add the NodeSource repository
To install Node.js 22.x:
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
Or the subsequent version (e.g., 23.x), if you need something newer than 22:
curl -fsSL https://deb.nodesource.com/setup_23.x | sudo -E bash -
Step 4. Install Node.js
sudo apt install -y nodejs
Verification
node -v
npm -v
Expected: v22.x.x and npm >=10.
Option 2 — NVM (flexible for development)
NVM allows you to install multiple versions of Node.js and quickly switch between them — convenient for developers.
Step 1. Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
Activate nvm in the current session (or simply restart the terminal):
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
Step 2. Install Node.js via nvm
Latest stable release:
nvm install node
Specific version, for example 22:
nvm install 22
nvm use 22
Set 22 as default:
nvm alias default 22
Verification
node -v
npm -v
WSL: nuances and best practices
- Install Node.js inside WSL if you plan to work in the Linux file system (
~/projects) — this will speed up npm/pnpm/yarn and avoid conflicts with Windows. - Store projects in the Linux path, such as
~/code, rather than on drives/mnt/c, to avoid performance degradation. - If you use VS Code, install the Remote – WSL extension and open folders directly from WSL.
Useful after installation
Enable Corepack (yarn/pnpm without global installation)
sudo corepack enable # via NodeSource
# or (if via nvm — without sudo)
corepack enable
corepack prepare pnpm@latest --activate
corepack prepare yarn@stable --activate
Optimal npm settings (global prefix in home directory)
This will help avoid sudo for global installations:
mkdir -p ~/.npm-global
npm config set prefix ~/.npm-global
echo 'export PATH="$HOME/.npm-global/bin:$PATH"' >> ~/.bashrc
# For Zsh:
# echo 'export PATH="$HOME/.npm-global/bin:$PATH"' >> ~/.zshrc
source ~/.bashrc # or source ~/.zshrc
Package verification and diagnostics
node -p "process.version"
node -p "process.versions"
npm doctor
Common mistakes and solutions
Insufficient permissions for global npm installation
Symptom: EACCES errors when npm i -g ....
Solution: configure npm to use a directory in $HOME (see the section on global prefix) or use corepack/local installations.
Old version of npm after installing Node.js
Update npm manually:
npm i -g npm@latest
Conflict between system Node.js and nvm
Do not mix methods: either NodeSource or nvm. If you need to switch — use nvm. To "clean" the system from the NodeSource package:
sudo apt remove -y nodejs
sudo rm -f /etc/apt/sources.list.d/nodesource.list
sudo apt update
Slow build/installation of packages in WSL
- Keep the project in the Linux path (
~/project), not on/mnt/c. - Update WSL:
wsl --update(in Powershell) and restart the distribution.
Quick checklist
- Need a stable version for the server → NodeSource.
- Need to switch between versions → nvm.
- WSL: keep code in
~/, use VS Code with Remote – WSL. - Enable corepack and/or set npm prefix in the home directory.
All set 🎉
Now you have the latest Node.js (> 22) installed on Ubuntu 24.04 and in WSL. You can confidently run modern tools (Vite, Next.js, NestJS) and package managers (npm, pnpm, yarn) without unnecessary hassle.