How to Install Yarn on Debian

Yarn is a JavaScript package manager that handles project dependencies with parallel downloads, offline caching, and deterministic lockfiles. Whether you need to build React applications, manage Node.js backend projects, or work with modern JavaScript toolchains, Yarn provides faster installs and more reliable dependency resolution than npm in many workflows. By the end of this guide, you will have Yarn Berry 4.x installed on your Debian system, configured with per-project version control and ready for production JavaScript development.

Understand Yarn Versions

Before installing, understand that Yarn has two major versions with different capabilities:

Yarn Classic (1.x): Legacy Version

  • The original Yarn, now in maintenance mode since January 2020
  • Only receives critical security fixes
  • Uses traditional node_modules folder structure
  • Not recommended for new projects

Yarn Berry (2.x, 3.x, 4.x): Modern Version

  • The actively developed version recommended by the Yarn team
  • Installed via Corepack, which ships with Node.js 16.10+
  • Features Plug’n’Play (PnP) for faster installs without node_modules
  • Per-project version control via packageManager field in package.json
  • Zero-installs capability for instant project setup

The official Yarn repository explicitly states: “Yarn 1 (Classic) is in maintenance mode. We recommend upgrading to Yarn 4.” This guide focuses exclusively on Yarn Berry installed via Corepack, the officially recommended method.

Choose Your Installation Method

Two methods provide modern Yarn Berry on Debian, and the comparison table below outlines each method’s trade-offs.

MethodChannelVersionUpdatesBest For
Corepack (Recommended)Yarn OfficialLatest stable (4.12.0+)Manual via corepack prepareAll users: official method with per-project version control
Debian ReposDebian PackagesDistro stable (4.1.0)Automatic via apt upgradeDebian 13 users wanting distro-managed packages

For most users, the Corepack method is recommended because it’s officially endorsed by the Yarn team, provides per-project version control via the packageManager field in package.json, and works across all Debian versions. The Debian repository method is only viable on Debian 13, which ships Yarn Berry 4.x. In contrast, Debian 11 and 12 ship the legacy Classic version (1.x), which is no longer recommended for new projects.

Install Yarn via Corepack

Corepack is Node.js’s built-in package manager manager that enables per-project Yarn version control, and this is the officially recommended installation method from the Yarn documentation.

Install Node.js on Debian 13

Debian 13 includes Node.js 20.x with Corepack in the default repositories, so installation is straightforward:

sudo apt update
sudo apt install nodejs

Next, verify that Node.js and Corepack are available:

node --version
corepack --version

You should see output similar to:

v20.19.2
0.24.0

Debian 13’s Node.js 20.x is sufficient for Yarn Berry. If you specifically need Node.js 24.x, you can use the NodeSource instructions in the Debian 11/12 section below.

Install Node.js on Debian 11 or 12

Debian 11 ships Node.js 12.x and Debian 12 ships Node.js 18.x, but neither version includes the Corepack binary required for Yarn Berry installation. Additionally, the yarnpkg packages in both releases are Yarn Classic 1.x, which is in maintenance mode. Therefore, to use the recommended Corepack method, you’ll need to install Node.js LTS from NodeSource.

First, install the prerequisites and create the keyrings directory:

sudo apt update
sudo apt install -y curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings

Next, download and import the NodeSource GPG key:

curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

Then, create the NodeSource repository configuration:

cat <<EOF | sudo tee /etc/apt/sources.list.d/nodesource.sources
Types: deb
URIs: https://deb.nodesource.com/node_24.x
Suites: nodistro
Components: main
Signed-By: /etc/apt/keyrings/nodesource.gpg
EOF

The nodistro suite is NodeSource’s unified repository that works across all Debian and Ubuntu versions. If you need a different Node.js major version, change node_24.x to node_22.x or node_20.x.

Now, update the package lists and install Node.js:

sudo apt update
sudo apt install -y nodejs

Finally, verify the installation:

node --version
corepack --version

You should see output similar to:

v24.12.0
0.34.5

Enable Corepack and Install Yarn Berry

With Node.js installed, enable Corepack to manage Yarn:

sudo corepack enable

This command runs silently on success, creating the Yarn shim in your system’s Node.js bin directory.

Next, install the latest stable Yarn version globally:

corepack prepare yarn@stable --activate

Finally, verify that Yarn Berry is installed:

yarn --version

The output should display:

4.12.0

