Install Yarn using npm if you already have Node.js

The fastest way to get Yarn is through npm, the package manager that comes with Node.js. If you have Node.js installed, open your terminal or command prompt and run a single command. This installs Yarn globally on your machine, meaning you can use it in any project folder.

On Windows, Mac, or Linux, the command is the same: npm install -g yarn. The -g flag tells npm to install Yarn globally rather than just in your current project. After a few seconds, npm downloads Yarn and makes it available system-wide.

To confirm the installation worked, type yarn --version and press Enter. You should see a version number like 1.22.19 or 4.0.1. If you see an error instead, Node.js may not be in your system path, or you may need to restart your terminal.

Key Takeaways

  • The npm method works on all operating systems and requires only that Node.js is already installed on your computer.
  • Homebrew on Mac and Chocolatey on Windows offer alternative installation routes that do not depend on npm.
  • After installation, run yarn --version to confirm Yarn is working before you start a project.
  • Once installed, you initialize Yarn in a project by running yarn init or yarn install in the project folder.

Install Yarn on Mac using Homebrew

If you use Homebrew on Mac, you can install Yarn without needing Node.js first. Homebrew will read both Yarn and Node.js if they are not already on your machine. Open Terminal and run brew install yarn.

Homebrew handles the read and setup automatically. When the command finishes, Yarn is ready to use. This method is often faster than the npm route because Homebrew manages dependencies in the background and you do not have to think about whether Node.js is installed.

If you do not have Homebrew installed, you can get it from brew.sh. The installation takes a few minutes and is a one-time setup.

Install Yarn on Windows using Chocolatey or the installer

Windows users have two main options. The first is Chocolatey, a package manager for Windows similar to Homebrew on Mac. If you have Chocolatey installed, open Command Prompt as Administrator and run choco install yarn. Chocolatey downloads and installs both Node.js and Yarn for you.

The second option is the Yarn installer from yarnpkg.com. Go to the Yarn website, read the Windows installer (.msi file), and run it like any other Windows program. The installer walks you through setup and adds Yarn to your system path automatically. This method does not require Chocolatey or npm, making it the simplest choice if you have neither installed yet.

After installation by either method, open a new Command Prompt window and run yarn --version to confirm it works.

Install Yarn on Linux using your package manager

Linux distributions have their own package managers. On Ubuntu or Debian, run sudo apt-get install yarn. On Fedora or Red Hat, use sudo dnf install yarn. On Arch, use sudo pacman -S yarn. Each command downloads Yarn and its dependencies from your distribution's official repositories.

Using your distribution's package manager keeps Yarn updated alongside your other system software. When you run system updates, Yarn updates automatically as part of that process.

After installation, verify it worked by running yarn --version in a terminal.

Start using Yarn in a new or existing project

Once Yarn is installed, navigate to your project folder in the terminal and run yarn init. Yarn asks you questions about your project — name, version, description — and creates a package.json file. This file tells Yarn what packages your project needs.

If you already have a package.json file (from an existing project or from npm), just run yarn install in that folder. Yarn reads the file, downloads all the packages listed, and creates a yarn.lock file that locks each package to a specific version. This ensures everyone working on the project uses the same versions.

To add a new package to your project, run yarn add package-name. Yarn downloads the package and updates both package.json and yarn.lock automatically.

Troubleshoot common installation problems

If yarn --version returns "command not found" after installation, your system does not know where Yarn is located. On Mac or Linux, try closing and reopening your terminal window. On Windows, restart Command Prompt or PowerShell. If that does not work, the installer may not have added Yarn to your system path — reinstall using the method for your operating system and choose the option to add to PATH during setup.

If npm install -g yarn fails with a permission error on Mac or Linux, you may need to use sudo npm install -g yarn instead, though this is not recommended for security reasons. A better fix is to configure npm to use a different directory for global packages, which you can learn about on the npm documentation site.

If you installed Node.js but npm is not found, Node.js may not be in your system path. Reinstall Node.js from nodejs.org and make sure to choose the option to add Node.js to PATH during installation.

Frequently Asked Questions

Do I need Node.js to use Yarn?

Yarn itself runs on Node.js, so yes, Node.js must be installed. However, you do not have to install Node.js separately if you use Homebrew on Mac or Chocolatey on Windows — those package managers install Node.js automatically when you install Yarn. If you use the npm method, Node.js must already be on your machine.

What is the difference between yarn.lock and package.json?

package.json lists the packages your project needs and the version ranges you allow. yarn.lock records the exact version of every package that was installed, including dependencies of dependencies. When someone else clones your project and runs yarn install, Yarn uses yarn.lock to install the exact same versions you used, preventing surprises from version changes.

Can I use Yarn and npm in the same project?

You can, but it is not recommended. Using both creates two lock files (yarn.lock and package-lock.json) that can conflict. Pick one package manager for your project and stick with it. If you inherit a project that uses npm, you can switch to Yarn by deleting package-lock.json and running yarn install.

How do I uninstall Yarn?

If you installed Yarn with npm, run npm uninstall -g yarn. On Mac with Homebrew, run brew uninstall yarn. On Windows with Chocolatey, run choco uninstall yarn. On Linux, use your package manager: sudo apt-get remove yarn on Ubuntu, or the equivalent for your distribution.