Yarn is a fast, reliable and secure package manager for Node.js applications. It has several advantages over npm. Yarn creates a cache for every package downloaded to your system and reuses it when needed, so there is no need to download the same package again and again.
This tutorial presents 3 methods for installing Yarn on Ubuntu 20.04. You can choose one of the methods below depending on your preference and system environment.
Before you start installing Yarn, don't forget to install Node.js on your system.
1. Install Yarn with the PPA
Yarn provides an official repository for installation on Linux systems. When you use the repository, Yarn is installed system-wide and every system user can access it.
First, import the GPG key used to verify the Yarn packages before installation.
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
Next, enable the Yarn package manager repository:
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
Then run the following commands to install the Yarn package manager:
sudo apt update && sudo apt install yarn
When the installation is complete, check the installed Yarn version
.
yarn --version
1.22.4
2. Install Yarn with NPM
The Yarn package manager is also available as an npm package. You can install it by running:
npm install -g yarn
The "-g" option installs Yarn globally on your system. This way you can access it from anywhere on your system or from any project.
Install Yarn with a Script
You can also use the bash script provided by the Yarn team to install Yarn. The script installs Yarn in the user's home directory. This means that when you install Yarn with this script, it will be available only to the current user.
To install the Yarn package manager with the script, type:
sudo apt install curl -y
curl -o- -L https://yarnpkg.com/install.sh | bash
The installer configures the Yarn environment in the user's .bashrc file. You can reload this file to load the environment:
source ~/.bashrc
That's it. Yarn has been installed successfully.