Yarn is a rapidly growing JavaScript package manager that was developed by Meta (formerly known as Facebook) for the Node.js JavaScript runtime environment. It offers a bunch of features compared to the standard NPM package manager, followed by.

  • Fast performance and support for parallel installation.
  • Caches the downloaded packages to avoid re-downloading.
  • Uses "lockfiles" for dependency tracking.
  • Verifies licenses for all dependent modules.
  • Actively growing community.

Since the release of NPM 5, the Yarn "lockfiles" feature has been added to NPM as "package-lock.json", which is automatically generated and updated when new packages are installed via the "npm" command.

The main difference between Yarn and NPM, in my personal experience, lies in how they handle package downloads: Yarn installs packages parallelly, resulting in fast installations of large files, whereas NPM installs packages sequentially, causing slowdowns with large files.

Despite this, Yarn performs security checks while downloading packages, using the package license information and checksums to avoid downloading dangerous scripts, and caching the downloaded package locally to prevent repeated downloads in the future.

I must suggest trying out Yarn. In this quick guide, I'll show you how to install Yarn on Ubuntu 24.04, although the same method would work for Ubuntu 23.04 and other versions.

How to Install Yarn on Ubuntu

Multiple methods exist for installing Yarn on Ubuntu, including through the NPM package manager, Corepack, and the APT repository. The first method installs a stable (but older) version of Yarn, while the other two methods allow you to install the latest or specific version of Yarn.

You can select the one according to your needs, but if you're a beginner having trouble deciding, I'd recommend going with the first method to install Yarn via NPM.

Method 1: Install Yarn via NPM Package Manager

Yarn is a JavaScript package manager for JavaScript projects that uses the Node.js runtime environment. You can install it quickly via the Node Package Manager. But first, make sure you have NPM installed; if not, execute the following command to install it.

$ sudo apt install npm

Next, install Yarn using the "npm" command with the "-g" flag to allow it to be installed globally, which provides the privilege of accessing it from any directory of the system:

$ sudo npm install -g yarn

When done, verify the installation by checking the Yarn version.

$ yarn --version
yarn installed via npm command

Uninstall

To uninstall Yarn installed via the NPM package manager, run:

$ sudo npm remove -g yarn

Method 2: Install Yarn via Corepack

Corepack is a tool that allows us to use third-party package managers like Yarn and pnpm without needing to install them; it comes preinstalled with the latest version of Node.js and only needs to be enabled, but if it's not installed, you can install it with this command:

$ sudo npm install -g corepack

After the installation, you must enable Corepack by running:

$ corepack enable

With Corepack installed, you can either activate all package managers or a specific version of a package manager. To enable the stable version of Yarn, you can run.

$ corepack prepare yarn@stable --activate

You can now access Yarn via your terminal; start by checking its version.

$ yarn --version
yarn installed via npm corepack

Uninstall

To remove Yarn installed via Corepack, disable and remove it by running the following two commands:

$ corepack disable
$ sudo npm uninstall -g corepack

Method 3: Install Yarn via APT Repository

Yarn officially provides an APT repository for stable, release candidate, and nightly versions of Yarn, allowing you to install the needed Yarn version using the "apt" command. To start, add the GPG key using the following command:

$ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -

Then execute only one of the following commands to add Yarn's stable, release candidate, or nightly APT repository. If unsure, pick the stable one.

# Stable Version
$ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

# Release Candidate
$ echo "deb https://dl.yarnpkg.com/debian/ rc main" | sudo tee /etc/apt/sources.list.d/yarn.list

# Nightly Version
$ echo "deb https://nightly.yarnpkg.com/debian/ nightly main" | sudo tee /etc/apt/sources.list.d/yarn.list

Lastly, update the package database and install Yarn using this command.

$ sudo apt update && sudo apt install yarn

After installation, make sure to verify the Yarn version that's been installed.

$ yarn --version
yarn installed via apt repository

Uninstall

To uninstall Yarn installed from a custom APT repository, execute these commands to remove both Yarn and the added repository.

sudo apt remove --autoremove yarn
sudo rm /etc/apt/sources.list.d/yarn.list

I hope you were able to successfully install Yarn on your Ubuntu system. If you are experiencing difficulties, please don't hesitate to reach out to us via the comment section.