· coding · 5 min read · Save it 👉

How to install Yarn

A brief guide on how to install yarn.

A brief guide on how to install yarn.

How to Install 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. For an effortless setup, opt for Hostinger’s Node.js VPS plans that offer a pre-made template of Ubuntu 22.04 64bit with Node.js and OpenLiteSpeed.

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

How to Install 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.

  4. Review the end-user license agreement and check the box to accept the terms. Click Next to continue.

  5. Choose the destination folder for the installation and select Next to proceed.

  6. Click Install.

  7. Yarn has been installed successfully – click Finish.

  8. Open the Command Prompt application and run the following command to verify whether the installation was successful:

yarn --version

How to Use 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.

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 that help developers improve their workflow. It was developed to address performance, consistency, and security issues with previous dependency managers.

This article has covered the most common methods for installing Yarn; choose one based on your operating system. In addition, we covered the fundamentals of using Yarn, such as creating a new project, managing dependencies, and upgrading it.

We hope that this article has helped you understand how to install Yarn and how to use it. If you have any comments or questions, please let me know.

Share:
Back to Blog