Install and Use Yarn on Ubuntu 22.04 | Easy Steps

Posted on

Install and Use Yarn on Ubuntu 22.04 | Easy Steps

This tutorial on the Orcacore website aims to guide you through the process of how to Install and Use Yarn on Ubuntu 22.04. Yarn (Yet Another Resource Navigator) is a JavaScript package manager designed to automate the installation, configuration, updating, and removal of npm packages. Developed by Facebook, Yarn mirrors the functionality of npm but emphasizes speed, security, and consistency. It integrates seamlessly with the npm registry, offering a streamlined alternative to the standard npm client.

To follow along with this guide, you’ll need a non-root user with sudo privileges on your Ubuntu 22.04 server. If you haven’t set this up yet, you can refer to our guide on Initial Server Setup with Ubuntu 22.04.

1. Installing Node.js for Yarn

Yarn relies on Node.js. Therefore, you need to install Node.js on your server. Start by updating your local package index using the following command:

sudo apt update

Next, install the necessary packages:

sudo apt install software-properties-common apt-transport-https wget ca-certificates gnupg2 gcc make g++ curl -y

Now, import the NodeSource repository. You can choose between the current release or the LTS (Long Term Support) version, depending on your needs:

# curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
# curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo bash -

2. Add Yarn GPG Key and Repository

With Node.js installed, you can proceed with the Install and Use Yarn on Ubuntu 22.04 process. First, import the Yarn package manager GPG key:

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null

Then, add the Yarn repository:

echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

Update the system again:

sudo apt update

3. Install Yarn Package Manager on Ubuntu 22.04

Now you’re ready to install both Yarn and Node.js. Execute the following command:

sudo apt install yarn nodejs -y

Verify your Yarn installation by checking its version:

yarn -v
**Output**
1.22.19

Alternatively, you can use:

apt-cache policy yarn
Verify Yarn installation Ubuntu 22.04

4. How To Use Yarn Command?

Let’s explore how to use Yarn by creating a new project and managing dependencies.

To create a new Yarn project, use the yarn init command.

For example, to create a project named my_project:

yarn init my_project

This command will prompt you for information about your project. Press Enter to accept the defaults or fill in the details.

The output will look similar to this:

This command creates a basic package.json file containing the provided information. You can modify this file as needed.

To initialize a Yarn project in an existing directory, navigate to the directory and run:

yarn init

To add a package as a dependency, use the following command:

yarn add [package_name]

This installs the package and its dependencies, updating the project’s package.json and yarn.lock files. By default, Yarn installs the latest version.

To install a specific version or tag:

yarn add [package_name]@[version_or_tag]

To upgrade packages:

$ yarn upgrade
$ yarn upgrade [package_name]
$ yarn upgrade [package_name]@[version_or_tag]

Without a package name, the command updates all project dependencies to the latest versions allowed by the version ranges in package.json. Otherwise, only the specified packages are updated.

To remove a package from your project:

yarn remove [package_name]

This command updates the package.json and yarn.lock files.

To install all dependencies specified in the package.json file in an existing project:

yarn

Or:

yarn install

Conclusion

Yarn is gaining popularity due to its performance improvements, ease of installation, and helpful features. With this guide, you should now know how to Install and Use Yarn on Ubuntu 22.04.

Please subscribe to us on Facebook, Instagram, and YouTube.

Also, you may like to read the following articles:

Introducing Ubuntu 25.04 Plucky Puffin

Top 5 Song Translation Websites

Free AI Photo Editing Tools in 2025

Discover Quick Settings in Android 16

Alternative Solutions for Installing Yarn on Ubuntu 22.04

While the above instructions provide a solid method for installing Yarn, here are two alternative approaches:

1. Using the Corepack Package Manager (Recommended for Node.js 16.17+)

Corepack is a tool that ships with Node.js starting from version 16.17 and is designed to manage package managers like Yarn and pnpm. This is now the officially recommended way to manage Yarn, and it simplifies the process considerably.

  • Explanation: Corepack acts as a proxy, automatically detecting the required package manager version (specified in the project’s package.json or a global setting) and installing it if it’s not already present. This eliminates the need to manually add repositories or GPG keys.

  • Steps:

    1. Enable Corepack:

      corepack enable
    2. Ensure Yarn is installed (globally or locally): Corepack will handle this automatically if needed. If you want to pre-install it:

      corepack prepare yarn@latest --activate

      This command fetches the latest version of Yarn and prepares it for use. The --activate flag ensures that the prepared version is used.

    3. Use Yarn: Now you can use the yarn command as usual. Corepack will ensure that the correct version is available.

    • Why this is better: Corepack simplifies dependency management and ensures consistency across projects. It is also the officially recommended approach from the Node.js team.

2. Installing Yarn with npm (if you already have npm)

While it might seem counterintuitive to use npm to install Yarn, it’s a valid method, especially if you already have Node.js and npm set up. This method installs Yarn globally using npm.

  • Explanation: This method leverages the existing npm installation to download and install the Yarn package. It’s straightforward but might not be the most recommended approach due to potential version conflicts or dependency issues if not managed carefully.

  • Steps:

    1. Ensure Node.js and npm are installed: If you followed the original guide, you already have this. If not, install Node.js using your preferred method.

    2. Install Yarn globally using npm:

      sudo npm install -g yarn

      The -g flag installs Yarn globally, making it accessible from any directory.

    3. Verify Installation:

      yarn -v
    • Considerations: This method relies on npm, so any npm configuration or proxy settings will affect the Yarn installation. Also, managing Yarn’s version independently from npm might become complex over time. This method is not recommended as much as Corepack.

By exploring these alternative methods, you gain a broader understanding of the options available for installing and using Yarn on Ubuntu 22.04. The best approach depends on your specific needs and environment. Remember that Install and Use Yarn on Ubuntu 22.04 is easier now than ever before.