Ubuntu's default APT repository ships an outdated Node.js build that is not suitable for modern JavaScript development. This guide installs Node.js 24.x from the NodeSource repository and covers NVM as an alternative for managing multiple versions side by side, verified with a running Express application confirming the setup.
Install Node.js via NodeSource
NodeSource maintains a Debian/Ubuntu repository that tracks current Node.js major releases, providing a single pinned version installed system-wide.
1. Update the APT package index:
$ sudo apt update
2. Download the NodeSource setup script:
$ curl -fsSL https://deb.nodesource.com/setup_24.x -o nodesource_setup.sh
3. Run the setup script:
$ sudo bash nodesource_setup.sh
4. Install Node.js:
$ sudo apt install nodejs -y
5. Verify the installation:
$ node -v
$ npm -v
Manage Multiple Versions with NVM
NVM allows installing and switching between Node.js versions without affecting system-wide packages, making it suitable for environments where different projects require different runtimes.
1. Download and run the NVM installer:
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
2. Reload the shell environment:
$ source ~/.bashrc
3. Install the latest LTS release:
$ nvm install --lts
4. Install a specific version:
$ nvm install 24
5. Switch to that version:
$ nvm use 24
6. Set it as the default:
$ nvm alias default 24
7. Verify the active version:
$ node -v
Test the Installation
A minimal Express application confirms Node.js and npm are installed and working correctly.
1. Create a project directory:
$ mkdir ~/example-app && cd ~/example-app
2. Initialize the project:
$ npm init -y
3. Install Express:
$ npm install express
4. Create the application file:
$ nano index.js
Add the following:
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World from Node.js')
})
app.listen(port, () => {
console.log(`Application listening on port ${port}`)
})
5. Open port 3000 in the firewall:
$ sudo ufw allow 3000/tcp
6. Start the application:
$ node index.js
7. Test in a second terminal:
$ curl http://localhost:3000
The response Hello World from Node.js confirms the server is running correctly.
Next Steps
Node.js and npm are now installed and serving requests. From here you can:
- Deploy your application with PM2 for process management and automatic restarts on failure
- Configure Nginx as a reverse proxy to serve the application on port 80
- Use
nvm ls-remoteto browse all available Node.js releases
For the complete guide, visit the original article on Vultr Docs.
Top comments (0)