This article explains how to install Node.js and NPM on Ubuntu 24.04 LTS. You will install both Node.js and NPM using the Node Source PPA and run multiple versions using NVM (Node Version Manager) on your server.
Node.js is an open-source, cross-platform JavaScript runtime environment for developing server-side scalable applications. The Node Package Manager (NPM) is an essential tool for managing libraries, dependencies, and scripts in a Node.js project.
Table of Contents
Prerequisites
- Ubuntu 24.04 LTS
- SSH access with sudo privileges
- Firewall Port: 3000
Step#1:Install NodeJS and NPM on Ubuntu 24.04 LTS
To install the latest version of Node.js, use the Node Source PPA to install the latest repository information for a specific Node.js version.
curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh
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
Run the Node.js setup script.
sudo -E bash nodesource_setup.sh
Install Node.js and NPM using the following command.
sudo apt-get install -y nodejs npm
Install node.js
sudo apt install nodejs
View the installed Node.js version on your server.
node -v
Output:
ubuntu@ip-172-31-8-16:~$ node -v
v22.7.0
Check the installed NPM version on your server.
npm -v
Output:
ubuntu@ip-172-31-8-16:~$ npm -v
10.8.2
Step#2:Create Simple NodeJS App
Create a new project directory name with example-site
.
mkdir example-site
Switch to the directory.
cd example-site
Initialize a new Node.js project using npm
.
npm init -y
Install the express
module using npm
.
npm install express
Create a new index.js
application file.
sudo vi index.js
Add the following code to the file.
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World! Greetings from Vultr')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
Save and close the file.
Allow port 3000
through the firewall to enable network connections to the application.
The above Node.js code creates a new express application that listens for connections on the host port 3000
and outputs is shown below.
sudo ufw allow 3000
Start the Node.js application.
node index.js
Access your Node.js application using your server IP and port 3000
in a web browser such as Chrome.
http://Hostname:3000
Step#3:Install Multiple Versions with Node Version Manager (NVM)
NVM (Node Version Manager) is a useful tool that enables you to install and switch between multiple Node.js versions on your server.
Download the latest NVM installation script on your server.
curl -O https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh
Run the script to install NVM in your user environment.
bash install.sh
Output:
ubuntu@ip-172-31-8-16:~$ bash install.sh
=> Downloading nvm from git to '/home/ubuntu/.nvm'
=> Cloning into '/home/ubuntu/.nvm'...
remote: Enumerating objects: 376, done.
remote: Counting objects: 100% (376/376), done.
remote: Compressing objects: 100% (320/320), done.
remote: Total 376 (delta 46), reused 176 (delta 29), pack-reused 0 (from 0)
Receiving objects: 100% (376/376), 372.57 KiB | 1.70 MiB/s, done.
Resolving deltas: 100% (46/46), done.
* (HEAD detached at FETCH_HEAD)
master
=> Compressing and cleaning up git repository
=> Appending nvm source string to /home/ubuntu/.bashrc
=> Appending bash_completion source string to /home/ubuntu/.bashrc
=> You currently have modules installed globally with `npm`. These will no
=> longer be linked to the active version of Node when you install a new node
=> with `nvm`; and they may (depending on how you construct your `$PATH`)
=> override the binaries of modules installed with `nvm`:
/usr/lib
├── [email protected]
=> If you wish to uninstall them at a later point (or re-install them under your
=> `nvm` Nodes), you can remove them from the system Node as follows:
$ nvm use system
$ npm uninstall -g a_module
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Reload your server environment variables.
source ~/.profile
List all available Node.js versions you can install on your server.
nvm ls-remote
Output:
ubuntu@ip-172-31-8-16:~$ nvm ls-remote
v0.1.14
v0.1.15
v0.1.16
v0.1.17
v0.1.18
v0.1.19
v0.1.20
v0.1.21
v0.1.22
v0.1.23
v0.1.24
v0.1.25
v0.1.26
v0.1.27
v0.1.28
v0.1.29
v0.1.30
v0.1.31
v0.1.32
v0.1.33
v0.1.90
v0.1.91
v0.1.92
v0.1.93
v0.1.94
v0.1.95
v0.1.96
v0.1.97
v0.1.98
v0.1.99
v0.1.100
v0.1.101
v0.1.102
v0.1.103
v0.1.104
v0.2.0
v0.2.1
v0.2.2
v0.2.3
v0.2.4
v0.2.5
v0.2.6
v0.3.0
v0.3.1
v0.3.2
v0.3.3
v0.3.4
v0.3.5
v0.3.6
v0.3.7
v0.3.8
v0.4.0
v0.4.1
v0.4.2
v0.4.3
v0.4.4
v0.4.5
v0.4.6
v0.4.7
v0.4.8
v0.4.9
v0.4.10
v0.4.11
v0.4.12
v0.5.0
v0.5.1
v0.5.2
v0.5.3
v0.5.4
v0.5.5
v0.5.6
v0.5.7
v0.5.8
v0.5.9
v0.5.10
v0.6.0
v0.6.1
v0.6.2
v0.6.3
v0.6.4
v0.6.5
v0.6.6
v0.6.7
v0.6.8
v0.6.9
v0.6.10
v0.6.11
v0.6.12
v0.6.13
v0.6.14
v0.6.15
v0.6.16
v0.6.17
v0.6.18
v0.6.19
v0.6.20
v0.6.21
v0.7.0
v0.7.1
v0.7.2
v0.7.3
v0.7.4
v0.7.5
v0.7.6
v0.7.7
v0.7.8
v0.7.9
v0.7.10
v0.7.11
v0.7.12
v0.8.0
v0.8.1
v0.8.2
v0.8.3
v0.8.4
v0.8.5
v0.8.6
v0.8.7
v0.8.8
v0.8.9
v0.8.10
v0.8.11
v0.8.12
v0.8.13
v0.8.14
v0.8.15
v0.8.16
v0.8.17
v0.8.18
v0.8.19
More....
Install a specific Node.js version. For example, Node.js 20.
nvm install 20
Output:
ubuntu@ip-172-31-8-16:~$ nvm install 20
Downloading and installing node v20.17.0...
Downloading https://nodejs.org/dist/v20.17.0/node-v20.17.0-linux-x64.tar.xz...
###################################################################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v20.17.0 (npm v10.8.2)
Creating default alias: default -> 20 (-> v20.17.0)
List all installed Node.js versions and verify the default version on your server.
nvm ls
Output:
ubuntu@ip-172-31-8-16:~$ nvm ls
-> v20.17.0
system
default -> 20 (-> v20.17.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v20.17.0) (default)
stable -> 20.17 (-> v20.17.0) (default)
lts/* -> lts/iron (-> v20.17.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.2 (-> N/A)
lts/hydrogen -> v18.20.4 (-> N/A)
lts/iron -> v20.17.0
Run below command to install a specific Node.js version such as 20
using NVM.
nvm use 20
Output:
ubuntu@ip-172-31-8-16:~$ nvm use 20
Now using node v20.17.0 (npm v10.8.2)
View the active Node.js version in your user environment.
node -v
Output:
ubuntu@ip-172-31-8-16:~$ node -v
v20.17.0
View the NPM version in your user environment.
npm -v
Output:
ubuntu@ip-172-31-8-16:~$ npm -v
10.8.2
Run below command to a specific version as the default Node.js version in your environment.
nvm alias default 20
Output:
ubuntu@ip-172-31-8-16:~$ nvm alias default 20
default -> 20 (-> v20.17.0)
Here, we have wrapped up how to install Node.js and NPM on your Ubuntu 24.04 server. You can develop applications and switch between different Node.js versions on your server using the Node Version Manager (NVM).
Related Articles:
How to Install Latest Node.js and NPM on Ubuntu 19.04,18.04/16.04 LTS
How to Install Angular CLI on Ubuntu 18.04/16.04 LTS
How to Install node.js on Mac OS How to Install Node.js on Windows 10
How to Install Node.js and NPM on Ubuntu 20.04 LTS
Reference: