How To

How to Install Node.js and npm on Ubuntu

If you want to build JavaScript applications on Linux, learning how to Install Node.js Ubuntu systems properly is essential. Node.js allows developers to run JavaScript outside the browser, while npm helps manage packages and project dependencies efficiently.

Ubuntu users can install Node.js using several methods depending on their workflow. Some prefer Ubuntu’s built-in packages for simplicity, while developers often use NodeSource or nvm for newer releases and version management.

Install Node.js Ubuntu Using APT

The easiest way to install Node.js on Ubuntu is through the default APT repositories. This method works well for stable environments and basic development tasks.

First, update your package lists:

sudo apt update

Then install Node.js and npm:

sudo apt install nodejs npm

After installation, verify both tools:

node -vnpm -v

Ubuntu’s repositories usually provide a stable but older release. While this approach is convenient, it may not include the latest LTS version required for modern frameworks.

To compile native npm packages, install development tools as well:

sudo apt install build-essential

Install Node.js Ubuntu with NodeSource

For users who need newer Node.js versions, NodeSource provides an official repository with updated releases.

Start by installing required dependencies:

sudo apt install ca-certificates curl gnupg

Next, import the NodeSource signing key:

sudo mkdir -p /etc/apt/keyringscurl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

Add the repository:

NODE_MAJOR=24echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list

Finally, install Node.js:

sudo apt updatesudo apt install nodejs

This method installs both Node.js and npm together.

Install Node.js Ubuntu with nvm

Developers who work on multiple projects often prefer nvm, also known as Node Version Manager. It allows easy switching between different Node.js versions without affecting the entire system.

Install nvm using:

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash

Reload your terminal and verify installation:

nvm -v

Install the latest LTS version:

nvm install --lts

You can also install older versions:

nvm install 22nvm install 20

Switch between versions anytime:

nvm use 22

This flexibility makes nvm ideal for modern JavaScript development environments.

Common Node.js Installation Issues

Sometimes users encounter problems like node: command not found or missing npm packages. In most cases, these issues happen because the shell session was not refreshed or Node.js was installed incorrectly.

For nvm users, reopening the terminal usually resolves the issue.

Permission problems with global npm packages are another common concern. Using nvm helps avoid these conflicts because packages remain inside the user directory instead of system folders.

Conclusion

Choosing the right way to Install Node.js depends on your workflow. Ubuntu’s default repositories are simple and stable, NodeSource offers newer production-ready releases, and nvm provides the most flexibility for developers managing multiple projects.

For most developers, nvm remains the preferred choice because it makes switching between Node.js versions fast, clean, and hassle-free.

Cyber Defence

Recent Posts

groupdel Command in Linux: Remove a Group and Audit Files

The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…

12 hours ago

wc Command in Linux: Count Lines, Words, Characters, and Bytes

The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It…

13 hours ago

top Command in Linux: Monitor Processes and Resource Usage

The top command in Linux provides a real-time view of running processes and system resource usage. From…

13 hours ago

usermod Command in Linux: Modify User Accounts and Groups

The usermod command in Linux modifies existing user account attributes. You can use it to manage group…

13 hours ago

sort Command in Linux: Sort Text, Numbers, Columns, and More

The sort command in Linux reads lines from files or standard input and writes them to standard…

2 days ago

wall Command in Linux: Broadcast Messages to Logged-In Users

The wall command in Linux sends a message to the terminals of all currently logged-in users. The…

2 days ago