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:
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>
Alternatively, Homebrew users can employ:
<code class="language-bash">brew install fnm</code>
After fnm installation, install Node.js using:
<code class="language-bash">fnm install --lts</code>
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>
(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>
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>
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>
Or with wget
:
<code class="language-bash">brew install fnm</code>
Install the LTS version with:
<code class="language-bash">fnm install --lts</code>
Verify with node -v
.
Updating Node.js with nvm
Update Node.js using:
<code class="language-bash">fnm completions --shell <shell></code>
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>
Add --latest-npm
to simultaneously update npm:
<code class="language-bash">fnm install --lts fnm use --lts</code>
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:
.nvmrc
or .node-version
files.A future guide will cover uninstalling Node.js from Ubuntu.
Footnotes
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!