Install NVM on Ubuntu
NVM (Node Version Manager) installs and manages multiple Node.js versions per user. The apt nodejs package installs system-wide, is often outdated, and makes version switching difficult. NVM avoids both problems and is the standard for modern JS development.
Install NVM
Section titled “Install NVM”The install script clones the NVM repo into ~/.nvm and adds initialization lines to ~/.bashrc or ~/.zshrc. Check the nvm-sh/nvm repo for the current release and replace v0.40.4 below if a newer version is out.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bashActivate NVM
Section titled “Activate NVM”Either restart the terminal, or reload the shell config in the current session.
source ~/.bashrcVerify the install.
nvm --versionInstall Node.js
Section titled “Install Node.js”Install the current LTS, set it as the active version, and pin it as the default for new shells.
nvm install --ltsnvm use --ltsnvm alias default lts/*Confirm node and npm are available.
node -vnpm -vManage versions
Section titled “Manage versions”nvm install 20 # install a specific major versionnvm use 20 # switch the current shell to that versionnvm alias default 20 # set the default for new shellsnvm ls # list installed versionsnvm uninstall 18 # remove a versionInspect before piping (optional)
Section titled “Inspect before piping (optional)”Piping a remote script straight into bash should only be done for trusted sources. To inspect the script before running it, download first.
curl -O https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.shless install.shbash install.sh