To install a specific version of Node.js on Ubuntu 24.04, you can use the NodeSource binary distributions. Here are the steps to do so:
Update your package index:
sudo apt update
Install the required dependencies:
sudo apt install curl software-properties-common
Add the NodeSource repository:
- To install a specific version, such as Node.js 16, use:
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
- To install a specific version, such as Node.js 16, use:
Install Node.js:
sudo apt install nodejs
- Verify the installation:
node -v
npm -v
These steps will install the specified version of Node.js along with npm. If you need a different version, replace setup_16.x
in the URL with the appropriate version number (e.g., setup_14.x
for Node.js 14).
If you need to manage multiple versions of Node.js, consider using nvm
(Node Version Manager):
Install
nvm
:curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
Activate
nvm
: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"Install a specific Node.js version:
nvm install 16
Set the default Node.js version:
nvm use 16
nvm alias default 16Verify the installation:
node -v
npm -v
Using nvm
is particularly useful if you frequently switch between different Node.js versions.