Read more

Installing Node.js / npm under Ubuntu with nvm (with yarn)

Henning Koch
September 10, 2015Software engineer at makandra GmbH

I recommend install Node.js using nvm. This way you can have multiple Node versions in your ~/.nvm. You also won't need to install global packages with sudo anymore.

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show snapshot

Node via nvm will automatically bring npm. yarn will automatically be available if corepack is enabled for node.

Installing nvm

DigitalOcean has a HOWTO for installing nvm on Ubuntu ( 16.04 Show snapshot , 18.04 Show snapshot , 20.04 Show snapshot ) that I recommend.
During the installation of node via nvm, a compatible version of npm will be installed automatically.

Installing yarn

Install yarn for the current node version

It is very easy to install yarn when node is already available. This has to be done separatly for each node version on your system, though.

For recent node versions

Corepack Show snapshot is a new node feature that automatically installs yarn (and pnpm) for you when it is called. It allows you to specify a desired yarn version inside package.json and makes sure the exact version is downloaded and used:

  "packageManager": "yarn@3.3.1"

Corepack comes with Node.js starting from 14.19.0 and 16.9.0 but is disabled by default. To enable it, call:

corepack enable

The yarn command will be available afterwards.


To start a new project (no yarn version pinned in package.json) with a desired yarn version, run
corepack prepare yarn@<version> --activate

Install yarn for node < 14.19.0

For older versions of node that don't come with corepack, you can install yarn 1 manually:

npm install --global yarn

Install yarn 1 system-wide via apt (deprecated)

The yarn package Show snapshot depends on the nodejs debian package, but with nvm you no longer want to have that package installed through apt.

You can install yarn while skipping dependencies like this:

curl -sS | sudo apt-key add -
echo "deb stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install --no-install-recommends yarn

If yarn stops updating, the imported key has expired and you need to re-run the line with apt-key add.

Migrating an apt-installed Node.js to nvm

Uninstall the nodejs package:

sudo apt remove nodejs

Check if you want to remove any custom package sources for Node.js in your /etc/apt/sources.list.d (e.g. nodelist.list).

Now install Node.js with nvm Show snapshot

Configuration in the ~/.bashrc

In case you see an error like:

Yarn requires Node.js 4.0 or higher to be installed.

Check if command -v "node" returns a path and you load NVM before Yarn. This could look like this:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/" ] && \. "$NVM_DIR/"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

# Load global yarn binaries
export PATH="$PATH:$(yarn global bin)"

If you want to switch the Node version automatically with a cd you can use this snippet Show snapshot .

Henning Koch
September 10, 2015Software engineer at makandra GmbH
Posted by Henning Koch to makandra dev (2015-09-10 10:18)