How to Install Yarn on Ubuntu (26.04, 24.04, 22.04)

Yarn is a fast, reliable JavaScript package manager developed by Facebook (now Meta) that serves as an alternative to npm for managing project dependencies. Yarn provides deterministic installs through lockfiles, parallel package downloads, and offline caching, making it a popular choice for Node.js development workflows. The project has two active lines: Yarn Classic (1.x), which is frozen and receives only security fixes, and Yarn Modern (4.x, also called Berry), which is under active development with features like Plug’n’Play installs and per-project version management.

This guide demonstrates how to install Yarn on Ubuntu with three methods: Corepack for Yarn 4 (recommended), npm for Yarn Classic, and the default Ubuntu repository. By the end, you will have Yarn installed, verified, and ready for JavaScript development.

Install Yarn on Ubuntu

Ubuntu provides multiple paths for installing Yarn, each delivering a different version and update experience:

MethodChannelVersionUpdatesBest For
Corepack (Recommended)yarnpkg.com4.x (Modern)Per-project via corepack useNew projects, active development, Plug’n’Play support
npm Global Installnpm registry1.22.x (Classic)Manual via npm update -g yarnLegacy projects that depend on Yarn Classic
Ubuntu RepositoryUbuntu Universe4.1.x on 26.04, 1.22.x on 24.04/22.04System updates via apt upgradeQuick setup without npm; version varies by release

For most users, Corepack is the recommended method because it installs the actively maintained Yarn 4 release and enables per-project version pinning through the packageManager field in package.json. The Ubuntu repository is an alternative on 26.04 (which ships Yarn 4), while npm is the preferred path when your project specifically requires Yarn Classic 1.x.

These steps cover Ubuntu 26.04 LTS, 24.04 LTS, and 22.04 LTS. All three methods require Node.js as a prerequisite. Ubuntu 26.04 ships Node.js 22.x, which is current and works with all methods. Older releases include Node.js 18.x (24.04) and 12.x (22.04), so installing a current release from NodeSource or NVM is recommended on those versions.

Default Yarn and Node.js Versions by Ubuntu Release

Ubuntu ships the Yarn package manager as yarnpkg in its Universe repository. Starting with Ubuntu 26.04, the repository provides Yarn 4 (Modern), while 24.04 and 22.04 still carry Yarn Classic 1.x. The default Node.js versions included with each release also affect which installation methods are available without additional setup:

Ubuntu ReleaseDefault yarnpkgDefault Node.jsNotes
Ubuntu 26.04 LTS4.1.x22.xCorepack ships with Node.js; yarnpkg provides Yarn 4 (Modern)
Ubuntu 24.04 LTS1.22.x18.19.xCorepack works with the default Node.js; the yarnpkg binary avoids conflict with cmdtest
Ubuntu 22.04 LTS1.22.x12.22.xNode.js 12 is too old for modern Corepack; upgrade Node.js first or use npm method

Update Ubuntu Before Yarn Installation

Refresh your package index and apply any pending updates before installing new software:

sudo apt update && sudo apt upgrade

If you are not set up with a sudo user, visit our guide on how to add a user to sudoers on Ubuntu.

Method 1: Install Yarn via Corepack (Recommended)

Corepack is a Node.js tool that manages package manager versions on a per-project basis. It is the officially recommended way to install Yarn and gives you access to Yarn 4 (Modern/Berry), the actively developed release line.

Install Node.js Prerequisites

Corepack requires Node.js and npm. Install them from Ubuntu’s repositories:

sudo apt install nodejs npm

Verify the installed versions:

node --version && npm --version

Expected output on Ubuntu 26.04:

v22.22.0
9.2.0

Expected output on Ubuntu 24.04:

v18.19.1
9.2.0

Ubuntu 26.04 ships Node.js 22.x with Corepack pre-installed, so npm install -g corepack is optional on that release. Ubuntu 22.04 ships Node.js 12.22.x, which is too old for the current Corepack release. If you are on 22.04, install a newer Node.js version from NodeSource or NVM before continuing with this method. Ubuntu 24.04’s Node.js 18 works with Corepack, though a newer LTS release (20 or 22) is still recommended for production use.

Install and Enable Corepack

Install Corepack globally through npm, then enable it to create the yarn shim on your system:

sudo npm install -g corepack
corepack enable

Verify Corepack is installed:

corepack --version
0.34.6

At this point, running yarn --version outside a project directory returns the default Classic version (1.22.22) that Corepack provides as a fallback. The next step sets up Yarn 4 for your project.

Initialize a Yarn 4 Project

Create a new project directory and use corepack use to pin Yarn 4 (the current stable release) to the project:

mkdir ~/my-project && cd ~/my-project
corepack use yarn@stable
Installing yarn@4.12.0 in the project...

