DeveloperBreeze

How to Update Node.js and npm on Ubuntu

In this tutorial, you'll learn how to update Node.js and npm to the latest version on an Ubuntu-based system. This is particularly useful when you're facing compatibility issues with outdated Node.js versions, or when your project requires newer features from the latest releases.

Step 1: Check Current Node.js and npm Versions

To begin, check the current versions of Node.js and npm installed on your system:

node -v
npm -v

If the version is outdated (e.g., Node.js v12 or earlier), it's time to upgrade.

Step 2: Remove the Old Version of Node.js (If Needed)

If you have an older version of Node.js, you can remove it to prevent conflicts during installation of the new version.

sudo apt remove nodejs

This command removes the old version of Node.js from your system.

Step 3: Add NodeSource Repository for Node.js

NodeSource provides an easy way to install and manage Node.js. To install a specific Node.js version, add the NodeSource repository for the version you want.

For Node.js 18 (LTS) (recommended):

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

For Node.js 20 (latest version):

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

This command adds the NodeSource repository to your system, allowing you to install newer versions of Node.js.

Step 4: Install Node.js

Once the repository is added, install Node.js:

sudo apt install -y nodejs

This installs Node.js and the latest version of npm that comes with it.

Step 5: Verify Node.js and npm Installation

After installation, verify the new versions of Node.js and npm:

node -v
npm -v

You should now see the updated versions of Node.js (v18.x or v20.x, depending on your choice) and npm.

Step 6: Clean npm Cache (Optional but Recommended)

Sometimes, older npm cache files can cause issues with new installs. Clean the npm cache to ensure a smooth experience:

npm cache clean --force

Step 7: Reinstall Project Dependencies (If Applicable)

If you're working on a Node.js project, you might want to reinstall your project's dependencies to ensure compatibility with the updated Node.js version. Navigate to your project directory and run the following commands:

  1. Remove node_modules and package-lock.json:
   rm -rf node_modules package-lock.json
  1. Reinstall dependencies:
   npm install

Conclusion

Updating Node.js and npm is an essential task for keeping your development environment up to date. Following this tutorial ensures that you’re running the latest, most secure, and feature-rich versions of Node.js and npm.

Feel free to restart any services or your system as needed after completing these steps.


Related Posts

More content you might like

Tutorial

Build a Custom Rate Limiter in Node.js with Redis

REDIS_URL=redis://localhost:6379
// redisClient.js
const redis = require("redis");

const client = redis.createClient({ url: process.env.REDIS_URL });

client.on("error", (err) => console.error("Redis error:", err));
client.connect();

module.exports = client;

Apr 04, 2025
Read More
Tutorial
javascript

Using Node.js to Run JavaScript

  • Use it in your code:
     const _ = require('lodash');
     console.log(_.capitalize("hello world"));

Dec 10, 2024
Read More
Tutorial

Connecting a Node.js Application to an SQLite Database Using sqlite3

When working with databases, especially those storing sensitive information like private keys, adhering to security best practices is essential.

  • Encryption: Store sensitive data, such as private keys, in an encrypted format.
  • Access Controls: Limit who and what can access the database.

Oct 24, 2024
Read More
Tutorial
javascript

Creating a Component Library with Storybook and React

// src/index.js
export { default as Button } from './components/Button/Button';

Finally, publish your library to npm:

Aug 27, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!