If you are setting up a development environment on an Ubuntu server, your first instinct is probably to run sudo apt install nodejs. However, when it comes to Node.js, using the default APT package manager is almost always a terrible idea.
Ubuntu’s official software repositories prioritize extreme stability over cutting-edge features. This means the version of Node.js available via APT is usually years out of date. Furthermore, installing Node via APT installs it at the system level, which frequently leads to frustrating “EACCES” permissions errors when trying to install global NPM packages.
The industry standard solution is to use NVM (Node Version Manager). NVM allows you to install the latest version of Node.js directly into your user’s home directory, completely bypassing permission issues and allowing you to swap between multiple Node versions on the fly.
Here is how to install Node.js the right way using NVM.
Step 1: Install NVM
First, you need to download and run the NVM installation script directly from their official GitHub repository. We will use the curl command to pull the script.
- Open your terminal and run the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.6/install.sh | bash - The script will clone the NVM repository to
~/.nvmand automatically add the necessary source lines to your bash profile (like~/.bashrcor~/.zshrc). - Crucial Step: The terminal will not recognize the
nvmcommand immediately. You must either close your terminal and open a new one, or force your current session to reload its configuration by running:source ~/.bashrc
Step 2: Install Node.js LTS
Now that NVM is installed, you can use it to pull down any version of Node you want. For production servers and general development, you should always install the LTS (Long Term Support) version. LTS versions are guaranteed to receive security updates and are the most stable.
Run the following command:
nvm install --lts
NVM will reach out to the Node.js servers, download the binaries for the latest LTS release, and set it as your default environment.
Step 3: Verify the Installation
To confirm that both Node.js and its companion package manager (NPM) are installed and ready to use, check their versions.
- Check the Node version:
node -v - Check the NPM version:
npm -v
If both commands return a version number (like v22.x.x), your installation was successful. Because NVM installs everything inside your home directory, you can now run commands like npm install -g create-react-app without ever needing to type sudo.