Introduction
There are several ways to install npm. We will show different ways to install npm, so the reader can choose whatever they like.
We sorted the alternatives by the best option from our point of view.
Alternative 1: Use Node Version Manager
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
After the installation is complete, execute the following command to activate nvm:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
TIP: The two lines above need to be added to .bashrc or .zshrc if you want to use nvm next time.
NVM allows you to install any npm version, check the available version with:
nvm ls-remote
To install v7.3.0 and use it:
nvm install 7.3.0
nvm use 7.3.0
TIP: list installed versionuse nvm ls.
Remeber that each version of Nodejs has its own packages installed and switching between version could make some packages to be missing.
Alternative 2: Official nodejs binaries
The npm official site says
The best way to install npm is to install node using the node.js installer. npm is installed as part of node.
Go to the node.js installer webpage to download the desired version of node.
Node.js file verification
Before using the .tar file we will check that authetincity of the file with sha2sum. Check the latest url of node, here we downloaded the v7.3.0 version:
export NODE_VERSION="v7.3.0"
wget https://nodejs.org/dist/$NODE_VERSION/SHASUMS256.txt
wget https://nodejs.org/dist/$NODE_VERSION/node-$NODE_VERSION-linux-x64.tar.xz
grep node-$NODE_VERSION-linux-x64.tar.xz SHASUMS256.txt | sha256sum -c -
The output should be:
node-v7.3.0-linux-x64.tar.xz: OK
Now to install the node js binaries:
sudo cp -rp node-$NODE_VERSION-linux-x64 /usr/local/
sudo ln -s /usr/local/node-$NODE_VERSION-linux-x64 /usr/local/node
Make sure you are using the latest npm with:
sudo npm install -g npm
Alternative 3: Using a PPA
This could be dangerous is you don't trust the PPA. The good thing is that you will install npm with apt-get and have a newer version than the one form Ubuntu repositories.
curl -sL https://deb.nodesource.com/setup_6.x -o nodesource_setup.sh
chmox +x nodesource_setup.sh
sudo apt-get install
sudo apt-get install nodejs
sudo apt-get install npm
Alternative 4: Using npm from ubuntu repositories
With ubuntu you can simply use apt-get to install npm. The only issue is that usally the npm versionf rom ubuntu repositories is old.
sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm
Conclusion
We found that the best installation for development usage of npm is with nvm, since it allow to manage multiple versions of it. For production nvm could fit, but also the official node.js binaries could be a good idea. If the version shipped with ubuntu is enough it could be a good idea, probably security updates could take more time. We think that installing the linux distribution is usually a bad decision, for a development environment nvm seems to be the best option and for production using the official node.js binaries could be a good ideas when you need to use a recently version of node.js.