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:
sudo apt update
sudo apt upgrade -yStep 2: Install Node.js
To install the latest version of Node.js, you can use the NodeSource repository. Run the following commands:
curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
sudo apt install -y nodejsVerify the installation:
node -v
npm -vStep 3: Install Yarn
Yarn is a popular package manager for Node.js. To install Yarn, follow these steps:
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 yarnVerify the installation:
yarn -vStep 4: Install nvm (Node Version Manager)
nvm allows you to manage multiple versions of Node.js. To install nvm, run the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bashActivate nvm by adding the following lines to your ~/.bashrc or ~/.zshrc file:
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 nvmThen, source the file to apply the changes:
source ~/.bashrcVerify the installation:
nvm -vYou can now install and use different versions of Node.js using nvm. For example, to install the latest LTS version of Node.js, run:
nvm install --ltsConclusion
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.