Installing Node.js and npm on Ubuntu

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine, while npm (Node Package Manager) is a package manager for JavaScript, allowing developers to install libraries and tools. This guide will show you how to install both Node.js and npm on Ubuntu, and verify the installation.

← Back

Step 1: Update Package Index

Before installing Node.js, it’s a good practice to update the package index on your system:

sudo apt update

Step 2: Install Node.js from NodeSource Repository

To get the latest version of Node.js, it’s best to install it from the official NodeSource repository. First, install the required dependencies:

sudo apt install curl software-properties-common

Next, download and add the NodeSource repository for the desired version of Node.js. For example, to install Node.js 16.x, run:

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

Then, install Node.js with the following command:

sudo apt install nodejs

Step 3: Verify Installation

After installation, you can verify that Node.js and npm have been installed correctly by checking their versions:

node -v

You should see something like:

v16.x.x

To verify the version of npm, run:

npm -v

This should output the npm version, for example:

7.x.x

Step 4: Install Additional Packages (Optional)

To ensure that npm can install packages that require building native modules, you may need to install the build tools:

sudo sudo apt install build-essential

Step 5: Updating Node.js and npm

If you need to update Node.js or npm in the future, you can use the following commands:

sudo apt install --only-upgrade nodejs
sudo npm install -g npm@latest

Conclusion

Congratulations, you have successfully installed Node.js and npm on your Ubuntu system! Now you can start building JavaScript-based applications and using npm to manage packages. If you have any issues or need more help, check the official Node.js and npm documentation for further guidance.

← Back