Published on October 03, 2024By DeveloperBreeze

Tutorial: 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.

Comments

Please log in to leave a comment.

Continue Reading:

Weather App with Node.js

Published on August 08, 2024

javascript

Installing Solana on Ubuntu

Published on August 09, 2024

bashrust

Fetching Address Details from Solana

Published on August 09, 2024

javascriptjson

Tracking Newly Created Tokens on Ethereum

Published on August 09, 2024

javascriptnodejs

Tracking Newly Created Tokens on Solana

Published on August 09, 2024

javascriptnodejs

Tracking Solana Address for New Trades and Amounts

Published on August 09, 2024

javascriptnodejs

Node.js: How to Create an HTTP Server

Published on August 12, 2024

javascriptnodejs

GraphQL API Server with Node.js and Apollo Server

Published on August 12, 2024

nodejsgraphql