Read in 6 minutes

last updated 

How to install Node.js on Ubuntu 18.04

This tutorial explains how to install Node.js and npm on a Ubuntu 18.04 machine. 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 on Ubuntu.

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.

Choose one of the installation options that will work best for you.

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

Install Node.js from the Ubuntu repository

Node.js package is 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.

To install Node.js and npm from the default Ubuntu repositories, follow these steps:

  1. Install Node.js

    As always before installing a package with apt the first thing that we need to do is to refresh the packages index and after that install the Node.js package by typing:

    sudo apt update
    sudo apt install nodejs

    Verify the installation by typing:

    nodejs --version
    v8.10.0

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

  2. Install npm

    To be able to download npm packages, we also need to install npm, the Node.js package manager. We can do that by typing:

    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. Enabling NodeSource repository

    To enable the NodeSource repository type the following command:

    curl -sL https://deb.nodesource.com/setup_8.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 8.x, Carbon. If you need to install version 10.x just change setup_8.x with setup_10.x

  2. Installing Node.js and npm

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

    sudo apt install nodejs
  3. Verifying Node.js and npm installation

    The simplest way to verify that Node.js and npm installation were successful is by printing their versions:

    node --version
    v8.11.3
    npm --version
    5.6.0
Advertisement

Install Node.js 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

    To download or update the NVM install script type:

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

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

    download-nvm-script.

    => 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, we need to 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 using NVM

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

    nvm install node
    

    Once the installation is completed we can verify in ny printing the Node.js version:

    node --version
    v10.6.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 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 v10.6.0. Default version is the version that will be active when opening new shells.

We change the current active version with:

nvm use 8.11.3

and verify it by typing:

nvm current
v8.11.3

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

nvm alias default 8.11.3

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

Install Yarn package manager

Yarn is a JavaScript package manager compatible with npm. It was created to solve a set of problems with the npm such as speeding up the packages installation process by parallelizing operations and reducing errors related to network connectivity.

The official Yarn repository provides the most up-to-date version and is the recommended way of installing Yarn on Ubuntu.

To install Yarn through apt on your Ubuntu system, follow these steps:

  1. Add Yarn yum repository

    To add the the GPG key and the repository to your system run the following commands:

    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
  2. Install Yarn on Ubuntu

    Once the repository is added, update the package index and install Yarn, with:

    sudo apt update
    sudo apt install yarn

    If you are using nvm to skip the Node.js installation use:

    sudo apt update
    sudo apt install --no-install-recommends yarn
  3. Verify the installation

    Verify the installation by printing the Yarn version number:

    yarn --version
    1.7.0

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 have any questions or feedback, feel free to comment below.