Talal Zaman Abbasi

Web Designer

Blogger

Social Media Marketer

SEO Expert

Talal Zaman Abbasi

Web Designer

Blogger

Social Media Marketer

SEO Expert

Blog Post

How to Install Node.js on Ubuntu – Node Linux Installation Guide

October 20, 2023 Design
How to Install Node.js on Ubuntu – Node Linux Installation Guide

[ad_1]

If you are a web developer working on the frontend or the backend, you’ll want to have Node.js installed on your system.

But if you use the typical sudo apt install nodejs command, it may install a very old version of Node which can be troublesome for you.

So you’ll want to install a specific version, which requires a different command. This will install the LTS (Long-Term Support) version of Node which is useful for devs because it has a longer period for support.

Today, I am going to show you how you can install the latest LTS version of Node on your Ubuntu operating system.

This processes will work on any kind of Debian-based Linux operating system (Ubuntu, Mint, Zorin, Debian, Elementary OS, and so on). It’ll work whether you are using that as your main operating system, secondary operating system on dual boot, WSL on Windows, or using in a virtual machine (VMware Workstation, VirtualBox, and so on).

Video Tutorial

I have also created a complete video to show you the process step-by-step. You can watch it here:

At the time of writing this article, the latest LTS version for Node is 18.18.2.

Screenshot-2023-10-20-141242
Node download page showing current LTS version

When you install Node following the instructions in this article, it will install the latest LTS version of Nodejs automatically. So you’ll be safe without any hassle if you simply follow this article and the accompanying video.

Update Your Operating System

First, you’ll want to ensure that you have installed all the updates beforehand. I like to work in the terminal mostly, so I’ll install the updates using the terminal directly.

For updating to the latest versions of all the relevant packages, use sudo apt update in the terminal. Use your password when it asks for that.

Screenshot-2023-10-20-104647
Updating all relevant packages

Now use sudo apt upgrade -y to upgrade all the upgradable packages.

Screenshot-2023-10-20-120347
Upgrading all relevant packages

Install CURL

We’re using the Node Version Manager (NVM) here to install Node. There are various advantages when we install Node and npm using the NVM as it also allows us to manage multiple versions of Node.js on our system altogether.

First, you’ll need to install curl if it’s not installed on your system already. You can install curl by using the command below:

sudo apt install curl -y
Screenshot-2023-10-20-122355
Installing CURL

How to Install Node.js

Now you’ll need to follow these steps in order to ensure that you’ve installed Node.js successfully on your system.

Install Node Version Manager (NVM)

Install the Node Version Manager (NVM) by using the following command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
Screenshot-2023-10-20-122423
Installing the Node Version Manager (NVM)

When you run this specific command, the curl downloads the NVM installation script from that specific URL. Afterward, bash executes the same script for installing NVM.

Activate NVM

Activate the NVM using the command below:

source ~/.bashrc
Screenshot-2023-10-20-122517
Activating the Node Version Manager (NVM)

Install the latest LTS version of Node

Install the latest Long Term Support version of Node by using the command below:

nvm install --lts
Screenshot-2023-10-20-122656
Command for installing the latest LTS version of Node.js

It installs the latest version of the LTS release of Node by default.

Make the default LTS version as NVM

We have installed the latest LTS version of Node, but we also need to set the default version of NVM so that it gets used by default whenever we need it. You can use the command below to do that. Make sure to change the version to the exact LTS version you have installed on your system just now.

nvm alias default 18.18.2
Screenshot-2023-10-20-122842
Selecting the appropriate Node version as the default version

If your LTS version is something like 24.1.2 then the command would be like below:

nvm alias default 24.1.2

Confirm that Node was installed

Use the command below to check whether the default version is the exact version you just installed:

node -v npm -v
Screenshot-2023-10-20-122937
Showing the current version of Node installed

How to Set Up the Node.js Environment

After installing Node and NPM, you need to set up the Node environment by creating a new Node project.

Use the command below to create a new directory/folder where you want to test a simple “Hello World” type Node project.

mkdir my-node-project
Screenshot-2023-10-20-123101
Creating a new directory/folder to test a simple “Hello World” program on Node

Navigate to the my-node-project directory by using the command below:

cd my-node-project
Screenshot-2023-10-20-123148
Changing the directory to enter into that newly created directory/folder

Initialize the new Node project like this:

npm init -y

This command will create a “package.json” file containing your project’s metadata and dependencies. Here is the JSON output:

Screenshot-2023-10-20-123304-1
Initializing npm in the folder

The JSON output is below:

{
  "name": "my-node-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Now run the setup with the simple command. For this, I am going to create a new file called app.js using the nano text editor in the terminal.

sudo nano app.js
Screenshot-2023-10-20-123550
Opening app.js file in nano

Once the text editor opens, type the below code:

console.log("Hello, Node.js from Ubuntu!");
Screenshot-2023-10-20-123710
Writing a simple console.log code in the app.js file using nano

Use Ctrl+ O to save the file. Use Enter to save the file as app.js:

Screenshot-2023-10-20-123831
Save the app.js file with the newly added line of code

Use Ctrl + X to return to the bash terminal again.

Screenshot-2023-10-20-123907
Returning to the terminal again

Now, it is time to check the output and see whether it’s working or not.

Use the command below:

node app.js
Screenshot-2023-10-20-124010
Running the app.js file using Node

It is working!

We have successfully installed the latest LTS release of Node on our Ubuntu/Debian-based Linux operating system.

Cheers! 🥂

Conclusion

Thank you so much for reading the entire article till now.

If you have enjoyed the procedures step-by-step, then don’t forget to let me know on Twitter/X or LinkedIn.

You can follow me on GitHub as well if you are interested in open source. Make sure to check my website (https://fahimbinamin.com/) as well!

If you like to watch programming and technology-related videos, then you can check my YouTube channel, too.

All the best for your programming and development journey. 😊

You can do it! Don’t give up, never! ❤️



[ad_2]

Source link

Write a comment