Home > Web Front-end > JS Tutorial > How to Install Node.js on Ubuntu and Linux Distros: A Complete Guide

How to Install Node.js on Ubuntu and Linux Distros: A Complete Guide

Mary-Kate Olsen
Release: 2025-01-21 18:32:12
Original
483 people have browsed it

How to Install Node.js on Ubuntu and Linux Distros: A Complete Guide

This guide provides a comprehensive walkthrough for installing Node.js on Ubuntu (and other Linux distributions). While a direct download from the official website is possible, utilizing a version manager offers superior flexibility and control over your development environment. We'll demonstrate Node.js installation using two popular version managers: fnm and nvm.

Understanding Node.js Version Managers

Node.js version managers simplify the process of installing and switching between different Node.js versions. They're especially beneficial for:

  • Managing multiple projects with varying Node.js version requirements.
  • Facilitating cross-version code testing.
  • Ensuring safe and straightforward Node.js upgrades.

While perhaps less critical for production, these managers streamline upgrades without disrupting running applications. We'll explore two recommended options: fnm (Fast Node Manager) and nvm (Node Version Manager).

Installing Node.js with fnm

fnm (Fast Node Manager) is a modern, Rust-based alternative to nvm and n, offering cross-platform compatibility and superior speed due to native code compilation. You must install fnm before installing Node.js.

Install fnm using:

<code class="language-bash">curl -fsSL https://fnm.vercel.app/install | bash</code>
Copy after login
Copy after login

Alternatively, Homebrew users can employ:

<code class="language-bash">brew install fnm</code>
Copy after login
Copy after login

After fnm installation, install Node.js using:

<code class="language-bash">fnm install --lts</code>
Copy after login
Copy after login

This installs the latest Long Term Support (LTS) version. For a specific version, replace --lts with the version number (e.g., fnm install 23). Verify the installation with node -v.

fnm Shell Completions and Configuration

fnm's shell completions are included with the binary. Enable them with:

<code class="language-bash">fnm completions --shell <shell></code>
Copy after login
Copy after login

(Replace <shell> with your shell—e.g., bash, zsh, fish.)

Many fnm features are initially disabled to avoid altering default shell behavior. Configure them by adding flags to the fnm env call during shell initialization. For example:

<code class="language-bash">eval "$(fnm env --use-on-cd)"</code>
Copy after login
Copy after login

The --use-on-cd flag integrates with your shell, automatically switching Node.js versions upon directory changes based on .node-version, .nvmrc, or packages.json#engines#node (if --resolve-engines is enabled).

Updating Node.js with fnm

Regular Node.js updates are crucial for security and new features. For new LTS versions, use:

<code class="language-bash">fnm install --lts
fnm use --lts</code>
Copy after login
Copy after login

A shortcut is fnm use --install-if-missing [VERSION]. Use fnm list-remote to find the latest version number.

Installing Node.js with nvm

Node Version Manager (nvm) is a long-standing, command-line tool for managing multiple Node.js versions. Written in POSIX-compliant shell script, it provides comparable speed to fnm. Like fnm, it requires installation before Node.js.

Install nvm using:

<code class="language-bash">curl -fsSL https://fnm.vercel.app/install | bash</code>
Copy after login
Copy after login

Or with wget:

<code class="language-bash">brew install fnm</code>
Copy after login
Copy after login

Install the LTS version with:

<code class="language-bash">fnm install --lts</code>
Copy after login
Copy after login

Verify with node -v.

Updating Node.js with nvm

Update Node.js using:

<code class="language-bash">fnm completions --shell <shell></code>
Copy after login
Copy after login

This installs and switches to the latest LTS version. Replace --lts with a specific version number (e.g., nvm install 23).

To update the LTS version and migrate existing packages, use:

<code class="language-bash">eval "$(fnm env --use-on-cd)"</code>
Copy after login
Copy after login

Add --latest-npm to simultaneously update npm:

<code class="language-bash">fnm install --lts
fnm use --lts</code>
Copy after login
Copy after login

Best Practices and Tips

Specify the Node.js version in your project's package.json or use a .nvmrc file to ensure consistent versions across your team. Both fnm and nvm support these files.

If you configured fnm with --use-on-cd, it automatically switches versions when changing directories with .node-version or .nvmrc. With nvm, use nvm use.

Conclusion and Next Steps

You've successfully installed Node.js using either fnm or nvm. fnm provides better performance, while nvm boasts wider community support.

Remember to:

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

A future guide will cover uninstalling Node.js from Ubuntu.

Footnotes

  1. fnm
  2. nvm
  3. n is a Node.js version manager built on BASH scripts, offering simplicity and cross-platform support (Linux, macOS, and Windows via WSL).
  4. fnm configuration

The above is the detailed content of How to Install Node.js on Ubuntu and Linux Distros: A Complete Guide. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template