@cagatay The most reliable way to upgrade Node.js on Ubuntu depends on how you originally installed it.
Method 1: Using NVM (Recommended)
If you already use Node Version Manager (NVM), upgrading is simple. NVM allows you to keep both versions and switch between them if needed.
Install Node 22:
nvm install 22
Switch to Node 22:
nvm use 22
Set it as your default:
nvm alias default 22
Verify the change:
node -v
Method 2: Using NodeSource (PPA)
If you installed Node.js via apt using the NodeSource repository, you need to update the repository script to point to the new version.
Remove the old NodeSource list (optional but cleaner):
sudo rm /etc/apt/sources.list.d/nodesource.list
Download and run the NodeSource setup script for Node 22:
curl -fsSL [https://deb.nodesource.com/setup_22.x](https://deb.nodesource.com/setup_22.x) | sudo -E bash -
Install/Upgrade Node.js:
sudo apt-get install -y nodejs
Verify the installation:
node -v
Method 3: Using the ‘n’ Package
If you have npm installed, you can use the n interactive manager.
Clear the npm cache:
sudo npm cache clean -f
Install the ‘n’ helper:
sudo npm install -g n
Install Node 22:
sudo n 22
Update your shell:
hash -r
Troubleshooting
Permission Denied: If you see permission errors using Method 2 or 3, ensure you are using sudo.
Path Issues: If node -v still shows version 20 after upgrading via NVM, restart your terminal or run source ~/.bashrc.
Conflicts: Avoid mixing these methods. If you switch from apt to nvm, it is best to sudo apt remove nodejs first to avoid path conflicts.