When deploying Node.js applications on a CentOS 8 server, different projects require different versions of Node.js. Installing directly using the system package manager only yields a single global version, making switching between them cumbersome. NVM allows you to easily install, switch, and manage multiple Node.js versions on a single server. Below is the complete process for installing a specific version of Node.js on CentOS 8 using NVM.
First, some preparation is required. Since CentOS 8 is no longer maintained, you need to ensure that the system's basic software repositories are working properly. Next, connect to your CentOS 8 server via SSH, preferably using an account with sudo privileges. Before starting the installation, it is recommended to update any existing packages on your system. Although the main update repository for CentOS 8 is closed, some basic updates are still available. Run the following commands to complete the preparation:
# Switch to root user or use sudo
sudo -i
# Update existing packages (you may encounter warnings that some repositories are unavailable, this is normal)
dnf update -y
# Install basic dependencies that may be needed to compile Node.js
dnf install -y curl wget git gcc-c++ make
These dependencies are important: `curl` is used to download the installation script, `gcc-c++` and `make` are required for compiling some native Node.js modules. After completing the preparation, you can begin installing NVM. NVM stands for "Node Version Manager," which isolates and switches between versions of Node.js by installing different versions in your user directory.
The safest way to install NVM is through the official installation script. Do not download from unofficial sources to avoid security risks. Use curl to download and run the script:
# Install NVM using the official script
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
This command will download the installation script from GitHub and execute it automatically. After installation, the script will prompt you to reload your shell configuration or log in again. This is because the installation script adds the source code for initializing NVM to your shell configuration file (`~/.bashrc`). To make NVM take effect immediately in the current session, you can run:
# Reload the bash configuration to make the nvm command take effect immediately
source ~/.bashrc
# Verify that nvm is installed successfully
nvm --version
# If successful, the version number will be displayed, for example: 0.39.0
If you see the version number output, NVM has been installed successfully. Sometimes you may encounter the error `nvm: command not found`, which is usually because the shell configuration file is not loaded correctly. You can try completely exiting the SSH session and logging back in, or manually executing `source ~/.bash_profile` or `source ~/.zshrc` (if you're using zsh).
Next is the crucial step: installing a specific version of Node.js using NVM. First, you can view all installable Node.js versions:
# View all installable remote versions (the list is long)
nvm ls-remote
# If you only need to view LTS (Long Term Support) versions
nvm ls-remote --lts
Assuming you need to install a specific version of Node.js, 16.14.0, the installation command is very simple:
# Install a specific version of Node.js
nvm install 16.14.0
During the installation process, NVM will download the specified version of the Node.js source code, compile it, and install it to the `~/.nvm/versions/node/` directory. After installation, this version will be automatically set as the "active version" for the current shell session. You can verify the following commands:
# Verify the currently active Node.js version
node --version # Should display: v16.14.0
npm --version # Display the corresponding npm version
# View all installed Node.js versions
nvm ls
The `nvm ls` command lists all installed versions, with an arrow next to the currently active version. If you need to install other versions, such as the latest 18.x LTS version, simply repeat the installation command:
# Install another Node.js 18 LTS version
nvm install 18 --lts
Now you have multiple Node.js versions installed on your server. How do you switch between them? NVM provides very simple switching commands:
# Switch to Node.js 16.14.0
nvm use 16.14.0
# Switch to Node.js 18.x
nvm use 18
# Set the default version (new shell sessions will automatically use this version)
nvm alias default 18
The `nvm alias default` command is particularly useful; it sets a default version so that it's automatically used every time a new terminal session is opened, instead of requiring manual switching each time.
When using NVM, each Node.js version has its own independent global npm package space. This means that when you switch Node.js versions, previously installed global npm packages may become unavailable. If you need to share certain global tools across multiple versions, consider using the `nvm reinstall-packages` command:
# Copy global packages from one version to another
nvm install 20 --reinstall-packages-from=18
Sometimes you might encounter installation failures. Common problems and solutions include:
1. Network issues causing download failures: Try setting up an npm mirror source before installation.
# Temporarily use the Taobao mirror source for installation
NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node nvm install 16.14.0
2. Compilation failures: Usually due to missing compilation dependencies.
# Install more development tools
dnf groupinstall -y "Development Tools"
dnf install -y kernel-headers kernel-devel
3. Permission issues: Ensure you are installing in your own user directory, not running the nvm command as root.
To make Node.js applications run more stably in production environments, here are a few more suggestions. For production servers, it is recommended to use LTS versions because they have longer support periods and better stability. You can create different system users for different applications on the server, each with their own NVM environment, which better isolates the applications. Consider using a process manager like PM2 to manage your Node.js applications. It offers features such as fault tolerance, logging, and monitoring:
# Install PM2 globally (within the selected Node.js version)
npm install -g pm2
# Start your application using PM2
pm2 start app.js
Finally, regular maintenance is also important. You can periodically check the installed versions and uninstall those you no longer need to save disk space:
# Uninstall a specific version
nvm uninstall 14.17.0
# Check the nvm cache and clean up downloaded source code packages
nvm cache dir
By following these steps, you should be able to manage multiple Node.js versions smoothly using NVM on your CentOS 8 server. The biggest advantage of this method is its flexibility and isolation—each project can use the Node.js version best suited to it without affecting other projects. Although CentOS 8 is no longer maintained, installing Node.js via NVM remains a reliable option, especially when you need to maintain compatibility with older projects.