Yarn is still useful when a JavaScript project needs dependency installs that are pinned to the repository instead of whatever package manager happens to be global on the machine. To install Yarn on Ubuntu, the main choice is between Corepack for Yarn 4, npm for Yarn Classic, or Ubuntu’s own yarnpkg package.
Ubuntu 26.04 LTS (Resolute Raccoon), Ubuntu 24.04 LTS (Noble Numbat), and Ubuntu 22.04 LTS (Jammy Jellyfish) all support Yarn, but their default Node.js and yarnpkg packages differ enough to make method choice matter. Yarn does not use a Node.js-style LTS channel: use the stable Yarn 4 line for new work, and keep Yarn Classic 1.x only when an older project expects it.
Install Yarn on Ubuntu
Refresh APT first so Ubuntu sees the latest package metadata before you install Node.js, Corepack, or yarnpkg:
sudo apt update
sudo apt upgrade
These commands use
sudofor root-owned package operations. If your account cannot run sudo yet, follow the Ubuntu guide on how to add a user to sudoers on Ubuntu before continuing.
Choose one installation path based on the Yarn line your project needs:
| Method | Yarn line | Best fit | Main command |
|---|---|---|---|
| Corepack | Yarn 4 stable | New projects and teams that pin package managers per repository | corepack use yarn@stable |
| npm global package | Yarn Classic 1.22.x | Legacy projects that still require Yarn 1.x | sudo npm install -g yarn |
Ubuntu yarnpkg | 4.1.x on 26.04, 1.22.x on 24.04 and 22.04 | APT-managed installs from Ubuntu Universe | sudo apt install yarnpkg |
Pick one Yarn provider unless you deliberately need more than one. Corepack and npm expose the command as
yarn, while Ubuntu’s APT package exposesyarnpkg. Mixing providers can make your shell run a different Yarn version than the one your project expects.
Install Yarn 4 with Corepack
Corepack is the Yarn-recommended package-manager bridge because it lets each project declare its Yarn version in package.json. Ubuntu 26.04 can use the packaged node-corepack flow directly; Ubuntu 24.04 and 22.04 users should install a current Node.js release first with NodeSource or NVM on Ubuntu, then return to the Corepack commands below.
On Ubuntu 26.04, install Node.js and Corepack from Ubuntu Universe:
sudo apt install nodejs node-corepack
On Ubuntu 24.04 and 22.04, skip that APT command after you install a current Node.js release from NodeSource or NVM. Then verify that your shell sees both node and corepack.
Verify the active versions:
node --version
corepack --version
Expected output on Ubuntu 26.04:
v22.22.1 0.24.0
Ubuntu 24.04 ships Node.js 18.19.1, and Ubuntu 22.04 ships Node.js 12.22.9. Yarn 4 currently requires Node.js 18.12.0 or newer, while the npm-published Corepack package currently targets newer Node.js than Ubuntu 24.04’s default package. For the least confusing Corepack setup on 24.04 and 22.04, install a current Node.js LTS line before enabling Corepack.
Enable Corepack. With Ubuntu’s packaged Corepack or a system-wide NodeSource package, the command writes shims into root-owned system paths, so run it with sudo:
sudo corepack enable
Create or enter a project directory, add a minimal package.json if needed, then pin the current stable Yarn 4 release:
mkdir -p ~/yarn-demo
cd ~/yarn-demo
printf '%s\n' '{"name":"yarn-demo","version":"1.0.0"}' > package.json
corepack use yarn@stable
Verify the active Yarn version inside the project:
yarn --version
grep '"packageManager"' package.json
The version command should report a Yarn 4 release. In this check, grep filters the package manifest for a packageManager line beginning with "yarn@4.. The exact point release and checksum change as the stable channel moves.
Install Yarn Classic with npm
The npm yarn package installs Yarn Classic 1.x. Use this method only for projects that still expect Classic behavior or tooling pinned to Yarn 1.
Install Node.js and npm from Ubuntu, then install Yarn Classic globally:
sudo apt install nodejs npm
sudo npm install -g yarn
Verify the global Yarn Classic version. It should report a 1.22.x Classic release, not Yarn 4:
yarn --version
Installing yarn through npm does not install Yarn 4. The Yarn project keeps Yarn 4 in the Corepack-managed flow so each project can pin the package-manager version instead of relying on a global binary.
Install Yarn from Ubuntu Repository
Ubuntu packages Yarn as yarnpkg in the Universe component. If a minimal system does not have Universe enabled, enable Universe first with the Ubuntu repository guide for enabling Universe and Multiverse on Ubuntu, then install the package.
sudo apt install yarnpkg
The APT package uses yarnpkg as the command name:
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 use only the APT method and want the shorter yarn command, add a symlink only when another provider has not already created one:
if ! command -v yarn >/dev/null; then
sudo ln -s /usr/bin/yarnpkg /usr/local/bin/yarn
fi
Ubuntu Yarn and Node.js Version Matrix
The default Ubuntu package versions explain why Corepack is the cleanest new-project path on 26.04, while 24.04 and 22.04 usually need a newer Node.js setup for modern Yarn workflows:
| Ubuntu release | yarnpkg candidate | Default Node.js candidate | Practical note |
|---|---|---|---|
| 26.04 LTS (Resolute) | 4.1.0+dfsg-1 | 22.22.1+dfsg+~cs22.19.15-1ubuntu1 | APT gives Yarn 4.1.0, and nodejs depends on node-corepack. |
| 24.04 LTS (Noble) | 1.22.19+~cs24.27.18-4 | 18.19.1+dfsg-6ubuntu5 | APT gives Yarn Classic; use a current Node.js setup for Corepack-first Yarn 4. |
| 22.04 LTS (Jammy) | 1.22.10+~cs22.25.14-8 | 12.22.9~dfsg-1ubuntu3.6 | The default Node.js package is too old for Yarn 4. |
Use Yarn on Ubuntu After Installation
The common project commands are similar between Yarn 4 and Yarn Classic, although Yarn 4 stores more package-manager state in the project itself.
Initialize a Project
yarn init
Yarn 4 creates a minimal manifest quickly, while Yarn Classic asks interactive questions unless you pass its non-interactive flags.
Add and Remove Dependencies
yarn add lodash
yarn add --dev eslint
yarn remove lodash
The --dev flag records a package in devDependencies, which is the right place for build tools, linters, test runners, and other packages that are not part of the production runtime.
Install Dependencies and Run Scripts
yarn install
yarn run build
Use yarn install after cloning an existing project with a package.json and yarn.lock. Use yarn run for scripts defined in the project’s package.json.
Use node_modules with Yarn 4
Yarn 4 defaults to Plug’n’Play, which removes the traditional node_modules directory. If a framework, editor extension, or build tool still expects node_modules, set Yarn’s node linker in the project:
nodeLinker: node-modules
Save that line in .yarnrc.yml, then reinstall dependencies:
yarn install
Compare Yarn with npm and pnpm
npm ships with Node.js and remains the default for many projects. Yarn 4 focuses on per-project package-manager pinning, workspaces, and Plug’n’Play, while pnpm on Ubuntu focuses on a content-addressable store that saves disk space across many repositories.
Use --ignore-engines Carefully
The yarn install --ignore-engines flag tells Yarn Classic to skip package engine checks from package.json. Use it only as a temporary compatibility workaround, because packages that ask for a newer Node.js version may still fail at runtime.
Update Yarn on Ubuntu
Update Yarn with the same provider you used for installation. Corepack updates the project pin, npm updates the global Classic package, and APT updates Ubuntu’s yarnpkg package.
corepack use yarn@stable
yarn install
sudo npm update -g yarn
sudo apt install --only-upgrade yarnpkg
The official Yarn installation page also documents yarn set version stable, which is useful when a project already has Yarn available and you want to move its pinned release forward.
Troubleshoot Yarn on Ubuntu
apt install yarn Does Not Install Yarn
Ubuntu does not provide the Yarn package manager as yarn. On Ubuntu 26.04, sudo apt install yarn returns Unable to locate package yarn. On Ubuntu 24.04 and 22.04, that command installs cmdtest, an unrelated testing tool.
If you installed cmdtest by mistake, remove it and install Yarn through Corepack, npm, or yarnpkg instead:
sudo apt remove cmdtest
hash -r
corepack: command not found
On Ubuntu 26.04, install node-corepack from APT. On Ubuntu 24.04 or 22.04, install a current Node.js release first, because the default packages are not the cleanest Corepack path for current Yarn 4 projects.
node --version
corepack --version
If corepack --version still fails after installing a current Node.js release, install Corepack through npm and enable it:
sudo npm install -g corepack
sudo corepack enable
Permission Denied During corepack enable
Ubuntu’s packaged Corepack writes shims such as yarn and pnpm into root-owned system paths. If corepack enable prints an EACCES symlink error, rerun it with sudo:
sudo corepack enable
If you installed Node.js through NVM in your home directory, do not use sudo; reload that shell and run corepack enable as your normal user instead.
Project Requests yarn@4.x but Yarn 1 Runs
If a project’s package.json defines "packageManager": "yarn@4.x" but your shell runs Yarn Classic, Corepack is not active or your PATH is finding a global Yarn first. Enable Corepack, enter the project, and verify again:
sudo corepack enable
cd /path/to/project
yarn --version
Normal Yarn 4 projects do not need manual downloads of files such as yarn-4.x.x.cjs. Corepack and yarn set version stable manage those release files when a project needs them.
Remove Yarn from Ubuntu
Remove Yarn with the same provider that installed it. Avoid a broad apt autoremove unless you review the preview first, because shared development packages may belong to other tools.
Remove Corepack-Managed Yarn
Disable Corepack shims first. This disables all Corepack-managed shims, including Yarn and pnpm; if you still need Ubuntu’s Node.js package, stop after this command:
sudo corepack disable
If you installed Ubuntu’s Corepack package only for Yarn and also want to remove Ubuntu’s packaged Node.js runtime, remove both packages together. On Ubuntu 26.04, nodejs depends on node-corepack, so removing node-corepack can remove nodejs too.
sudo apt remove node-corepack nodejs
If Corepack came from npm instead, remove the npm package:
sudo npm uninstall -g corepack
Remove the local Corepack cache if you no longer need downloaded package-manager releases:
rm -rf ~/.cache/node/corepack ~/.cache/Corepack
Remove npm Global Yarn Classic
sudo npm uninstall -g yarn
hash -r
npm list -g --depth=0 --unicode=false
The npm list command should no longer show yarn in the global package tree.
Remove APT Yarn
sudo apt remove yarnpkg
if [ "$(readlink /usr/local/bin/yarn 2>/dev/null)" = "/usr/bin/yarnpkg" ]; then
sudo rm -f /usr/local/bin/yarn
fi
hash -r
apt-cache policy yarnpkg
The relevant package-state line after removal should show:
Installed: (none)
Clean User Yarn Cache and Config
The next command deletes account-level Yarn cache and configuration files. It does not remove project-level
node_modules,yarn.lock,.yarnrc.yml, or.yarn/directories inside existing repositories.
rm -rf ~/.yarn ~/.yarnrc ~/.yarnrc.yml
Conclusion
Yarn is available on Ubuntu through Corepack for Yarn 4 projects, npm for Yarn Classic, or Ubuntu’s yarnpkg package when an APT-managed install is enough. Corepack is the better default for modern repositories because the project records its own package-manager version; after that, Git on Ubuntu and TypeScript on Ubuntu are natural next steps for JavaScript project work.


Formatting tips for your comment
You can use basic HTML to format your comment. Useful tags currently allowed in published comments:
<code>command</code>command<strong>bold</strong><em>italic</em><blockquote>quote</blockquote>