How to install Node.js on Linux/Ubuntu

Node.js is an an open source JavaScript development platform for general purpose programming that enables users to build applications that need a persistent connection from the client (browser) to the server. Node.js is useful for real time applications such as web push notifications, chat, news feeds…
Node.js makes development more integrated and consistent as it uses an event driven, non blocking I/O model making it efficient and lightweight.
Node.js package manager called npm is by far the biggest open source libraries ecosystem in the world.

In this tutorial, you will learn how to install Node.js (using Ubuntu Official repository, PPA and nvm) on Ubuntu 18.04.

Read: Best Nodejs Frameworks for Web Apps in 2023

Installing Node.js and npm using Ubuntu official repository

Ubuntu 18.04 has a stable version of Node.js in its official repositories. Although this will be an older version, it should be sufficient and stable enough for general purposes.

You can rely on the apt package manager to install this version. First you refresh your package list index by running the command:

sudo apt update

Next, install Node.js by issuing the command below:

sudo apt install nodejs

Install nodejs on Linux

Read: How to install Boost on Ubuntu 22.04

Now you can check the Node.js installed version :

nodejs –version

Install nodejs on Ubuntu

Read: The Best Spreadsheet Software for Linux

Oftentimes, you’ll also need to install npm, the Node.js package ecosystem. This can be done by executing the command :

sudo apt install npm                           [install npm Ubuntu]

Install npm on Ubuntu

This will give you the possibility to install various packages and modules that can be used with Node.js .

Read: How to Install TensorFlow on Ubuntu 22.04

Installing Node.js using a PPA :NodeSource repository

Adding the PPA directly from NodeSource, will enable you to get a more recent version of Node.js . As mentioned in the previous section, NodeSource maintains up-to-date versions of Node.js unlike that found in the official Ubuntu repositories.

You will first need to install the PPA from NodeSource. Type in the following command to install version 11 :

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

To install Node.Js version 11, simply replace setup_13.x in the command above with setup_11.x as shown below in the snapshot :

The curl command will retrieve the appropriate installation script for your desired version.

Run the downloaded script using the command below:

sudo bash nodesource_setup.sh

This will add a new repository in /etc/apt/source.list.d , .i.e the source list directory and will update your local package cache.

You can now install the Node.js package once the setup script above has been run. Type in the command below :

sudo apt install nodejs

Install nodejs ubuntu

Now you can verify Node.js installation by typing in :

node –version

The Nodejs package contains already the Nodejs binary along with npm. Contrary to the previous installation from Ubuntu repositories, npm does not to be installed separately.

To verify npm version number, issue the command below:

npm –version

Uninstalling Node.js

We have created an article on how to uninstall Nodejs on Ubuntu 22.04. You can access it here.

Installing Node.js using nvm(Node Version Manager)

NVM is a bash script for managing multiple active Node.js versions. NVM will allow you to install and uninstall any given Node.js version. NVM provides you access to the latest versions of Node.js . It also helps keep and manage previous releases.

The following steps will help you install Node.js as well as npm on your Ubuntu system using NVM .

Installing NVM script

The curl command will help download the nvm installation Bash script from the nvm’s GitHub page. Note that the version number may differ from what is highlighted here:

curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh -o install_nvm.sh

Once the script has been transferred to your local machine, run it with bash:

bash install_nvm.sh

This will install the application into a sub-directory of your home folder at ~/.nvm.

To start using nvm, either quit the current session and open your terminal again or source the file ~/.profile in order for your current session to know about the changes:

source ~/.profile

You should now be able to verify that nvm was installed properly by typing:

nvm –version

Installing Node.js and npm

After nvm has been installed in your machine, you can now proceed to the installation of the latest version of Node.js. To do this , run the command below :

nvm install node

Once this is done, display the node.js version using the command:

node –version

Notice that the executable is called node since you used nvm .

In most cases, nvm will use the recently installed version. If however you have a Node.js version in mind (10.0.0 say) and want to install it and use it , run the command below :

nvm use 10.0.0

In case you have several Node.js versions, the following command will show what versions are installed:

nvm ls

To default one a given version, run:

nvm alias default 10.0.0

This version, i.e. 10.0.0 will be selected automatically when a new session starts. This can also be referred to like this:

nvm use default

The npm utility will manage each Node.js version so that it keeps track of its own packages.

Conclusion

You have seen three different methods that will help you install Node.js ion your Ubuntu 18.04. Although the two first installations, i.e. using Ubuntu official repository and Nodesource are the easiest, the nvm option will provide you more freedom and flexibility when it comes to dealing with multiple Node.js versions.

To work with Node.js, any reliable Linux code editor can be used. You can find out in this article a list of the best code editors for Linux.


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

Nikolaus Oosterhof

Nikolaus holds a degree in software development and has a strong passion for all things tech-related, especially gadgets with screens. Though he is nostalgic for older phone models, he's a retired gamer and continues to enjoy programming in open-source environments. Additionally, Nikolaus enjoys writing about Linux, macOS and Windows and has experience designing web pages.

Leave a Reply