Read in 5 minutes
How to install Node.js on Ubuntu 18.04
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.
Before starting with the tutorial, make sure you are logged in as a user with sudo privileges.
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:
Refresh the packages index and install the Node.js by typing:
sudo apt update sudo apt install nodejs
To verify the installation execute the following command:
The Node.js executable from the Ubuntu repositories is named
nodebecause of a conflict with another package.
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:
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:
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
Installing Node.js and npm
Once the NodeSource repository is enabled install Node.js and npm by typing:
sudo apt install nodejs
Verifying Node.js and npm installation
The simplest way to verify that Node.js and npm installation were successful is by printing their versions:
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:
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
nvmrepository from Github to the
~/.nvmdirectory 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, 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:
Installing Node.js using NVM
Now that we have
nvminstalled we can install the latest available version of Node.js, by typing:
nvm install node
Once the installation is completed we can verify in by printing the Node.js version:
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:
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:
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
If for some reasons you want to uninstall Node.js and npm packages, you can use the following command:
sudo apt remove nodejs npm
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.