How to install Node.js and npm on Ubuntu 18.04

s2 0

Node.js is an open source, cross-platform JavaScript runtime environment that enables the server-side execution of JavaScript code. This means that you can run JavaScript code on your computer as a standalone application without a web browser. Node.js is mainly used for building server-side back-end applications, but is also very popular as a full-stack and front-end solution.

Npm is the standard package manager for Node.js and the world’s largest software registry.

In this tutorial we will show you different ways to install Node.js and npm on Ubuntu 18.04. The same instructions apply to any Ubuntu-based distribution, including Kubuntu, Linux Mint, and Elementary OS.

If you only need Node.js as a local runtime to deploy Node.js applications, the easiest option is to install Node.js from the NodeSource repository. Developers should rather use the NVM script to install Node.js.

Choose the installation option that is appropriate for your environment. It is best to refer to the documentation for the Node.js application you are using to find out which Node.js versions are supported.

Install Node.js and npm from NodeSource

NodeSource is a company focused on providing enterprise grade node support and maintains a repository of the latest versions of Node.js.

Use this repository when you need to install a specific version of Node.js. At the time of writing, the NodeSource repository offers the following versions – v14.x, v13.x, v12.x, and v10.x. We install the current LTS version of Node.js, version 12.

To install Node.js and npm from the NodeSource repository, do the following:

  1. Enable the NodeSource repository by doing the following curl
    Command as a user with sudo permissions:

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

    The command will add the NodeSource signature key to your system, create an apt source repository file, install all required packages, and update the apt cache.

    If you need to install a different version, for example 14.x, just change setup_12.x With setup_14.x

  2. Once the NodeSource repository is enabled, install Node.js and npm by typing:

    sudo apt install nodejs

    The nodejs package contains both the node and npm Binary files.

  3. Verify that Node.js and npm were installed successfully by printing their versions:

    node --version
    v12.16.3
    npm --version
    6.14.4

Install Node.js and npm with NVM

NVM (Node Version Manager) is a bash script for managing multiple active Node.js versions. NVM allows you to install and uninstall any specific version of Node.js that you want to use or test.

Follow the steps below to install Node.js and npm using NVM on your Ubuntu system:

1.Install the NVM script (Node Version Manager)

To download and install the nvm Script execution:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

The above command clones Github’s NVM repository to the ~/.nvm Directory:

=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

As the output above says, you should either close and reopen Terminal or run the commands to add the path nvm Script for the current shell session. You can do what you find easier.

Once the script is in your PATH, Check that nvm was properly installed by typing:

nvm --version
0.34.0

2. Install Node.js and npm

Now that the nvm installed, you can install the latest available version of Node.js by typing:

nvm install node

The output should look something like this:

Downloading and installing node v12.8.1...
Downloading https://nodejs.org/dist/v12.8.1/node-v12.8.1-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.8.1 (npm v6.10.2)
Creating default alias: default -> node (-> v12.8.1)

Once the installation is complete, check it out by printing the Node.js version:

node --version
v12.8.1

Let’s install two more versions, the latest LTS version and version 8.10.0

nvm install --ltsnvm install 8.10.0

Enter the following to list the installed Node.js versions:

nvm ls

The output should look something like this:

->      v8.10.0
       v10.16.3
        v12.8.1
default -> node (-> v12.8.1)
node -> stable (-> v12.8.1) (default)
stable -> 12.8 (-> v12.8.1) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/dubnium (-> v10.16.3)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.1 (-> N/A)
lts/dubnium -> v10.16.3

The entry with an arrow on the right (-> v8.10.0) is the Node.js version used in the current shell session and the standard version is set to v12.8.1. Standard version is the version that is active when new shells are opened.

You can change the currently active version with:

nvm use 10.16.3
Now using node v10.16.3 (npm v6.9.0)

If you want to change the default version of Node.js, use the following command:

nvm alias default 10.16.3

Install Node.js and npm from the Ubuntu repository

Node.js and npm packages are available in the standard Ubuntu 18.04 repositories.

At the time of writing, the version included in the Ubuntu repositories is v8.10.0 This is the previous version of TLS.

To install nodejs and npm run the following commands:

sudo apt updatesudo apt install nodejs npm

The executable Node.js file from the Ubuntu repositories is called nodejs instead of node because of a conflict with another package.

Verify the installation by doing the following:

nodejs --version
v8.10.0

Install development tools

In order to compile and install native add-ons from npm, you need to install the development tools.

The following command installs all required packages including the GCC compiler:

sudo apt install build-essential

Uninstall Node.js.

If for some reason you want to uninstall Node.js and npm packages, you can use the following command:

sudo apt remove nodejs npm

Conclusion

We showed you three different ways to install Node.js and npm on your Ubuntu 18.04 server. Which method you choose depends on your needs and preferences. While installing the packaged version from the Ubuntu or NodeSource repository is easier, the nvm method gives you more flexibility in adding and removing different Node.js versions on a per-user basis.

If you want to manage your npm packages with yarn, this tutorial can teach you how to install and use yarn on Ubuntu 18.04.

If you have any questions or feedback, please feel free to comment below.