TLDR: This problem is caused by Bash caching the path of the npm
command, and can be solved by hash -d npm
. You don't even need to deal with apt purge
unless you want to.
Explanation
Here were my steps for getting a new npm
version on Ubuntu. First, do the installation as OP describes:
$ sudo apt-get install npm
(...apt installation of npm was successful...)
$ npm -v
3.5.2
$ command -v npm
/usr/bin/npm
$ sudo npm install -g npm
(...npm installation of npm was successful...so far, so good)
You can see that the new version is already working fine in /usr/local/bin/npm
, but unfortunately the Bash cache still has /usr/bin/npm
:
$ /usr/local/bin/npm -v
6.4.1
$ npm -v
3.5.2
$ command -v npm
/usr/bin/npm
$ type npm
npm is hashed (/usr/bin/npm)
To fix the problem, clear it from the Bash cache (do this in all open shells):
$ hash -d npm
Now the new version works as desired:
$ npm -v
6.4.1
$ command -v npm
/usr/local/bin/npm
Node.js package is available in two releases: LTS release and the current release. You can install any version you want. I am installing LTS version for this answer.
- Add Node.js PPA
You will need curl
to download the script, so first, install curl
by typing sudo apt install curl
in the terminal
.
Once, curl
is installed successfully, run the command to download and install the installer.
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
- Install
nodejs
Now, run the below command install node on and Ubuntu using apt
. This commmand will also install npm
with nodejs
. It will also install dependencies.
sudo apt install nodejs
Note: To check the version of node
run the command
node -v
To check the version of npm
run the command
npm -v
Best Answer
Fresh installation
Use the NodeSource PPA. For details look at the installation instructions. First, choose the Node.js version you need and add the sources for it:
Then install the Node.js package.
P.S.:
curl
package must be installed on server for these code lines.Upgrading
If you have
nodejs
already installed and want to update, then first remove current instalation and install it again using scripts above.