DEV Community

Cover image for 3 easy ways to install nodejs in linux
Aashish Panthi
Aashish Panthi

Posted on

3 easy ways to install nodejs in linux

Installing node in linux is quite challenging sometimes, especially if you are a beginner and don't have any idea on how to do that.

In this article, I will show you three different ways of installing Nodejs in linux. You can feel comfortable following the same process in any ubuntu-baseds distro you use.

1. Installation using apt package manager

Installation of Nodejs is insanely easy with apt package manager in linux. To install nodejs with apt package manager, just open up your terminal. Copy this command sudo apt update && sudo apt upgrade and paste here, hit enter, and type your password. Then we use the command sudo apt install nodejs, press enter, wait few seconds and you find your nodejs is successfully installed. To check the version just type the command node —version and you get the version.

2. Installation with Linux Binaries

To install linux binaries, just open up your browser and head over to nodejs.org. Click on downloads tab then choose linux binaries. After the download is completed, go to the directory where you've downloaded your nodejs. Then extract the tar file by right-clicking on it and click on extract here.

You'll have all the necessary folders here. Now, right-click and open the terminal here. Here we have to copy some folders from this directory(downloads) to our user(/user) directory. Copy this command sudo cp -r ./{lib,share,include,bin} /usr, paste it here, and hit enter. Then type your password and your nodejs is installed. If you verify if it’s actually installed or not. You can use the same command node --version and you get the nodejs version and npm --version returns the npm version.

3. Installation using NVM

If you’re wondering what this NVM is then it’s a node version manager which helps us to easily install and switch between different versions of node.
Actually installing NVM is also simple. We can directly install NVM using curl but in some cases, you may run into a problem. So, to avoid such problems just create some files with this command touch ~/.bash_profile ~/.bashrc ~/.zshrc ~/.profile. Again run the following command curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash or this one wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash. It's your choice but either of them downloads a script and runs it. Press enter and if it looks like you don't have curl installed. You can simply install the curl with the command sudo apt install curl and rerun that command again. To check if nvm has been installed or not, you need to restart the terminal so close the active one, and open a new one. Check the version with command nvm --version and if it returns the version then you have your NVM ready to use. And there are a lot of commands waiting for you to use them. One of them is nvm ls which lists all of the installed nodejs versions on your system. Then we have nvm ls-remote which basically lists all of the nodejs versions available till now. There will bw few LTS versions also in there but if we only want to install the latest LTS i.e 14.x (at the time of writing this article). Again to install nodejs, we have the simple command nvm install node which installs the latest release of nodejs but if we want to install some specific versions then instead of the node we give the version of nodejs that we want to install. In this case, it is 14.18.0. Press enter and it will download and install our desired version of the node. You can verify if it is installed or not by checking the version of nodejs.

You can install as many versions of the node you want on your computer. If you want to switch to nodejs version n from version m then there is indeed an easy way to switch between them with the command nvm use version_of_node. In this case, it's v14.18.0, hit enter and you can verify again by checking the version of nodejs. And you can also check the version of npm.

Those were the ways you can install nodejs with. Watch this video fully for the detailed information:
Nodejs installation in linux

Discussion (0)