The first real hurdle I encountered on my journey wasn’t some tricky algorithm or inbuilt function. It was how to install the latest npm package manager on my Ubuntu system. The instructions on most blogs you come across on the internet are, I am afraid to say, inaccurate and downright false. It’s probably because they all copied each other and most of them never bothered to test and see if their solutions work.
What doesn’t work
First of all the easiest way to install npm is to use the package manager apt. You just open a terminal and type sudo apt install npm. You will get a working version of npm but it won’t be the latest version. It will be a hopelessly out of date version. Generally, to install the latest version of the software in Ubuntu you need to install what are known as PPAs i.e. repositories/software sources that are maintained either by the software developer e.g. Mozilla has a Firefox PPA or some other kind third party, for example, Ondřej Surý’s famous LEMP PPA.
If you Google right now you will be told to follow these steps in order to install the latest npm on Ubuntu:
- curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash –
- sudo apt install npm
This will not work and will give you errors.
So you have two (actually three) methods to install npm on your Ubuntu system depending on your needs. First of all, it depends on whether you will need/want to use Nodejs or not. If you don’t then you can just install npm the normal way using sudo apt install npm. Then run the command sudo npm install npm@latest -g in other words use npm to update itself.
If you do need to install the LTS/latest version of npm there are two methods. Follow step one above i.e. the step with curl. This where you say but you said it wouldn’t work? Well, it doesn’t work but you can make it work by saying sudo apt install nodejs instead of sudo apt install npm. It appears the node team now bundles npm with Node.js. So if you install Node.js you can use npm.
However, if you do decide to use the version of npm bundled with Node.js you cannot update it using the npm@latest command. If you do try to do that the operation will fail but not before it breaks npm. So if you try to make npm update itself it will break npm. To get it back you will have to run the command sudo apt reinstall nodejs.