sodcalculator

10 Tech How-To

Yarn Version Install for Ubuntu Projects

Guide 10-43985 · Updated · Step by step

This guide shows you how to install the exact Yarn version your Ubuntu project needs, whether it’s a legacy release for an older codebase or the latest stable build. You’ll learn to check your project’s requirement, install Yarn via Corepack or npm, and verify the installation—all from the command line.

What You’ll Need

  • Ubuntu (20.04 or newer recommended) with sudo access
  • Node.js and npm installed (check with node -v and npm -v)
  • An internet connection
  • Terminal access

Step-by-Step

  1. Determine the required Yarn version. Look for a packageManager field in your project’s package.json (e.g., "packageManager": "yarn@3.2.1"). If absent, check for a yarn.lock file—its header often indicates the major version (e.g., # yarn lockfile v1 for Yarn 1.x).
  2. Check if Yarn is already installed. Run yarn --version. If it prints a version, that’s your current global Yarn. Note it, but you may still need to switch for the project.
  3. Enable Corepack (for Yarn 2+). Corepack ships with Node.js 16.13+ and manages Yarn versions. Run sudo corepack enable. If Corepack isn’t found, update Node.js or install it via npm install -g corepack.
  4. Install the project-specific version. Navigate to your project directory (cd /path/to/project) and run corepack prepare yarn@<version> --activate (replace <version> with the exact version, e.g., yarn@3.2.1). This downloads and sets that version as active for the current shell.
  5. For Yarn 1.x (classic), use npm. If your project uses Yarn 1.x (lockfile v1), run sudo npm install -g yarn@1.22.19 (or the specific 1.x version you need). This replaces any global Yarn with the classic version.
  6. Verify the installation. Run yarn --version again. It should now show the version you installed. If you used Corepack, you can also check corepack yarn --version to confirm the downloaded version.
  7. Run yarn install. With the correct version active, run yarn install in your project directory. This installs dependencies according to the lockfile.

Common Mistakes

  • Using the wrong package manager for the lockfile. If you have a yarn.lock v1, using Yarn 2+ may cause errors or silently upgrade the lockfile. Always match the major version.
  • Forgetting to activate Corepack. Corepack is not enabled by default on some Ubuntu setups. Without corepack enable, the yarn command may not exist or may point to an old version.
  • Installing globally when you only need a project version. Global installs affect all projects. Prefer Corepack for project-specific versions to avoid conflicts.
  • Not checking the packageManager field. Many modern projects specify the exact Yarn version there. Ignoring it can lead to using an incompatible version.

Tips

  • Use Corepack for Yarn 2+. It automatically respects the packageManager field in package.json when you run corepack yarn—no manual version switching needed.
  • Pin your Yarn version. Add the packageManager field to your package.json so all developers use the same version.
  • Check the official Yarn docs. For migration or version-specific features, refer to yarnpkg.com—it has detailed guides for each major version.
  • Keep Node.js updated. Corepack is bundled with Node.js, so staying current ensures you have the latest management features.

Troubleshooting

  • “Command ‘yarn’ not found” after installation. Ensure Corepack is enabled (sudo corepack enable) or that npm’s global bin directory is in your PATH. If using npm, check with npm bin -g and add it to your shell profile.
  • Yarn version doesn’t change after running corepack prepare. The --activate flag sets it for the current shell only. Open a new terminal or run source ~/.bashrc to refresh. Also, ensure you’re in the project directory when running the command.
  • Corepack errors like “Can’t find the requested version.” The version string must be exact (e.g., 3.2.1, not 3.2). Check the project’s packageManager field or the Yarn releases page for valid versions.
  • Yarn 1.x install fails with “Cannot find module ‘semver’”. This often happens when the global npm installation is corrupted. Reinstall with sudo npm uninstall -g yarn then sudo npm install -g yarn@1.22.19.
  • Lockfile conflicts after switching versions. If you accidentally ran a different Yarn version, delete yarn.lock and node_modules, then run yarn install with the correct version to regenerate a clean lockfile.

Frequently Asked Questions

How do I know which Yarn version my project needs?

Check the packageManager field in package.json. If it’s absent, the yarn.lock header will indicate the major version (v1 for classic, v2+ for berry).

Can I have multiple Yarn versions installed at once?

Yes. Corepack allows you to have multiple versions cached and switch per project. Global npm installs only hold one version at a time, but you can use Corepack alongside it.

What’s the difference between Yarn 1 and Yarn 2+?

Yarn 1 (classic) is the older stable line. Yarn 2+ (berry) introduces changes like Plug’n’Play (zero-install), stricter workspace support, and different configuration files. Some older projects may not be compatible with Yarn 2+.

Why does yarn install fail with “The remote archive doesn’t match the expected checksum”?

This usually happens when the lockfile is out of sync or when using a different Yarn version. Delete yarn.lock and node_modules, then reinstall with the correct Yarn version.

Do I need sudo to install Yarn?

For global npm installs, yes, unless you have configured npm to install to a user-writable directory. For Corepack, sudo is needed to enable it system-wide, but using corepack yarn within a project doesn’t require sudo.

How do I switch back to a different Yarn version later?

Use corepack prepare yarn@<version> --activate to switch, or reinstall globally via npm if you’re using classic. You can also set the packageManager field in each project to auto-switch with Corepack.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →