Home

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

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

Heads 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:

bash
=> 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:

bash
nvm list-remote

You'll get a long list — the most recent versions are at the bottom. Older sample:

bash
       v14.17.6   (Latest LTS: Fermium)        v15.0.0        ...        v16.8.0

Install 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:

bash
nvm install --lts

Or pin to a specific LTS line:

bash
nvm install lts/fermium

Verify:

bash
node -vv14.17.6

3. Install another version

Need a different runtime for another project? Just install it.

bash
nvm install stable
bash
Now using node v16.8.0 (npm v7.21.0)

List what's installed:

bash
nvm list
bash
        v6.17.1       v14.17.6->      v16.8.0default -> lts/fermium (-> v14.17.6)node -> stable (-> v16.8.0) (default)lts/fermium -> v14.17.6

4. Switch between versions

bash
nvm use <version>
bash
$ 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.6

To pin a project to a specific Node version, drop a .nvmrc file in the repo root:

bash
echo "lts/fermium" > .nvmrc

Then nvm use (no argument) inside the project picks that up automatically.

5. Remove a version

bash
nvm uninstall <version>
bash
$ nvm uninstall lts/fermiumUninstalled node v14.17.6

6. Install Yarn

Yarn's a popular alternative to npm. Add the official apt repo and install:

bash
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 yarn

Verify:

bash
yarn -v1.22.5

The 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:

js
corepack 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.