Skip to content

Installing Node.js and Package Managers on Ubuntu Server 22.04

This guide will help you install the latest version of Node.js and other package managers on Ubuntu Server 22.04.

Step 1: Update the System

First, update the package index and upgrade the installed packages to the latest versions:

bash
sudo apt update
sudo apt upgrade -y

Step 2: Install Node.js

To install the latest version of Node.js, you can use the NodeSource repository. Run the following commands:

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

Verify the installation:

bash
node -v
npm -v

Step 3: Install Yarn

Yarn is a popular package manager for Node.js. To install Yarn, follow these steps:

bash
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install -y yarn

Verify the installation:

bash
yarn -v

Step 4: Install nvm (Node Version Manager)

nvm allows you to manage multiple versions of Node.js. To install nvm, run the following command:

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

Activate nvm by adding the following lines to your ~/.bashrc or ~/.zshrc file:

bash
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

Then, source the file to apply the changes:

bash
source ~/.bashrc

Verify the installation:

bash
nvm -v

You can now install and use different versions of Node.js using nvm. For example, to install the latest LTS version of Node.js, run:

bash
nvm install --lts

Conclusion

You have successfully installed Node.js, Yarn, and nvm on your Ubuntu Server 22.04. You can now start developing your Node.js applications with the latest tools and package managers.