DEV Community

Cover image for Installing Node.js and npm on Ubuntu 26.04
Sanskriti Harmukh for Vultr

Posted on • Originally published at docs.vultr.com

Installing Node.js and npm on Ubuntu 26.04

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
Enter fullscreen mode Exit fullscreen mode

2. Download the NodeSource setup script:

$ curl -fsSL https://deb.nodesource.com/setup_24.x -o nodesource_setup.sh
Enter fullscreen mode Exit fullscreen mode

3. Run the setup script:

$ sudo bash nodesource_setup.sh
Enter fullscreen mode Exit fullscreen mode

4. Install Node.js:

$ sudo apt install nodejs -y
Enter fullscreen mode Exit fullscreen mode

5. Verify the installation:

$ node -v
$ npm -v
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

2. Reload the shell environment:

$ source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

3. Install the latest LTS release:

$ nvm install --lts
Enter fullscreen mode Exit fullscreen mode

4. Install a specific version:

$ nvm install 24
Enter fullscreen mode Exit fullscreen mode

5. Switch to that version:

$ nvm use 24
Enter fullscreen mode Exit fullscreen mode

6. Set it as the default:

$ nvm alias default 24
Enter fullscreen mode Exit fullscreen mode

7. Verify the active version:

$ node -v
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

2. Initialize the project:

$ npm init -y
Enter fullscreen mode Exit fullscreen mode

3. Install Express:

$ npm install express
Enter fullscreen mode Exit fullscreen mode

4. Create the application file:

$ nano index.js
Enter fullscreen mode Exit fullscreen mode

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}`)
})
Enter fullscreen mode Exit fullscreen mode

5. Open port 3000 in the firewall:

$ sudo ufw allow 3000/tcp
Enter fullscreen mode Exit fullscreen mode

6. Start the application:

$ node index.js
Enter fullscreen mode Exit fullscreen mode

7. Test in a second terminal:

$ curl http://localhost:3000
Enter fullscreen mode Exit fullscreen mode

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-remote to browse all available Node.js releases

For the complete guide, visit the original article on Vultr Docs.

Top comments (0)