website logo

Last Updated:

How to Install Nodejs in Linux

NodeJs is a javascript runtime built using Chrome’s v8 engine. NodeJs is used to run JavaScript on the server.

In this blog, you will learn how to install Nodejs in Linux. I will give you 4 ways to install Nodejs, out of which, you can pick whichever seems simple to you.

Install Nodejs in Linux using Package Manager

Installing Nodejs using a Linux Package manager is maybe the easiest and most convenient method. But sometimes you get the very old version of Nodejs from your package manager which is not suited for development purposes.

To install NodeJs using a Linux package manager, run the following command.

sudo apt update
sudo apt install nodejs npm

To check the version of the Nodejs installed on your computer, use the --version flag after the node command.

node --version

npm --version

Install Nodejs in Linux using Prebuilt Binaries

If you want to install the latest version of NodeJs on your computer, and also keep your development environment stable, installing NodeJs from prebuilt binaries may be the best bet. This installation doesn’t update automatically. If you want to update the Nodejs binaries, you have to repeat all the following steps.

First download the prebuilt binaries from official Nodejs Website. Then extract the binary using the following command.

tar -xvJf node-v18.17.1-linux-x64.tar.xz

The above command extracts the compressed file into the node-v18.17.1-linux-x64 directory. Rename this directory into node-v18.17.1 for your convenience.

mv node-v18.17.1-linux-x64 node-v18.17.1

Now move the extracted file into the usr/local/bin directory.

sudo mv sudo mv node-v18.17.1 /usr/local/lib/

Add the binary path in your .profile file. Open the ~/.profile file using the nano ~/.profile command and paste the following content at the bottom of the file.

PATH="/usr/local/lib/node-v18.17.1/bin:$PATH"

Now source the .profile file and start using Nodejs on your computer

source .profile

node --version

Install Nodejs in Linux using Nodesource PPA

PPA or personal package archive is a way to deliver the latest software packages to your Ubuntu distribution. NodeSource provides various Nodejs versions. After installation, Nodejs updates automatically when a new version of the software is released.

To install Nodejs from Nodesource, first run the following command to configure the installation. Replace the 18.x with whatever Nodejs version you want.

curl -sL https://deb.nodesource.com/setup_18.x -o /tmp/nodesource_setup.sh

sudo bash /tmp/nodesource_setup.sh

Then install Nodejs using your package manager.

sudo apt install node

Install Nodejs in Linux using Node Version Manager

Node version manager or NVM enables you to install multiple versions of Nodejs on your device and lets you switch to a different version of Nodejs with a simple command. If you are collaborating with different people or need to maintain some code that depends on some old Nodejs versions, NVM may be your preferred tool to install Nodejs.

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

Now source your .bashrc file.

source ~/.bashrc

Now list all the available Nodejs versions using the nvm list-remote command and install your preferred version using the nvm install <version number> command.

nvm install v18.17.1

You can also list all the local versions you have installed in your system using NVM.

nvm list

If you want to switch to another version of Nodejs (for example 20.6.1), you can use the nvm use command.

nvm use v20.6.1

Conclusion

Out of these 4 methods I have explained in this blog, which one you like the most? I like the 2nd one. If you learn something new, share this blog on your social media. If you have any questions, drop a mail.

See Also