➤ YN0000: · Yarn 4.12.0
➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: · Done in 0s 43ms

This creates a package.json with a packageManager field that locks Yarn 4 to the project. Any collaborator who clones the repository and has Corepack enabled will automatically use the same Yarn version.

Verify Yarn Installation

Confirm Yarn 4 is active inside the project:

yarn --version
4.12.0

You can also verify the project’s package.json contains the version pin:

cat package.json
{
  "packageManager": "yarn@4.12.0"
}

The version numbers above (4.12.0) reflect the current stable release at the time of writing. Corepack automatically fetches the latest stable version when you run corepack use yarn@stable, so your output may show a newer version.

Method 2: Install Yarn Classic via npm

If your project depends on Yarn Classic (1.x), the simplest approach is installing it globally through npm. This method works on Ubuntu 26.04, 24.04, and 22.04 with the default system Node.js.

Install Node.js and npm

Install Node.js and npm from Ubuntu’s repositories if they are not already present:

sudo apt install nodejs npm

Install Yarn Classic Globally

Use npm to install Yarn Classic as a global package:

sudo npm install -g yarn

Verify Yarn Classic Installation

Confirm the installed version:

yarn --version
1.22.22

Yarn Classic (1.x) entered maintenance mode in January 2020 and only receives occasional security fixes. The latest Classic release is 1.22.22. For new projects, consider migrating to Yarn 4 via Method 1 to access active development, Plug’n’Play support, and improved workspace features.

Method 3: Install Yarn from Ubuntu Repository

Ubuntu packages the Yarn package manager in its Universe repository under the name yarnpkg (not yarn) to avoid a naming conflict with an unrelated package called cmdtest. On Ubuntu 26.04, this installs Yarn 4 (Modern). On 24.04 and 22.04, it installs Yarn Classic (1.x).

Install the yarnpkg Package

Install Yarn directly from Ubuntu’s Universe repository:

sudo apt install yarnpkg

Verify Yarn Installation

The APT package installs the binary as yarnpkg, not yarn:

yarnpkg --version

Expected output on Ubuntu 26.04:

4.1.0

Expected output on Ubuntu 24.04:

1.22.19

Expected output on Ubuntu 22.04:

1.22.10

If you want the standard yarn command name, create a symbolic link:

sudo ln -s /usr/bin/yarnpkg /usr/local/bin/yarn

Ubuntu 26.04 ships Yarn 4.1.x via the yarnpkg package, matching the Modern release line. On 24.04 and 22.04, the repository versions (1.22.19 and 1.22.10 respectively) lag behind the latest upstream Classic release (1.22.22). If you need the most current Classic release on those versions, use Method 2 instead.

Basic Yarn Usage Commands

The commands below apply to both Yarn 4 (Corepack) and Yarn Classic, with differences noted where applicable.

Initialize a New Project

yarn init

In Yarn 4, this creates a minimal package.json. In Yarn Classic, it walks you through an interactive prompt to fill in project details.

Add a Package

yarn add lodash

Yarn 4 output:

➤ YN0000: · Yarn 4.12.0
➤ YN0000: ┌ Resolution step
➤ YN0085: │ + lodash@npm:4.17.21
➤ YN0000: └ Completed
➤ YN0000: ┌ Fetch step
➤ YN0013: │ A package was added to the project (+ 1.48 MiB).
➤ YN0000: └ Completed in 0s 214ms
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: · Done in 0s 262ms

Add a Development Dependency

The --dev flag saves the package to devDependencies in package.json, keeping it out of production builds:

yarn add --dev eslint

Install All Dependencies

When you clone a project that already has a package.json and yarn.lock, install all dependencies with:

yarn install

Run a Package Script

Execute scripts defined in your package.json:

yarn run build

Remove a Package

Remove a package and update the lockfile in one step:

yarn remove lodash

Update Yarn to a Newer Version

Yarn 4 (Corepack): update the pinned version inside your project directory:

corepack use yarn@stable
yarn install

Yarn Classic (npm):

sudo npm update -g yarn

Yarn (APT):

sudo apt install --only-upgrade yarnpkg

Configure Yarn 4 Node Modules Strategy

Yarn 4 defaults to Plug’n’Play (PnP), which replaces the traditional node_modules directory with a more efficient resolution mechanism. Some tools and libraries do not yet support PnP. If you encounter compatibility issues, switch to the traditional node_modules strategy by creating a .yarnrc.yml file in your project root:

nodeLinker: node-modules

Then reinstall dependencies to apply the change:

yarn install

The nodeLinker: node-modules setting makes Yarn 4 behave like Yarn Classic with respect to dependency layout, while still providing Yarn 4’s improved resolution, audit, and workspace features. This is the easiest migration path for projects that are not yet PnP-compatible.

