How To Install and Use the Yarn Package Manager for Node.js

How To Install and Use the Yarn Package Manager for Node.js


Yarn is a package manager for Node.js that focuses on speed, security, and consistency. It was originally created to address some issues with the popular NPM package manager. Though the two package managers have since converged in terms of performance and features, Yarn remains popular, especially in the world of React development.

Some of the unique features of Yarn are:

  • A per-project caching mechanism, that can greatly speed up subsequent installs and builds
  • Consistent, deterministic installs that guarantee the structure of installed libraries are always the same
  • Checksum testing of all packages to verify their integrity
  • “Workspaces”, which facilitate using Yarn in a monorepo (multiple projects developed in a single source code repository)

In this tutorial, you will install Yarn globally, add Yarn to a specific project, and learn some basic Yarn commands.

Deploy your frontend applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app.

Installing & Using Yarn Package Manager for Node.js

  1. Install Yarn globally
  2. Install Yarn in your project
  3. Use Yarn


Before installing and using the Yarn package manager, you will need to have Node.js installed. To see if you already have Node.js installed, type the following command into your local command line terminal:

  1. node -v

If you see a version number, such as v12.16.3 printed, you have Node.js installed. If you get a command not found error (or similar phrasing), please install Node.js before continuing.

To install Node.js, follow our tutorial for Ubuntu, Debian, CentOS, or macOS.

Once you have Node.js installed, proceed to Step 1 to install the Yarn package manager.

Step 1 — Installing Yarn Globally

Yarn has a unique way of installing and running itself in your JavaScript projects. First, you install the yarn command globally, then you use the global yarn command to install a specific local version of Yarn into your project directory. This is necessary to ensure that everybody working on a project (and all of the project’s automated testing and deployment tooling) is running the same version of yarn, to avoid inconsistent behaviors and results.

The Yarn maintainers recommend installing Yarn globally by using the NPM package manager, which is included by default with all Node.js installations. Use the -g flag with npm install to do this:

  1. sudo npm install -g yarn

After the package installs, have the yarn command print its own version number. This will let you verify it was installed properly:

  1. yarn --version

Now that you have the yarn command installed globally, you can use it to install Yarn into a specific JavaScript project.

Step 2 — Installing Yarn in Your Project

You can skip this step if you are using Yarn to work with an existing Yarn-based project. The project should already be set up with a local version of Yarn and all the configuration files necessary to use it.

If you are setting up a new project of your own, you’ll want to configure a project-specific version of Yarn now.

First, navigate to your project directory:

  1. cd ~/my-project

If you don’t have a project directory, you can make a new one with mkdir and then move into it:

  1. mkdir my-project
  2. cd my-project

Now use the yarn set command to set the version to berry:

  1. yarn set version berry

This will download the current, actively developed version of Yarn – berry – save it to a .yarn/releases/ directory in your project, and set up a .yarnrc.yml configuration file as well:

Resolving berry to a url... Downloading https://github.com/yarnpkg/berry/raw/master/packages/berry-cli/bin/berry.js... Saving it into /home/sammy/my-project/.yarn/releases/yarn-berry.cjs... Updating /home/sammy/my-project/.yarnrc.yml... Done!

Now try the yarn --version command again:

  1. yarn --version

You’ll see the version is 3.0.0 or higher. This is the latest release of Yarn.

Note: If you cd out of your project directory and run yarn --version again, you’ll once again get the global Yarn’s version number, 1.22.11 in this case. Every time you run yarn, you use the command’s globally installed version. The global yarn command first checks to see if it’s in a Yarn project directory with a .yarnrc.yml file, and if it is, it hands the command off to the project-specific version of Yarn configured in the project’s yarnPath setting.

Your project is now set up with a project-specific version of Yarn. Next, we’ll look at a few commonly used yarn commands to get started with.

Using Yarn

Yarn has many subcommands, but you only need a few to get started. Let’s look at the first subcommands you’ll want to use.

Getting Help

When starting with any new tool, it’s useful to learn how to access its online help. In Yarn the --help flag can be added to any command to get more information:

  1. yarn --help

This will print out overall help for the yarn command. To get more specific information about a subcommand, add --help after the subcommand:

  1. yarn install --help

This would print out details on how to use the yarn install command.

Starting a New Yarn Project

If you’re starting a project from scratch, use the init subcommand to create the Yarn-specific files you’ll need:

  1. yarn init

This will add a package.json configuration file and a yarn.lock file to your directory. The package.json contains the configuration and your list of module dependencies. The yarn.lock file locks those dependencies to specific versions, making sure that the dependency tree is always consistent.

Installing all of a Project’s Dependencies

To download and install all the dependencies in an existing Yarn-based project, use the install subcommand:

  1. yarn install

This will download and install the modules you need to get started.

Adding a New Dependency to a Project

Use the add subcommand to add new dependencies to a project:

  1. yarn add package-name

This will download the module, install it, and update your package.json and yarn.lock files.

Updating Your .gitignore File for Yarn

Yarn stores files in a .yarn folder inside your project directory. Some of these files should be checked into version control and others should be ignored. The basic .gitignore configuration for Yarn follows:


This ignores the entire .yarn directory and then adds in some exceptions for important folders, including the releases directory which contains your project-specific version of Yarn.

For more details on how to configure Git and Yarn, please refer to the official Yarn documentation on .gitignore.


In this tutorial, you installed Yarn and learned about a few yarn subcommands. For more information on using Yarn, take a look at the official Yarn CLI documentation.

For more general Node.js and JavaScript help, please visit our Node.js and JavaScript tag pages, where you’ll find relevant tutorials, tech talks, and community Q&A.

DigitalOcean provides multiple options for deploying Node.js applications, from our simple, affordable virtual machines to our fully-managed App Platform offering. Easily host your Node.js application on DigitalOcean in seconds.

Learn more here

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Can add a section about Yarn adding and managing global packages? I’m stuck and searching for resources.

use “yarn add” instead of “yarn install”

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel