How to Install Node.js and NPM on Ubuntu 24.04?
Node.js is an open-source, cross-platform JavaScript runtime environment which enables the development of scalable server-side applications. And, Node Package Manager (NPM) is an important tool for managing libraries, dependencies, and scripts in a Node.js project.
Together they help empower developers to create dynamic and efficient applications.
This blog explains how to install node.js and NPM on Ubuntu 24.04.
Prerequisites
Before you begin, check if the below requirements are met:
- SSH access with sudo privileges
- Ubuntu 24.04 LTS
Installation of NodeJS and NPM
1. To install the new version of Node.js, use the Node Source PPA to install the repository information for a specific Node.js version.
curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh
Sample Output
ubuntu@ip-172-31-8-16:~$ curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh ubuntu@ip-172-31-8-16:~$ sudo -E bash nodesource_setup.sh 2024-08-23 05:13:05 - Installing pre-requisites Hit:1 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu noble InRelease Hit:2 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu noble-updates InRelease Hit:3 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu noble-backports InRelease Hit:4 http://security.ubuntu.com/ubuntu noble-security InRelease Reading package lists... Done Reading package lists... Done Building dependency tree... Done Reading state information... Done ca-certificates is already the newest version (20240203). ca-certificates set to manually installed. gnupg is already the newest version (2.4.4-2ubuntu17). gnupg set to manually installed. The following NEW packages will be installed: apt-transport-https The following packages will be upgraded: curl libcurl3t64-gnutls libcurl4t64 3 upgraded, 1 newly installed, 0 to remove and 83 not upgraded. Need to get 904 kB of archives. After this operation, 38.9 kB of additional disk space will be used. Get:1 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu noble/universe amd64 apt-transport-https all 2.7.14build2 [3974 B] Get:2 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu noble-updates/main amd64 curl amd64 8.5.0-2ubuntu10.3 [227 kB] Get:3 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu noble-updates/main amd64 libcurl4t64 amd64 8.5.0-2ubuntu10.3 [341 kB] Get:4 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu noble-updates/main amd64 libcurl3t64-gnutls amd64 8.5.0-2ubuntu10.3 [333 kB] Fetched 904 kB in 0s (29.6 MB/s) Selecting previously unselected package apt-transport-https. (Reading database ... 67741 files and directories currently installed.) Preparing to unpack .../apt-transport-https_2.7.14build2_all.deb ... Unpacking apt-transport-https (2.7.14build2) ... Preparing to unpack .../curl_8.5.0-2ubuntu10.3_amd64.deb ... Unpacking curl (8.5.0-2ubuntu10.3) over (8.5.0-2ubuntu10.1) ... Preparing to unpack .../libcurl4t64_8.5.0-2ubuntu10.3_amd64.deb ... Unpacking libcurl4t64:amd64 (8.5.0-2ubuntu10.3) over (8.5.0-2ubuntu10.1) ... Preparing to unpack .../libcurl3t64-gnutls_8.5.0-2ubuntu10.3_amd64.deb ... Unpacking libcurl3t64-gnutls:amd64 (8.5.0-2ubuntu10.3) over (8.5.0-2ubuntu10.1) ... Setting up apt-transport-https (2.7.14build2) ... Setting up libcurl4t64:amd64 (8.5.0-2ubuntu10.3) ... Setting up libcurl3t64-gnutls:amd64 (8.5.0-2ubuntu10.3) ... Setting up curl (8.5.0-2ubuntu10.3) ... Processing triggers for man-db (2.12.0-4build2) ... Processing triggers for libc-bin (2.39-0ubuntu8.2) ... Scanning processes... Scanning candidates... Scanning linux images... Running kernel seems to be up-to-date. Restarting services... systemctl restart packagekit.service No containers need to be restarted. No user sessions are running outdated binaries. No VM guests are running outdated hypervisor (qemu) binaries on this host. Hit:1 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu noble InRelease Hit:2 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu noble-updates InRelease Hit:3 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu noble-backports InRelease Get:4 https://deb.nodesource.com/node_22.x nodistro InRelease [12.1 kB] Hit:5 http://security.ubuntu.com/ubuntu noble-security InRelease Get:6 https://deb.nodesource.com/node_22.x nodistro/main amd64 Packages [3024 B] Fetched 15.2 kB in 1s (21.0 kB/s) Reading package lists... Done 2024-08-23 05:13:15 - Repository configured successfully. 2024-08-23 05:13:15 - To install Node.js, run: apt-get install nodejs -y 2024-08-23 05:13:15 - You can use N|solid Runtime as a node.js alternative 2024-08-23 05:13:15 - To install N|solid Runtime, run: apt-get install nsolid -y
2. Run the Node.js setup script using the below command
sudo -E bash nodesource_setup.sh
3. Install both Node.js and NPM using the below command.
sudo apt-get install -y nodejs npm
4. Install node.js
sudo apt install nodejs
5. View and verify the installed Node.js version on the server.
node -v
Output:
ubuntu@ip-172-31-8-16:~$ node -v v22.7.0
6. Verify the installed NPM version on the server.
npm -v
Sample Output
ubuntu@ip-172-31-8-16:~$ npm -v 10.8.2
FAQ’s
How to force npm install?
Force Installation with –force or -f flag
The –force flag will ignore any warnings and continue with the installation and this is helpful in cases where warnings prevent the installation, but it should be used with caution.
What is the difference between Node.js and npm modules?
Node.js is a piece of code that can be reusable and it encapsulates the related functionality. It can be a one single file or a collection of many files organized in a directory. Modules help in organizing code into manageable and small pieces to promote modularity and reuse of code.
Do I need to install a node for npm?
Node.js and the npm command line interface must be installed using either a Node version manager or a Node installer to publish and install packages to and from the public npm registry or a private npm registry, you must install Node.js and the npm command line interface using either a Node version manager or a Node installer.