Video Training
If you’re a developer who needs to work on multiple projects, you may encounter situations where different Node.js projects require different Node.js versions. Fortunately, there are several ways to manage multiple versions of Node.js on Ubuntu. In this guide, we’ll cover three methods for installing Node.js, from the default Ubuntu repositories to the NodeSource PPA and NVM (Node Version Manager).
Method 1: Installation Using the Ubuntu Repositories
Using the default Ubuntu repositories is one of the quickest methods for installing Node.js. However, it might not provide the latest version of Node.js, which may be essential for certain projects.
- Update the system’s package index:
sudo apt update
- Install Node.js:
sudo apt install nodejs
- Verify the installation by checking the Node.js version:
node -v
- Install npm, the Node.js package manager:
sudo apt install npm
Pros:
- Simple and straightforward installation process.
Cons:
- May not provide the latest version of Node.js.
Method 2: Installation Using the NodeSource PPA (Version 16.x)
For more control over the Node.js version installed, use the NodeSource Personal Package Archive (PPA). This method lets you specify a particular version of Node.js—in this case, version 16.x.
- Navigate to the home directory:
cd ~
- Download the NodeSource setup script for Node.js 16.x:
curl -sL https://deb.nodesource.com/setup_16.x -o /tmp/nodesource_setup.sh
- Optional: Review the downloaded script for security:
nano /tmp/nodesource_setup.sh
- Run the setup script:
sudo bash /tmp/nodesource_setup.sh
- Install Node.js:
sudo apt install nodejs
- Verify the installation by checking the version:
node -v
You should see version 16.x of Node.js installed.
Pros:
- Allows installation of specific versions.
Cons:
- Requires additional steps and knowledge of command-line operations.
Method 3: Installation Using NVM (Node Version Manager)
NVM is a powerful tool that lets you install and switch between multiple versions of Node.js seamlessly. It’s the best choice if you need to use various Node.js versions for different projects.
- Download and run the NVM installation script:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
- Reload your shell configuration:
source ~/.bashrc
- View the list of available Node.js versions:
nvm list-remote
- Install a specific version of Node.js (e.g., v4.10.0):
nvm install v4.10.0
- Switch to another version of Node.js (e.g., v20.10.0):
nvm use v20.10.0
Pros:
- Perfect for managing multiple versions.
- Simple to switch between versions as needed.
Cons:
- Installation of NVM is required before use.
Conclusion
Whether you’re setting up a development environment or managing multiple Node.js projects, knowing how to install and manage different versions of Node.js on Ubuntu is essential. While the Ubuntu repositories and NodeSource PPA offer quick solutions, NVM provides a flexible way to switch between versions seamlessly.