How to Install the Latest Node.js on Ubuntu 22.04: A Step-by-Step Guide

How to Install the Latest Node.js on Ubuntu 22.04: A Step-by-Step Guide

First Way

Refresh the Apt cache to update the repository:

sudo apt update                   

Install Node.js by entering the following command:`

sudo apt-get install nodejs                   

Once finished, install the NPM Node.js package manager by running the following:

sudo apt install npm                  

Check the Node.js and NPM version numbers to confirm it has been successfully installed. The command-line will return the installed version number after you enter these commands:

nodejs -v
npm -v

Suppose you want to remove Node.js or NPM from the Linux system. If this is the case, use the following commands:

sudo apt remove nodejs
sudo apt remove npm

This will not install the latest version

Second Way(Using https://deb.nodesource.com/)

This method is ideal if you want to install a specific or the latest Node.js release. Here’s how to install Node.js on Ubuntu 22.04 using the NodeSource repository:

Enter the following commands hitting Enter after each one:

sudo apt-get update
sudo apt-get upgrade

Skip this step if you have cURL installed already. Otherwise, enter the following command:

sudo apt-get install curl
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -
sudo apt-get install -y nodejs

If you have used the first way you may encounter a problem when you will try to install with the second way.

This error is commonly associated with issues in the package installation process which might be due to corrupted package downloads, unmet dependencies, or conflicts with other installed packages. Here are a few steps you can try to resolve this issue:

  1. Clean the package cache: Sometimes a corrupted download can cause this error. Clearing the cache and re-downloading the packages can help:

    sudo apt-get clean
    sudo apt-get update
    
  2. Fix broken packages: There might be broken dependencies that need to be fixed:

    sudo apt-get install -f
    
  3. Reconfigure packages: If dpkg was interrupted, you might need to reconfigure packages:

    sudo dpkg --configure -a
    
  4. Remove the problematic package: If the issue is with a specific package, try removing it and then reinstall:

    sudo dpkg --remove --force-remove-reinstreq nodejs
    sudo apt-get install nodejs
    
  5. Check for dpkg status: Sometimes, the dpkg status of a package might be corrupted. You can check and correct it manually:

    • Open the dpkg status file in a text editor:
      sudo nano /var/lib/dpkg/status
      
    • Locate the entry for nodejs and carefully edit out any corrupt or half-installed statuses. Save and close the editor.
  6. Use aptitude instead of apt-get: Aptitude can sometimes resolve package conflicts better:

    sudo aptitude install nodejs
    

If you're encountering dependency problems that prevent the removal of Node.js, it means that there are other packages installed on your system that rely on Node.js. To resolve this, you can try a few different strategies to either properly remove Nodejs or resolve the dependency issues:

1. Remove Node.js along with dependent packages You can attempt to remove Node.js and any packages that depend on it using the following command. Be cautious with this approach, as it might remove additional packages:

sudo apt-get remove --auto-remove nodejs

2. Force removal of Node.js (not recommended)

You can force the removal of Node.js, but this is generally not recommended as it could potentially leave your system in an inconsistent state:

sudo dpkg --remove --force-depends nodejs

Use this command only if you understand the risks and are prepared to deal with possible broken dependencies.

3. Resolve the dependencies It might be more prudent to try to resolve the dependencies first. You can see what packages are causing the issue with:

sudo apt-get -f install

This command tries to fix broken dependencies by completing the installation of pending packages or repairing broken ones.

4. Use dpkg to inspect dependency issues You can list what packages depend on Node.js, which might give you better insight into why it can't be removed:

dpkg -l | grep nodejs

And to see specifically what depends on Node.js:

apt-cache rdepends nodejs

5. Reinstall Node.js Sometimes, reinstalling the package can resolve dependency issues and then allow for a clean removal:

sudo apt-get install --reinstall nodejs
sudo apt-get remove nodejs

6. Check if other versions of Node.js are installed Sometimes conflicts arise because multiple versions of Node.js are installed, possibly from different sources (like nodesource and the default Ubuntu repositories). Check and clean as needed:

which node
which npm

Check the paths and manually remove or update alternatives if multiple versions exist.

After performing these steps, try removing Node.js again. If you're still encountering issues, consider looking at the specific errors provided in the output to determine if there's a more specific problem that needs addressing.

Third Way (Using NVM)

Installing a specific version of Node.js can be effectively managed using Node Version Manager (NVM). NVM allows you to install and switch between different versions of Node.js easily, which is especially useful in development environments. Here’s a step-by-step guide on how to install a specific version of Node.js using NVM on a Linux or macOS system:

Step 1: Install NVM

First, you'll need to install NVM itself. You can do this by using the curl or wget command to download and run the installation script. Open your terminal and run one of these commands:

Using curl:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Using wget:

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

This script clones the nvm repository to ~/.nvm, and attempts to add the source lines to your profile (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).

Step 2: Source Your Profile

To use nvm, you need to source your profile or restart your terminal. If you prefer not to restart, you can source the profile file where the script added the nvm script. Typically, this is your .bashrc, .zshrc, or equivalent:

source ~/.bashrc

Or, for zsh users:

source ~/.zshrc

Step 3: Install a Specific Version of Node.js

Now that NVM is installed, you can install a specific version of Node.js. To install a particular version, use the nvm install command followed by the version number. For example, to install Node.js version 14.17.0, you would run:

nvm install 22.0.0

Step 4: Using the Installed Version

After installing, you can switch to the version you installed using nvm use:

nvm use 22.0.0

This command switches the current terminal session to use the specified version of Node.js.

Step 5: Verify the Installation

To make sure that the correct version of Node.js is being used, check the version:

node -v

This should display the version number that you installed, confirming that the installation was successful.

Additional Tips

  • List Installed Versions: You can see all installed Node.js versions by running:

    nvm ls
    
  • Check all the available versions of Node.js using NVM by running the following command:

    nvm ls-remote
    
  • Install the Latest Version: To install the latest available version of Node.js, you can simply use:

    nvm install node
    
  • Default Version: To set a default Node.js version for any new shells, use:

    nvm alias default <version>
    

Using NVM is a powerful way to manage multiple Node.js versions for different projects or needs, ensuring compatibility and ease of use across environments.

More to read