Alternative: Install from Debian Repositories (Debian 13 Only)

Debian 13 ships Yarn Berry 4.x in the default repositories. While this method provides automatic security updates through standard system updates, it lacks the per-project version control that Corepack offers.

This method only works on Debian 13 (Trixie). Debian 11 and 12 ship Yarn Classic 1.x, which is in maintenance mode and not recommended for new projects. If you’re on Debian 11 or 12, use the Corepack method above.

Update your package lists and install Yarn:

sudo apt update
sudo apt install yarnpkg

Next, verify the installation:

yarnpkg --version

The output should show:

4.1.0

The package is named yarnpkg (not yarn) in Debian repositories to avoid conflicts with an unrelated package. After installation, run commands using yarnpkg instead of yarn, or create a symlink for convenience.

Create a Yarn Command Symlink

For convenience, to use the standard yarn command instead of yarnpkg, create a symbolic link:

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

Once created, you can use yarn directly:

yarn --version

Essential Yarn Berry Commands

Yarn Berry introduces several improvements over Classic that change how you interact with the package manager. The syntax differs in key areas: yarn up replaces yarn upgrade, and yarn dlx replaces the need for globally installed CLI tools. The following sections cover the commands you’ll use most frequently, organized by workflow. For complete documentation, see the official Yarn CLI reference.

If you installed Yarn from Debian repositories (the yarnpkg package), the command is yarnpkg instead of yarn. You can either use yarnpkg in all examples below, or create the symlink shown earlier to use the standard yarn command.

Initialize a Project

Proper initialization ensures consistent behavior across your team’s machines by pinning Yarn’s version in the project. Create a new project with Yarn Berry using:

yarn init -2

The -2 flag explicitly initializes a Yarn Berry project rather than falling back to Classic. As a result, this command creates a package.json with a packageManager field that pins the Yarn version for the project:

{
  "name": "my-project",
  "packageManager": "yarn@4.x.x"
}

Alternatively, for an interactive setup with prompts:

yarn init

Set Yarn Version Per-Project

Pinning a specific Yarn version ensures everyone on the team uses the same release. Use the stable channel to get the latest production-ready version:

yarn set version stable

Alternatively, specify an exact version:

yarn set version 4.5.0

Either approach downloads Yarn to .yarn/releases/ and updates the packageManager field in package.json. This ensures all team members use the same Yarn version when they clone the repository. Configuration settings are stored in .yarnrc.yml at the project root.

Manage Dependencies

Dependency management is where you’ll spend most of your time with Yarn. Understanding the difference between production and development dependencies helps keep your deployed applications lean. Production dependencies ship with your app, while development dependencies only assist during development.

Adding a production dependency is straightforward:

yarn add lodash

Development-only dependencies use the -D flag, which prevents them from shipping with production builds:

yarn add -D eslint

Removing a package works similarly:

yarn remove lodash

When cloning a project or restoring dependencies, install all packages from the existing package.json:

yarn install

Or use the shorthand form:

yarn

Update Dependencies

Keeping dependencies updated is essential for security patches and new features, but updates can also introduce breaking changes. Yarn Berry provides tools to review updates before applying them, which is especially important in production projects.

Before updating, check for outdated packages:

yarn outdated

Upgrading a specific package to its latest version requires the up command:

yarn up lodash

Interactive mode lets you review each update before applying it, which is useful for catching breaking changes:

yarn up --interactive

If you want to update all packages at once instead, use the wildcard pattern:

yarn up '*'

Run Scripts

Scripts defined in your package.json run through Yarn’s script runner:

yarn run build
yarn run test

Common scripts like build, test, and start work without the explicit run keyword:

yarn build
yarn test
yarn start

Run Binaries with dlx

The dlx command lets you run packages without installing them globally, similar to npm’s npx. This is useful for project scaffolding tools and one-off commands:

yarn dlx create-react-app my-app
yarn dlx degit user/repo my-project

Workspace Commands

Workspaces let you manage multiple related packages in a single repository, commonly called a monorepo. This approach is popular for organizations maintaining shared component libraries or microservices that need to stay in sync. As a result, Yarn Berry has powerful monorepo support built in.

Running a command in a specific workspace uses the following syntax:

yarn workspace my-package build

Executing a command across all workspaces requires the foreach subcommand:

yarn workspaces foreach run build

Plug’n’Play Mode

