1. Install NVM
Run the following command to download and install NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
Note: Replace
v0.39.5with the latest version of NVM if a newer version is available. Check the NVM GitHub page for the latest release.
2. Activate NVM
After installation, you need to load NVM into your current shell session. Run:
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"
Alternatively, you can restart your terminal or run:
source ~/.bashrc
3. Verify NVM Installation
Check if NVM is installed correctly by running:
nvm --version
This should display the installed version of NVM.
4. Install Node.js Using NVM
Now you can install any version of Node.js using NVM.
Install the Latest Version of Node.js:
nvm install node
Install a Specific Version of Node.js:
For example, to install Node.js version 18:
nvm install 18
Install the Latest LTS Version:
nvm install --lts
5. Switch Between Node.js Versions
You can switch between installed Node.js versions using:
nvm use 18 # Switch to Node.js version 18
To set a default Node.js version for new terminal sessions:
nvm alias default 18
6. List Installed Node.js Versions
To see all installed Node.js versions:
nvm ls
7. Uninstall a Node.js Version
To uninstall a specific Node.js version:
nvm uninstall 18
8. Update NVM
To update NVM to the latest version, run:
nvm install-latest-nvm
9. Uninstall NVM (Optional)
If you want to remove NVM, delete the NVM directory and remove the NVM-related lines from your shell configuration file (e.g., .bashrc, .zshrc):
rm -rf ~/.nvm
Then, remove the NVM lines from your shell configuration file.