How to install the latest npm on Ubuntu 20.04

Garikai Dzoma Avatar

Not so long ago the only thing I needed to learn in order to create plugins for my blogs was PHP. I could farm out the JavaScript and CSS parts of development to freelancers. With the recent versions of WordPress however, more and more code is in JavaScript so I decided to learn JavaScript deeply.

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.

What works?

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.

As hinted above there is a third method that uses Node Version Manager (nvm). I did not try this method and frankly, I think it’s too complicated for beginners in the Node world like me. All I want to do is learn how to code using JavaScript and this method wants to force me to learn about the quirks of each version of Node? No thank you.

,

2 comments

What’s your take?

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  1. Shirumatsu

    Tech Zim shld have an app by now ….you can just use ionic(Js, html,css). Very easy to build.

  2. Noah

2023 © Techzim All rights reserved. Hosted By Cloud Unboxed