Installing Yarn on Ubuntu 24.04: A Comprehensive Guide

JavaScript, frequently used with the Node.js runtime environment, is essential for building web servers and other applications that require internet access. Package managers are necessary to add functionalities to these applications. Yarn (Yet Another Resource Navigator) is a popular choice, alongside npm (Node Package Manager), for managing JavaScript project dependencies and it supports Ubuntu 24.04. This guide details how to install the Yarn package manager on Ubuntu 24.04.

How to Install Yarn on Ubuntu 24.04

Yarn is an open-source package manager designed for managing JavaScript project dependencies. It simplifies the process of installing, updating, configuring, and removing package dependencies. Yarn offers features like workspaces, offline caching, and parallel installs, which enhance speed, reliability, and security.

Here are four methods to install Yarn on Ubuntu 24.04:

Through Installation Script

This method offers a straightforward way to install Yarn on Ubuntu 24.04, using a bash script file which is directly downloaded and executed from Yarn’s official website.

Step 1: Download and execute the Yarn installation script.

curl -o- -L https://yarnpkg.com/install.sh | bash

Step 2: Verify the installation by checking the version.

Step 3: To make Yarn work properly, it’s necessary to restart your system.

Step 4: To install the pre-release version, use this command:

curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --rc

Step 5: To install the development version, execute:

curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --nightly

Through Corepack

Corepack is a tool that ships with Node.js, acting as a bridge between Node.js projects and package managers. It allows you to use Yarn without a separate installation.

Step 1: Install Corepack using npm, if it’s not already installed.

sudo npm install -g corepack

Step 2: Enable Corepack.

corepack enable

Step 3: Prepare and activate the stable version of Yarn.

corepack prepare yarn@stable --activate

Step 4: Update to the latest version of Yarn.

sudo corepack up

Through Yarn Repository

Installing an application via its repository is useful if the package is not in apt or if the apt version is outdated. This is the case with Yarn. Yarn offers three version types:

  • Stable: The most reliable version with minimal bugs, reliable dependency management, checksums, and fast installation due to cached packages.

  • Release Candidate: A pre-release version for testing new features; bugs are expected.

  • Nightly: A developmental version built from the latest Yarn source code.

Step 1: Add the Yarn GPG key. This command is the same for all versions.

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

Step 2: Fetch the Yarn repository and save it in the sources.list directory. For the nightly version:

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

Step 3: Update the package list and install Yarn.

sudo apt update
sudo apt install yarn

Step 4: To install the stable version, add this repository:

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

Step 5: To install the pre-release version, execute:

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

Note: Yarn can be installed via the default package installer without adding the repository, but the apt version might be old and cause issues.

Through Node Package Manager

Yarn can be installed using npm, but npm should be installed first.

Step 1: Install npm using apt.

sudo apt install npm

Step 2: Install Yarn globally using npm. The -g flag allows access from any directory.

sudo npm install -g yarn

Changing the Yarn Version

You might need to upgrade or downgrade Yarn for compatibility reasons.

Step 1: Use the set version command with the desired version name.

sudo yarn set version stable

Step 2: Alternatively, specify a version number.

sudo yarn set version <version-number>

Removing Yarn from Ubuntu 24.04

The removal process varies based on the installation method.

If installed via Yarn repository:

Step 1: Remove Yarn.

sudo apt remove --autoremove yarn

Step 2: Remove the repository from the sources.list directory.

sudo rm /etc/apt/sources.list.d/yarn.list

Step 3: List all keys to find the Yarn GPG key number.

sudo apt-key list

Step 4: Remove the Yarn GPG key.

sudo apt-key del B01B 86E5 0310

Note: Replace B01B 86E5 0310 with the actual key.

If installed via npm:

Step 1: Uninstall Yarn.

sudo npm uninstall -g yarn

If installed through Corepack:

Step 1: Disable Corepack.

sudo corepack disable

Step 2: Uninstall Corepack.

sudo npm uninstall -g corepack

If installed through a bash file:

Step 1: Locate the installation directory.

yarn global bin

Step 2: Remove the directory to uninstall Yarn.

sudo rm -r .yarn

Yarn is a valuable JavaScript package manager for enhancing applications or servers during development. You can install it on Ubuntu 24.04 using various methods, and easily switch between stable, pre-release, or development versions using the set version command.