Read in 6 minutes

last updated 

How to install Node.js and npm on Ubuntu 18.04

Node.js is an open source cross-platform JavaScript run-time environment that allows server-side execution of JavaScript code which simply means that you can run JavaScript code on your machine as a standalone application, free of any web browser.

Node.js is mainly used to build back-end server-side applications, but it is also very popular as a full-stack and front-end solution. npm is the default package manager for Node.js and the world’s largest software registry.

In this tutorial we will show you three different ways to install Node.js and npm on Ubuntu 18.04.

If you need Node.js only for deploying Node.js applications then the simplest option is to install the Node.js packages using apt from the Default Ubuntu repository or from the NodeSource repository in case you need the latest Node.js and npm versions. If you are using Node.js for development purposes then your best option is to install Node.js using the NVM script.

Although this tutorial is written for Ubuntu the same instructions apply for any Ubuntu based distribution, including Linux Mint and Elementary OS.

Prerequisites

Before starting with the tutorial, make sure you are logged in as a user with sudo privileges.

Install Node.js and npm from the Ubuntu repository

Node.js and npm packages are available from the Ubuntu 18.04 distribution repository. At the time of writing, the version in the repositories is v8.10.0 which may not always be the latest version.

Start by updating the packages list by typing:

sudo apt update

Install nodejs using the apt package manager:

sudo apt install nodejs npm

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

To verify the installation execute the following command:

nodejs --version
v8.10.0

To be able to download npm packages, you also need to install npm, the Node.js package manager. To do do type:

sudo apt install npm

Verify the installation by typing:

npm --version
3.5.2

Install Node.js from the NodeSource repository

NodeSource is a company focused on providing enterprise-grade Node support and they maintain a repository containing the latest versions of Node.js.

To install Node.js and npm from the the NodeSource repository, follow these steps:

  1. Enable the NodeSource repository with the following curl command:

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

    The command above will add the NodeSource signing key to your system, create an apt sources repository file install all necessary packages and refresh the apt cache.

    The current LTS version of Node.js is version 10.x, Carbon. If you need to install version 8.x just change setup_10.x with setup_8.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 binaries.

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

    node --version
    v10.13.0
    npm --version
    6.4.1

Install Node.js and npm using NVM

NVM (Node Version Manager) is a bash script used to manage multiple active Node.js versions.

With NVM we can install and uninstall any specific Node.js version that we want to use or test.

To install Node.js and npm using NVM on your Ubuntu system, follow these steps:

1. Downloading NVM (Node Version Manager) script

Start by downloading the NVM install script:

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

The command above will clone the nvm repository from Github to the ~/.nvm directory and add the nvm path to your Bash or ZSH profile.

=> 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 the terminal or run the commands to add the path to nvm script to our current session. You can do whatever is easier for you.

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

nvm --version
0.33.11

2. Installing Node.js and npm

Now that we have nvm installed we 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 v11.0.0...
Downloading https://nodejs.org/dist/v11.0.0/node-v11.0.0-linux-x64.tar.xz...
################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v11.0.0 (npm v6.4.1)
Creating default alias: default -> node (-> v11.0.0)

Once the installation is completed verify in by printing the Node.js version:

node --version
v11.0.0

Let’t install two more versions, the latest LTS version and version 4.9.1

nvm install --lts
nvm install 4.9.1

Once LTS and 4.9.1 versions are installed we can list installed Node.js instances by typing:

nvm ls

The output should look something like this:

->       v4.9.1
       v10.13.0
        v11.0.0
         system
default -> node (-> v11.0.0)
node -> stable (-> v11.0.0) (default)
stable -> 11.0 (-> v11.0.0) (default)
iojs -> N/A (default)
lts/* -> lts/dubnium (-> v10.13.0)
lts/argon -> v4.9.1
lts/boron -> v6.14.4 (-> N/A)
lts/carbon -> v8.12.0 (-> N/A)
lts/dubnium -> v10.13.0

The output tells us that the entry with an arrow on the right (-> v4.9.1), is the version used in the current shell session and the default version is set to v11.0.0. Default version is the version that will be active when opening new shells.

We change the current active version with:

nvm use 10.13.0
Now using node v10.13.0 (npm v6.4.1)

In case you want to set version 10.13.0 as a default Node.js version you can do that with the following command:

nvm alias default 10.13.0
nvm alias default 10.13.0

Install development tools

To be able to compile and install native addons from npm you need to install the development tools.

The following command will install all the necessary packages:

sudo apt install gcc g++ make

Uninstall Node.js

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

sudo apt remove nodejs npm

Conclusion

We have shown you three different ways to install Node.js and npm on your Ubuntu 18.04 server. The method you choose depends on your requirements and preferences. Even though installing the packaged version from the Ubuntu or NodeSource repository is easier, the nvm method gives you more flexibility for adding and removing different Node.js versions on a per user basis.

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

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