To install Yarn 2 (also known as Yarn Berry) globally on your system, you use a simple command via npm.
The exact command to install Yarn 2 globally is:
npm install -g yarn@berry
Using this command ensures that the latest stable release of Yarn 2 is available system-wide, allowing you to run yarn
commands from any directory on your machine.
Understanding the Global Installation Command
Let's break down the command:
npm install
: This is the standard npm command used to install packages.-g
: This flag specifies a global installation. Instead of installing the package within the current directory'snode_modules
, it installs it in a system-wide location, making the executable available in your system's PATH.yarn@berry
: This specifies the package to install (yarn
) and a specific tag (berry
). Theberry
tag points to the latest stable release of Yarn 2.
As per the reference provided: "To install yarn 2 globally, we use npm install -g yarn@berry". This confirms the command is the correct method for a global installation.
Why Install Globally?
Installing Yarn globally allows you to:
- Initialize new projects using
yarn init
. - Run Yarn commands (
yarn install
,yarn add
, etc.) in projects that are not yet configured with a specific Yarn version via theyarn policies
method. - Have a fallback Yarn version available system-wide.
Global vs. Project-Specific Installation
It's important to note the difference between global and project-specific installations:
- Global Installation (
npm install -g yarn@berry
): Installs Yarn onto your system PATH. Useful for initializing projects or when you need a general-purpose Yarn executable. - Project-Specific Installation (
yarn policies set-version berry
): Installs Yarn within a specific project's directory (.yarn/releases/
). This is the recommended approach for most projects as it locks the project to a specific Yarn version, ensuring consistency across development environments. The reference also highlights this distinction: "To install yarn 2 to a specific project, we useyarn policies set-version berry
".
Installation Type | Command | Location | Primary Use Case |
---|---|---|---|
Global | npm install -g yarn@berry |
System PATH | Initializing projects, general use |
Project-Specific | yarn policies set-version berry |
.yarn/releases/ |
Project consistency, version locking |
While a global installation gets you started, adopting the project-specific yarn policies
approach is often preferred for collaborative development and production builds to avoid "it works on my machine" issues related to differing Yarn versions.
Steps to Install Globally
- Open your terminal or command prompt.
- Ensure you have Node.js and npm installed, as npm is used for the global installation.
- Run the command:
npm install -g yarn@berry
- Verify the installation by checking the Yarn version:
yarn --version
The output should show a version number starting with
2.x.x
or3.x.x
(as Yarn 2 has evolved and subsequent releases keep the "Berry" architecture).
By following these steps, you will have successfully installed Yarn 2 globally on your system.