By default, Yarn Berry uses Plug’n’Play (PnP), which eliminates the node_modules folder entirely. Instead of thousands of nested folders, PnP uses a single .pnp.cjs file that maps imports directly to zip archives in .yarn/cache/. This approach dramatically speeds up installs and reduces disk usage. The main PnP files in your project are:

  • .pnp.cjs – The module resolution map (this is what Node uses to find packages)
  • .yarn/cache/ – Zipped package archives
  • .yarnrc.yml – Project-level Yarn configuration

However, some packages may not work correctly with PnP, particularly older ones or those that make assumptions about node_modules structure. If you encounter compatibility issues, you can switch to the traditional node_modules mode:

yarn config set nodeLinker node-modules
yarn install

Later, to switch back to PnP:

yarn config set nodeLinker pnp
yarn install

Troubleshoot Common Yarn Issues

Even after a successful installation, you may encounter configuration issues that prevent Yarn from working correctly. Most problems stem from Node.js version mismatches, permission conflicts, or PnP compatibility with certain packages. The following sections cover the most common problems and their solutions based on issues frequently reported in the Yarn community.

Corepack Not Found

If you see this error:

bash: corepack: command not found

This error means your Node.js version is too old (Corepack requires Node.js 16.10+). To diagnose, first check your version:

node --version

If the output shows a version below v16.10, install a modern Node.js version using NodeSource as shown in the installation section.

PnP Compatibility Issues

Some packages don’t work correctly with Plug’n’Play. This is particularly true for native modules, packages that scan node_modules at runtime, or older packages that haven’t been updated for PnP compatibility. If you see errors like:

Error: Cannot find module 'some-package'

This error typically indicates the package expects a traditional node_modules structure. To resolve this issue, switch to node_modules linker mode:

yarn config set nodeLinker node-modules
yarn install

Afterward, verify the change:

yarn config get nodeLinker

The output confirms the setting:

node-modules

Permission Errors

If you see permission errors when enabling Corepack:

EACCES: permission denied

Running Corepack with sudo resolves this issue since it needs to write to system directories:

sudo corepack enable

yarn Command Not Found (Debian Repos)

If you installed Yarn from Debian repositories and see “command not found” when running yarn, remember the package name is yarnpkg. Create the symlink shown earlier or use yarnpkg directly.

Remove Yarn from Debian

Should you need to uninstall Yarn, whether to switch installation methods or troubleshoot conflicts, use the method corresponding to how you originally installed it. Mixing removal methods can leave orphaned files or broken symlinks.

Disable Yarn in Corepack

Disabling the Yarn shim removes it from Corepack without affecting other package managers:

sudo corepack disable yarn

Verify the removal by checking if the command is still available:

yarn --version

The expected output confirms that Yarn is no longer available:

bash: yarn: command not found

Remove NodeSource Repository (If Added)

If you added the NodeSource repository for Debian 11 or 12 and no longer need it, remove the repository configuration and GPG key:

sudo rm /etc/apt/sources.list.d/nodesource.sources
sudo rm /etc/apt/keyrings/nodesource.gpg
sudo apt update

Once removed, the system will use Debian’s default Node.js version for future installations.

Remove Yarn Installed from Debian Repositories

To remove Yarn installed via APT, run:

sudo apt remove --purge yarnpkg
sudo apt autoremove

Additionally, remove the symlink if you created one:

sudo rm /usr/local/bin/yarn

Confirm the symlink was removed:

ls -l /usr/local/bin/yarn

You should see:

ls: cannot access '/usr/local/bin/yarn': No such file or directory

Next, verify the APT package removal:

dpkg -l yarnpkg

The expected output confirms the package was removed:

dpkg-query: no packages found matching yarnpkg

Remove Yarn Configuration and Cache

Warning: The following commands permanently delete Yarn’s configuration and cached packages. Back up any configuration you want to keep first.

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

Conclusion

You now have Yarn Berry installed on Debian using the officially recommended Corepack method. The corepack prepare yarn@stable --activate command installed the latest Yarn version, and you can use yarn set version to pin specific versions per-project. The yarn init -2 command creates modern projects with the packageManager field, while yarn dlx lets you run one-off commands without global installs. For existing projects, yarn up --interactive provides a convenient way to review and apply dependency updates. To continue building your development environment, consider installing Git on Debian for version control or setting up Visual Studio Code on Debian for a full-featured JavaScript IDE.

Leave a Comment