Installing Node.js and Yarn on Ubuntu with nvm
Node.js is the JavaScript runtime built on Chrome's V8 engine. It's the de facto standard for JS on the server side. This post walks through installing Node on Ubuntu 20.04 / Debian 10 using nvm (Node Version Manager) so you can install multiple Node versions side-by-side and switch between them as needed. Then we'll add Yarn — the package manager — via apt.
1. Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bashHeads up: this version (
v0.38.0) is from 2021. For a fresh install, check the latest tag at nvm-sh/nvm — the install command on the README always points at the current release.
Sample output:
=> Downloading nvm from git to '/root/.nvm'=> Cloning into '/root/.nvm'...=> Compressing and cleaning up git repository=> Appending nvm source string to /root/.bashrc=> Appending bash_completion source string to /root/.bashrc=> Close and reopen your terminal to start using nvm or run the following to use it now:export NVM_DIR="$HOME/.nvm"[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"Either source ~/.bashrc or open a new shell so nvm is on your PATH.
2. Install Node.js
List the available versions:
nvm list-remoteYou'll get a long list — the most recent versions are at the bottom. Older sample:
v14.17.6 (Latest LTS: Fermium) v15.0.0 ... v16.8.0Install a specific version by number, or use one of the friendly aliases (lts/*, stable, etc.). Picking the active LTS is the safe default for most apps:
nvm install --ltsOr pin to a specific LTS line:
nvm install lts/fermiumVerify:
node -vv14.17.63. Install another version
Need a different runtime for another project? Just install it.
nvm install stableNow using node v16.8.0 (npm v7.21.0)List what's installed:
nvm list v6.17.1 v14.17.6-> v16.8.0default -> lts/fermium (-> v14.17.6)node -> stable (-> v16.8.0) (default)lts/fermium -> v14.17.64. Switch between versions
nvm use <version>$ nvm use stableNow using node v16.8.0 (npm v7.21.0)$ node -vv16.8.0$ nvm use lts/fermiumNow using node v14.17.6 (npm v6.14.15)$ node -vv14.17.6To pin a project to a specific Node version, drop a .nvmrc file in the repo root:
echo "lts/fermium" > .nvmrcThen nvm use (no argument) inside the project picks that up automatically.
5. Remove a version
nvm uninstall <version>$ nvm uninstall lts/fermiumUninstalled node v14.17.66. Install Yarn
Yarn's a popular alternative to npm. Add the official apt repo and install:
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /usr/share/keyrings/yarnkey.gpg >/dev/nullecho "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | tee /etc/apt/sources.list.d/yarn.listapt-get update && apt-get install -y yarnVerify:
yarn -v1.22.5The apt-installed Yarn is the classic v1 line. For modern projects you may want Yarn Berry (v2/3/4) — that's installed per-project via Corepack instead:
jscorepack enablecorepack prepare yarn@stable --activate
That's it — Node and Yarn are ready to go. Switch between Node versions with nvm use and you're set up to work on multiple projects with different runtime requirements.