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 -vandnpm -v) - An internet connection
- Terminal access
Step-by-Step
- Determine the required Yarn version. Look for a
packageManagerfield in your project’spackage.json(e.g.,"packageManager": "yarn@3.2.1"). If absent, check for ayarn.lockfile—its header often indicates the major version (e.g.,# yarn lockfile v1for Yarn 1.x). - 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. - 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 vianpm install -g corepack. - Install the project-specific version. Navigate to your project directory (
cd /path/to/project) and runcorepack 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. - 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. - Verify the installation. Run
yarn --versionagain. It should now show the version you installed. If you used Corepack, you can also checkcorepack yarn --versionto confirm the downloaded version. - Run
yarn install. With the correct version active, runyarn installin 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.lockv1, 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, theyarncommand 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
packageManagerfield. 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
packageManagerfield inpackage.jsonwhen you runcorepack yarn—no manual version switching needed. - Pin your Yarn version. Add the
packageManagerfield to yourpackage.jsonso 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 withnpm bin -gand add it to your shell profile. - Yarn version doesn’t change after running
corepack prepare. The--activateflag sets it for the current shell only. Open a new terminal or runsource ~/.bashrcto 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, not3.2). Check the project’spackageManagerfield 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 yarnthensudo npm install -g yarn@1.22.19. - Lockfile conflicts after switching versions. If you accidentally ran a different Yarn version, delete
yarn.lockandnode_modules, then runyarn installwith 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
- How to Create App Shortcuts in Windows 10 Start Menu
- How to Rename a SQL Server Database
- Fix Administrator Blocked You From Running This App Error
- Fix Google Calendar Not Syncing on iPhone
- Fix Skype Microphone Not Working on Windows
- Google Docs Landscape: Change Page Orientation