Use n module from npm in order to upgrade node
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
To upgrade to latest version (and not current stable) version, you can use
sudo n latest
Fix PATH:
sudo apt-get install --reinstall nodejs-legacy # fix /usr/bin/nodeTo undo:
sudo n rm 6.0.0 # replace number with version of Node that was installed sudo npm uninstall -g n
You may need to restart your terminal to see the updated node version.
Found in David Walsh blog
Answer from eballeste on askubuntu.comUse n module from npm in order to upgrade node
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
To upgrade to latest version (and not current stable) version, you can use
sudo n latest
Fix PATH:
sudo apt-get install --reinstall nodejs-legacy # fix /usr/bin/nodeTo undo:
sudo n rm 6.0.0 # replace number with version of Node that was installed sudo npm uninstall -g n
You may need to restart your terminal to see the updated node version.
Found in David Walsh blog
NodeSource provides binary distributions of Node.js; complete installation instructions can be found here. The instructions have been copied below for your reference. Instructions are the same for updating to the latest version.
Run once:
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
Run whenever you want to change the major version of Node.js:
NODE_MAJOR=20
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
sudo apt-get update
sudo apt-get install nodejs -y
Prior to August 2023: Previously versions of this answer involved the use of a series of setup_XX.x scripts that you'd run to install/update Node.js:
curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
sudo apt-get install -y nodejs
But as @eis pointed out, these scripts are no longer supported. To see the previous answers, please look at the edit history for this answer.
How to update node on my WSL Ubuntu?
Why is it so hard to update Node on Linux? And why I still see old version of node after using "n" to update
Fresh install...can't seem to update nodejs/ npm on ubuntu?
[deleted by user]
Videos
The current LTS version can be installed via
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
In difference to the already provided answers, this evergreen link will always install the current long term supported version. So you don't need to tweak your script every couple month.
This can help:
$ sudo npm install -g n
$ sudo npm cache clean -f
$ sudo n stable
also you can replase stable with latest.
NOTE: if $ node -v shows the old version, open a new shell.
I have downloaded node on my win and it shows 20.11.1 while using node -v command. but when I use the same command on wsl ubuntu it shows v10.19.0. how to update it on wsl?
installed node using sudo apt install node
Using Ubuntu 22.04, I first installed node and npm using apt commands:
sudo apt install node sudo apt install npm
But from some reason it did not install the LTS version of node, instead it installed v12.22.9.
Then I tried to look for ways to update to the latest version, but no, not a simple way to do that:
Either use nvm, or n, or npx or from source?
So I used n as seen here: https://www.freecodecamp.org/news/how-to-update-node-and-npm-to-the-latest-version/
I ran these commands:
sudo npm install -g n sudo n lts sudo n prune
It did show me:
copying : node/18.12.1
installed : v18.12.1 (with npm 8.19.2)
but when I type node -v, I still get v12.22.9, so it did not uninstall the older version and did not use the newer version in the path
How can I safely undo everything and only leave my machine with the LTS version of node and npm?