Troubleshoot Yarn Installation Issues

apt install yarn Installs the Wrong Package

Running sudo apt install yarn on Ubuntu does not install the Yarn package manager. Ubuntu maps the yarn package name to cmdtest, a completely unrelated blackbox testing tool.

If you accidentally installed cmdtest, remove it:

sudo apt remove cmdtest
sudo apt autoremove

Then install Yarn using one of the three methods documented in this guide.

Corepack Command Not Found

If corepack: command not found appears after installing via npm, the npm global bin directory may not be in your PATH. Check where npm installs global packages and add it:

npm config get prefix

The default on Ubuntu is /usr/local, which means global binaries go to /usr/local/bin (already in PATH). If your output shows a different prefix, add it:

echo 'export PATH="$(npm config get prefix)/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Yarn Shows Wrong Version in a Project

If yarn --version shows 1.22.22 inside a project where you expect Yarn 4, Corepack may not be enabled. Enable it and verify:

corepack enable
yarn --version

If the project does not have a packageManager field in package.json, Corepack falls back to Yarn Classic. Pin Yarn 4 to the project:

corepack use yarn@stable

Permission Errors During Global npm Install

If you see EACCES permission errors when running npm install -g, prefix the command with sudo:

sudo npm install -g corepack

Alternatively, reconfigure npm’s global prefix to a user-owned directory to avoid needing sudo for global installs:

mkdir -p ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH="$HOME/.npm-global/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Remove Yarn from Ubuntu

Remove Corepack Yarn Installation

Disable Corepack and remove it:

corepack disable
sudo npm uninstall -g corepack

On Ubuntu 26.04, Corepack ships as the system package node-corepack. If you did not install Corepack via npm, remove it with sudo apt remove node-corepack instead.

Corepack stores downloaded package manager versions in its cache. Remove it to reclaim disk space:

rm -rf ~/.cache/node/corepack

Verify removal:

corepack --version
bash: corepack: command not found

Remove npm Global Yarn Installation

Uninstall the globally installed Yarn Classic package:

sudo npm uninstall -g yarn

Verify removal:

yarn --version
bash: yarn: command not found

Remove APT Yarn Installation

sudo apt remove yarnpkg
sudo apt autoremove

If you created the yarn symlink earlier, remove it as well:

sudo rm -f /usr/local/bin/yarn

Verify removal:

yarnpkg --version
bash: yarnpkg: command not found

Clean Up Yarn Cache and Configuration

The following commands permanently delete Yarn’s global cache and configuration files. This does not affect project-level node_modules or yarn.lock files, but any globally cached packages will need to be re-downloaded.

rm -rf ~/.yarn ~/.yarnrc ~/.yarnrc.yml

Frequently Asked Questions

What is the difference between Yarn Classic and Yarn Modern (Berry)?

Yarn Classic (1.x) is the original release that entered maintenance mode in January 2020 and only receives security fixes. Yarn Modern (4.x, also called Berry) is the actively developed version with features like Plug’n’Play installs, per-project version pinning via Corepack, a plugin system, and advanced workspace support. New projects should use Yarn 4 unless they have a specific dependency on Classic.

Why does apt install yarn install cmdtest instead of Yarn?

On Ubuntu, the yarn package name is mapped to cmdtest, an unrelated blackbox testing tool. The Yarn package manager is available as yarnpkg in Ubuntu’s repositories. To avoid confusion, use Corepack or npm to install Yarn instead of apt.

How does Yarn compare to npm and pnpm?

All three are JavaScript package managers. npm ships with Node.js and has the largest ecosystem. Yarn 4 offers Plug’n’Play for faster installs and zero-install workflows. pnpm uses a content-addressable store to deduplicate packages across projects, saving disk space. The choice depends on your workflow: npm for simplicity, Yarn for deterministic per-project versioning, and pnpm for disk efficiency across many projects.

What does yarn install --ignore-engines do?

The --ignore-engines flag tells Yarn Classic to skip engine compatibility checks defined in package.json (such as minimum Node.js version requirements). This is useful when you need to install packages on a Node.js version that does not match the declared engine constraints, though it may lead to runtime errors if the package truly requires a newer Node.js version.

Conclusion

You now have Yarn installed on Ubuntu using one of three methods: Corepack for the current Yarn 4 release, npm for Yarn Classic, or Ubuntu’s built-in yarnpkg package. Corepack provides the best experience for new projects by pinning the Yarn version to each repository, ensuring consistent builds across development machines. For projects that pair Yarn with version control, consider setting up Git on Ubuntu as a natural next step. If you are working with TypeScript, Yarn 4’s improved resolution and workspace features integrate well with typed JavaScript projects.

Leave a Comment

Let us know you are human: