How to Install Yarn on Debian Linux

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 typically provides faster installs and more reliable dependency resolution than npm in many workflows. By the end of this guide, you will have Yarn Berry installed on your Debian system using the officially recommended Corepack method.

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
CorepackYarn OfficialLatest Berry (4.x)Manual via corepack prepareAll users: official method with per-project version control
Debian ReposDebian PackagesBerry 4.xAutomatic via apt upgradeDebian 13 users wanting distro-managed packages

Recommendation: Use Corepack because it’s officially endorsed by the Yarn team, provides per-project version control, and works across all Debian versions. The Debian repository method is only viable on Debian 13, which ships Yarn Berry 4.x. Older Debian versions ship the legacy Classic version, which is no longer recommended.

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

Fortunately, Debian 13 includes Node.js 20.x with Corepack in the default repositories:

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.x.x
0.x.x

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.x.x
0.x.x

Enable Corepack and Install Yarn Berry

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

sudo corepack enable

Then 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.x.x

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.x.x

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 daily. The syntax differs in key areas. Notably, yarn up replaces yarn upgrade, and yarn dlx replaces the need for globally installed CLI tools. This section covers the commands you’ll use most frequently when managing JavaScript projects, and understanding these patterns will help you work more efficiently. For complete documentation, see the official Yarn CLI reference.

If you installed Yarn from Debian repositories, the command is yarnpkg instead of yarn. Either use yarnpkg in place of yarn in the following examples, or create the symlink shown in the Debian repository installation section above.

Initialize a Project

When starting a new JavaScript project, proper initialization ensures consistent behavior across your team’s machines. To get started, create a new project with Yarn Berry:

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

To ensure consistency, pin a specific Yarn version to the current project:

yarn set version stable

Alternatively, specify an exact version:

yarn set version 4.5.0

Either approach downloads Yarn to .yarn/releases/ and updates package.json, ensuring all team members use the same Yarn version.

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.

To add a production dependency:

yarn add lodash

For development dependencies, use the -D flag:

yarn add -D eslint

Conversely, to remove a dependency:

yarn remove lodash

When setting up a project, install all dependencies from package.json:

yarn install

Or simply:

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

To upgrade a specific package to its latest version:

yarn up lodash

For interactive upgrades that let you review each change:

yarn up --interactive

Alternatively, update all packages at once:

yarn up '*'

Run Scripts

To execute scripts defined in package.json:

yarn run build
yarn run test

For common scripts, you can omit run:

yarn build
yarn test
yarn start

Run Binaries with dlx

Similarly, Yarn Berry uses dlx to run packages without installing them globally (similar to npx):

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.

To run a command in a specific workspace:

yarn workspace my-package build

Likewise, to execute a command across all workspaces:

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/. Consequently, this approach dramatically speeds up installs and reduces disk usage.

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. Typically, most problems stem from Node.js version mismatches, permission conflicts, or PnP compatibility with certain packages. Below are 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 it’s below v16.10, then install a modern Node.js version using NodeSource as shown in the installation section.

PnP Compatibility Issues

Unfortunately, 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

To fix this, use sudo when running corepack enable:

sudo corepack enable

yarnpkg vs yarn Command (Debian Repos)

When installed from Debian repositories, the command is yarnpkg, not yarn. Either use yarnpkg directly or create a symlink as shown in the Debian repository installation section.

Remove Yarn from Debian

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

Disable Yarn in Corepack

To remove Yarn when installed via Corepack, disable the Yarn shim:

sudo corepack disable yarn

Next, verify the removal:

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

Finally, verify the removal:

dpkg -l yarnpkg

A successful removal shows:

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. With corepack prepare yarn@stable --activate, you installed the latest Yarn version and can now use yarn set version to pin specific versions per-project. Furthermore, 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 an easy way to keep dependencies current. To continue building your development environment, consider exploring installing Git on Debian for version control or setting up Flatpak on Debian for containerized desktop applications.

Leave a Comment