How to Install Yarn on Linux, macOS, and Windows

How to Install Yarn on Linux, macOS, and Windows

Yarn is one of the most popular dependency managers. Developers use it to package projects in a compact format and share them with communities worldwide.

Yarn is known for its high speed, robust security, and consistency. It was created to address the issues with the node package manager (npm), its famous competitor.

In this article, we will explain how to install Yarn on Linux, macOS, and Windows systems using three different tools – npm, cURL, and MSI.

What Is Yarn?

Yarn, which stands for Yet Another Resource Negotiator, is a JavaScript dependency manager created by Facebook, Google, Exponent, and Tilde. Commonly used on VPS (virtual private server), it aims to improve the workflow and performance of large codebases.

Here are the four main features of Yarn:

  • A per-project caching mechanism. Yarn stores data from every package installation in its cache. This speeds up the setup process when users want to install an application.
  • Plugins. Yarn comes with pre-installed plugins that contain most of its features. It also allows users to install more add-ons to extend the functionality.
  • Regular checksum checks. Yarn performs them at frequent intervals to verify the integrity of all packages. This process takes place before the setup.
  • Workspaces. Yarn provides workspaces that allow a monorepo (single repository) to manage the dependencies of multiple projects.

How to Install the Yarn Package Manager

In this section, we will explain how to set up the Yarn package manager on different operating systems.

Installing Yarn on Linux and macOS

You can use either npm or cURL to install Yarn on Linux VPS and macOS. However, it is important to have Node.js and npm ready before the installation process.

Using npm

Here’s how to install Yarn using npm:

  1. Open Terminal and connect to your server using SSH.
  2. Run the following command to install Yarn:
sudo npm install --global yarn
  1. Once the installation is completed, reopen Terminal and log in to SSH to enable the Yarn commands.
  2. Verify the Yarn version to make sure it has been successfully installed by typing in the command below and pressing Enter:
yarn --version

Using cURL

Follow these instructions to install Yarn using cURL on most Linux distros and macOS:

  1. Open Terminal and log in via SSH.
  2. Run the following command to install the Yarn package manager:
curl -o- -L https://yarnpkg.com/install.sh | bash
  1. Reopen Terminal and connect to SSH to enable Yarn.
  2. Lastly, verify the installation by typing in the command below and pressing Enter:
yarn --version

Using cURL (Debian)

Here’s how to install Yarn using cURL on Debian:

  1. Open Terminal and connect via SSH.
  2. Run the following command to add the GPG key and make sure the downloaded packages are authentic:
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
  1. Add the Yarn APT to the Debian package repository by typing in the command below and pressing Enter:
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
  1. Once the repository has been added, update the package list by inserting this command and pressing Enter:
sudo apt update
  1. Next, install Yarn by running the following command:
sudo apt install yarn
  1. Finally, type in the command below and press Enter to check whether the installation was successful:
yarn --version

Using cURL (CentOS)

Follow these steps to install Yarn using cURL on CentOS:

  1. Open Terminal and log in to SSH.
  2. Run the following command to add the Yarn repository to the system.
$ curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
  1. Next, import the repository’s GPG key by typing in the command below and pressing Enter:
sudo rpm --import https://dl.yarnpkg.com/rpm/pubkey.gpg
  1. Install Yarn by inserting the following command and pressing Enter:
sudo dnf install yarn
  1. Lastly, verify the installation by running this command:
yarn --version

Installing Yarn on Windows

Here’s how to install the Yarn package manager on Windows:

  1. Download the Yarn installation package from the official website.
  2. Run the installer by double-clicking the downloaded .msi file.
  3. Select Next to proceed.
The Yarn setup wizard on Windows
  1. Review the end-user license agreement and check the box to accept the terms. Click Next to continue.
The end-user license agreement of Yarn on Windows
  1. Choose the destination folder for the installation and select Next to proceed.
Selecting the destination folder of Yarn on Windows
  1. Click Install.
Beginning the Yarn installation on Windows
  1. Yarn has been installed successfully – click Finish.
The Yarn installation has been completed on Windows
  1. Open the Command Prompt application and run the following command to verify whether the installation was successful:
yarn --version
Verifying the Yarn version on Windows

The Basics of Using Yarn

In this section, we are going to explain the essentials of Yarn, including how to start a new project, manage dependencies, and update the package manager.

Starting a New Project

Here’s how to start a new project using Yarn:

  1. Create a new project by running the following command:
yarn init project_name
  1. The output will show the current version of the Yarn package manager.
  2. Press Enter. You will be presented with eight consecutive configuration questions.
  3. If you want Yarn to use the default values, press Enter after each question.
  4. Yarn will create two configuration files – package.json and yarn.lock. The former contains all the dependencies you have set when answering the questions, while the latter locks them to specific versions to ensure consistency within the dependency tree.
Setting up a new project on Yarn

Managing Dependencies

Adding a new dependency to your project will automatically download the module, install it, and update the package.json and yarn.lock files. Run the following command to do it:

yarn add [package]

It is also possible to define a specific version for each package or library as a dependency for your project. For example:

yarn add [package]@[version]

To update a package of projects specifically managed with Yarn, use the upgrade directive with the package name. For example:

yarn upgrade [package]

In addition, to specify which version to use in order to update the package, use this command:

yarn upgrade [package]@[version]

To remove a package from a project, run the command below:

yarn remove [package]

Running the above command will automatically update the package.json and yarn.lock files of the project.

Finally, to install all the defined dependencies, run the command below:

yarn install

Keep in mind that these dependencies are defined in the package.json file.

Upgrading Yarn

There are four methods to upgrade Yarn. Before doing it, verify the version you’re running. Do it by running the following command:

yarn --version

In order to upgrade Yarn, run either of these commands:

  • If Yarn was installed using npm:
npm install --global yarn
  • To update Yarn to the latest version:
yarn set version latest
  • To update Yarn to a specific version:
yarn set version [version.number]
  • For Unix machines using cURL:
curl --compressed -o- -L https://yarnpkg.com/install.sh | bash

Conclusion

Yarn is a software packaging system with advanced features to improve the development workflow. It was created as a solution to performance, consistency, and security issues with earlier dependency managers.

This article has discussed the most popular ways to install Yarn – choose one based on your operating system. In addition, we have also covered the basics of using Yarn – starting a new project, managing dependencies, and upgrading it.

We hope this article has helped you understand how to install Yarn and the basics of using it. If you have any questions or suggestions, please leave them in the comments section below.

Author
The author

Edward S.

Edward is a Content Editor with years of experience in IT as a writer, marketer, and Linux enthusiast. Edward's goal is to encourage readers to establish an impactful online presence. He also really loves dogs, guitars, and everything related to space.