How to Install Node.js and npm on Ubuntu Linux

If you are looking to install Node.js on Ubuntu¬†Linux, you’re in luck. Node.js is available in the Ubuntu repository and you can easily install it with a few commands.

Node.js is supported by npm¬†(Node.js Package Manager). It’s an open-source library of Node.js packages. So you can use one in your application without needing to rewrite the entire thing all on your own.

There are two main ways you can install Node.js and npm on Ubuntu:

Method 1: Install Node.js and npm on Ubuntu using the official repository

As I said earlier, Node.js is available on Ubuntu. So all you need to do is to open a terminal and use the following command:

sudo apt install nodejs

To install npm, use the following command:

sudo apt install npm

I recommend installing npm as well because you’re going to need it anyway. Both Node.js and npm are quite small.

For working on Node.js, you can use any good code editor for Linux or an IDE. It’s basically your preference.

I’m not going to show you how to get started with Node.js because that’s not the purpose of this quick tutorial.

If you want to remove Node.js and npm, you can use the command below:

sudo apt remove nodejs npm

Note: If you’re getting a “/usr/bin/env: node: No such file or directory” error while running the npm command to install a node module, you can easily fix it.

What happens here is that the program looks for the binary with path /usr/bin/node whereas the correct path in the system might be /usr/bin/nodejs. Making a soft link should fix it:

ln -s /usr/bin/nodejs /usr/bin/node

Method 2: Install Node.js and npm on Ubuntu using NodeSource repository

You can install Node.js and npm directly from the NodeSource repository. Node.js provides an easy-to-use bash script for this purpose.

What you have to keep in mind is that you need to specify which major version of Node.js you want to install.

Let’s say you want to install Node.js version 11. First, install Curl on Ubuntu:

sudo apt install curl

Now you can use the following command:

curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -

If you want to install Node.js version 10, you can replace setup_11.x with setup_10.x.

The above command will download and start running the installation script. The script will add a new repository in the source list directory (/etc/apt/sources.list.d). It will also add the GPG key of the new repository automatically.

With this new repository added to the sources list, you can install Node.js using the apt command. The added advantage is that the installed Node.js version can be updated easily like a regular package using the sudo apt update && sudo apt upgrade command.

To install the Node.js package, use the following command. Npm is also installed with this package.

sudo apt install nodejs

Once installed, you verify it by checking the installed version of Node.js with this command:

node --version

The output should be like this:

node --version

You can also verify the npm installation:

npm --version

The output should be similar to this:

npm --version

That’s all you need to do to install Node.js on Ubuntu. I hope you found this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.

After that, if you love Node.js, you should check out NodeOS, a Linux distribution for Node.js users.

Similar Posts

  • Getting below error

    The following packages have unmet dependencies:
    npm : Depends: node-gyp (>= 3.6.2~) but it is not going to be installed
    E: Unable to correct problems, you have held broken packages.