In this tutorial, we’re going to explore three different ways to install Node.js and npm on Ubuntu 20.04:
- From the standard Ubuntu repositories. This is the easiest way to install Node.js and npm on Ubuntu and should be sufficient for most use cases. The version included in the Ubuntu repositories is
- From the NodeSource repository. Use this repository if you want to install a different version of Node.js than the one provided in the Ubuntu repositories. NodeSource currently supports Node.js
nvm(Node Version Manager). You can use this tool to install multiple versions of Node.js on the same computer. If you are a Node.js developer, this is the preferred method to install Node.js.
Choose the installation method that is appropriate for your environment. If you are not sure which version of Node.js to install, see the documentation for the application you plan to deploy.
Install Node.js and npm from the Ubuntu repository
At the time of writing, the version of Node.js included in the Ubuntu 20.04 repositories is
10.19.0 This is the previous version of TLS.
Installation is pretty straightforward. Run the following commands to update the package index and install Node.js and npm:
sudo apt update
sudo apt install nodejs npm
The above command installs a number of packages including the tools required to compile and install npm native addons.
When you’re done, verify the installation by doing the following:
Install Node.js and npm from NodeSource
NodeSource is a company focused on providing enterprise grade node support. It maintains an APT repository with several Node.js versions. Use this repository if your application requires a specific version of Node.js.
At the time of writing, the NodeSource repository offers the following versions:
- v14.x – The latest stable release.
- v12.x – The latest LTS version.
- v10.x – The previous LTS version.
We install Node.js version 14.x:
Run the following command as a user with sudo privileges to download and run the NodeSource installation script:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
The script will add the NodeSource signature key to your system, create an apt repository file, install any required packages, and update the apt cache.
For example, if you need a different version of Node.js
12.x, change that
Once the NodeSource repository is enabled, install Node.js and npm:
sudo apt install nodejs
The nodejs package contains both the
Make sure Node.js and npm were installed successfully by printing their versions:
To be able to compile native add-ons from npm, you have to install the development tools:
sudo apt install build-essential
Install Node.js and npm with NVM
NVM (Node Version Manager) is a bash script that allows you to manage multiple Node.js versions per user. NVM allows you to install and uninstall any version of Node.js that you want to use or test.
Visit the nvm GitHub repository
Page and copy either the
Command to download and install the
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
Do not use
how it will make it possible
nvm for the root user.
The script clones the project’s repository from Github to the
=> 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:
To get a list of all Node.js versions that can be installed with
The command prints a huge list of all available Node.js versions.
To install the latest available version of Node.js, do the following:
nvm install node
The output should look something like this:
... Checksums matched! Now using node v14.2.0 (npm v6.14.4) Creating default alias: default -> node (-> v14.2.0)
Once the installation is complete, check it out by printing the Node.js version:
Let’s install two more versions, the latest LTS version and version
nvm install --lts
nvm install 10.9.0
You can list the installed Node.js versions by typing:
The output should look something like this:
> v10.9.0 v12.16.3 v14.2.0 default -> node (-> v14.2.0) node -> stable (-> v14.2.0) (default) stable -> 14.2 (-> v14.2.0) (default) iojs -> N/A (default) unstable -> N/A (default) lts/* -> lts/erbium (-> v12.16.3) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.17.0 (-> N/A) lts/dubnium -> v10.20.1 (-> N/A) lts/erbium -> v12.16.3
The entry with an arrow on the right (
> v10.9.0) is the Node.js version used in the current shell session, and the default version is on. set
v14.2.0. Standard version is the version that is active when new shells are opened.
To change the currently active version, enter:
nvm use 12.16.3
Now using node v12.16.3 (npm v6.14.4)
Run the following command to change the default version of Node.js:
nvm alias default 12.16.3
For more detailed information on using the
nvm You can find script on the project’s GitHub page.
We showed you three different ways to install Node.js and npm on your Ubuntu 20.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 use yarn to manage your application dependencies, check out our tutorial on installing and using yarn on Ubuntu 20.04.
Feel free to leave a comment if you have any questions.