Node.js
is a powerful JavaScript runtime that allows you to build scalable and performant applications. npm
, the Node Package Manager, is used for managing and sharing Node.js
packages. One of the convenient ways to install and manage Node.js
versions is by using nvm
(Node Version Manager). In this guide, we’ll walk through the steps to install Node.js
and npm
on Ubuntu 22.04 using nvm
.
Table of Contents
- 1. Introduction
- 2. Installing
nvm
- 3. Installing
Node.js
andnpm
withnvm
- 4. Verifying the Installation
- 5. Switching
Node.js
Versions - 6. Conclusion
1. Introduction
Node.js
is a JavaScript runtime that allows you to execute JavaScript code server-side. npm
, the Node Package Manager, is used for managing and distributing JavaScript packages. Installing Node.js
and npm
can be done in various ways, and nvm
(Node Version Manager) is a bash script that allows you to manage multiple Node.js
versions on a per-user basis.
2. Installing nvm
First, let’s install nvm
. Open your terminal and run the following commands:
# Download the nvm installation script
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
# Load nvm into the current shell session
source ~/.bashrc
The script will clone the project’s repository from GitHub to the ~/.nvm
directory.
Replace ~/.bashrc
with ~/.zshrc
if you are using the Zsh shell.
3. Installing Node.js
and npm
with nvm
Now that nvm
is installed, you can use it to install Node.js
and npm
. Run the following commands:
# Install the latest LTS version of Node.js
nvm install --lts
# Set the installed version as the default version
nvm alias default $(nvm current)
This installs the latest LTS (Long-Term Support) version of Node.js
. You can install specific versions by replacing —lts with a Node.js
version, such as 14.17.5.
4. Verifying the Installation
To verify that Node.js
and npm
are installed correctly, run the following commands:
# Check Node.js version
node --version
# Check npm version
npm --version
These commands should display the installed Node.js
and npm
versions.
5. Switching Node.js
Versions
One of the advantages of using nvm is the ability to switch between different Node.js
versions. For example, to switch to a different version, you can use the following command:
# Switch to a different `Node.js` version
nvm use 12.22.6
Replace 12.22.6
with the version you want to switch to.
6. Conclusion
You’ve successfully installed Node.js
and npm on Ubuntu 22.04 using nvm
. This setup allows you to manage multiple Node.js
versions effortlessly and switch between them based on your project requirements. Take advantage of nvm’s flexibility to work with various Node.js
projects seamlessly.