This is the hero image for this article

How to Install Node.js on Ubuntu and Linux Distros


Installing Node.js on Ubuntu (or any Linux distribution) is a crucial first step for JavaScript developers. While you could download Node.js directly from the official website, using a version manager provides more flexibility and control over your development environment. This guide shows you how to install Node.js on Ubuntu using two popular version managers: fnm and nvm.

Understanding Node.js Version Managers

Node.js version managers help you install and switch between different Node.js versions easily. They’re particularly useful when:

  • Working on multiple projects that requires different Node.js versions.
  • Testing your code across Node.js versions.
  • Upgrading to newer Node.js versions safely

These version managers may seem unnecessary for production environments, but they make it simple and safe to upgrade your Node.js version without interrupting running applications.

Let’s explore two officially recommended options: fnm (Fast Node Manager) and nvm (Node Version Manager).

Install Node.js with fnm

Fast Node Manager1 (fnm) is a modern alternative to nvm2 and n3 that’s written in Rust. It is cross-platform and particularly fast because it’s compiled to native code. You need to install fnm before installing Node.js.

Install fnm using this command:

curl -fsSL https://fnm.vercel.app/install | bash

If you use Homebrew then you can install fnm using the command brew install fnm.

Now you can install Node.js using fnm and the following command:

fnm install --lts

This command installs the latest LTS version of Node.js. You can also install a specific version by replacing --lts with the version number. For example, fnm install 23 installs Node.js version 23.

Now run node -v to verify that Node.js is installed and running.

fnm Shell Completions and Configuration

fnm ships its completions with the binary. You can enable them by running the following command and follow the instructions after that:

fnm completions --shell <SHELL>

Replace <SHELL> with the name of your shell (e.g., bash, zsh, fish).

fnm comes with many features out of the box. Some of them are not activated by default because they change your shell’s default behavior with regards to fnm. You can configure them by adding flags to the fnm env call when initializing the shell.

For example:

eval "$(fnm env --use-on-cd)"

The --use-on-cd flag appends output to fnm env’s output, which will hook into your shell upon changing directories, and will switch the Node.js version based on .node-version or .nvmrc (or packages.json#engines#node if --resolve-engines was enabled).

Update Node.js Version Using fnm

Keeping Node.js updated is essential for security patches and new features. If a new LTS version is released, you can update your Node.js version using fnm install --lts and fnm use to switch to the new version. A shortcut for that is fnm use --install-if-missing [VERSION].

You can get the latest version number using the command fnm list-remote.

Install Node.js with nvm

Node Version Manager2 (nvm) allows you to quickly install and use different versions of node via the command line, and has been a popular Node.js version manager for years. It’s written in shell script as a POSIX-compliant function and it’s equally fast.

Just like fnm, nvm is also cross-platform and you have to install it before installing Node.js. Install nvm using this command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash

Or using wget:

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash

Now install Node.js LTS version using the following command:

nvm install --lts

Run node -v to verify that Node.js is installed and running.

Update Node.js Version Using nvm

You can use the command below to update Node.js using nvm:

nvm install --lts
nvm use --lts

This installs and switches to the latest LTS version of Node.js. You can specify a specific version by replacing --lts with the version number. For example, nvm install 23 installs Node.js version 23.

To get the latest LTS version of node and migrate your existing installed packages, use the following command:

nvm install --reinstall-packages-from=current 'lts/*'

Add the --latest-npm flag to update npm at the same time.

nvm install --reinstall-packages-from=current --latest-npm 'lts/*'

Best Practices and Tips

Specify a Node.js version in your project’s package.json or use a .nvmrc file, to ensure that everyone on your team uses the same version. Both fnm and nvm support these files.

If you configured your shell to use --use-on-cd 4 for fnm, it’ll automatically switch Node.js versions when you cd to a directory with a .node-version or .nvmrc file. For nvm, you’ll have to use the nvm use command to switch Node.js versions.

Conclusion and Next Steps

You’ve successfully installed Node.js on your Linux system using either fnm or nvm. Both tools provide robust version management capabilities, with fnm offering better performance and nvm providing broader community support.

Remember to:

  • Regularly update your Node.js installation
  • Use project-specific .nvmrc or .node-version files
  • Keep your version manager updated

Need to remove Node.js or switch version managers? Watch out for our upcoming guide on properly uninstalling Node.js from Ubuntu systems.

Footnotes

Footnotes

  1. fnm

  2. nvm 2

  3. n is a Node.js version manager that’s built on BASH scripts. There are no subshells, no profile setup, no convoluted API, and equally simple. It works on Linux, macOS, and Windows only if you use WSL.

  4. fnm configuration