How to Install Yarn on Ubuntu

Introduction

Yarn is a tool for managing JavaScript runtime environments. Yarn uses Node.js to track libraries and dependencies and lets you share solutions with other developers.

This tutorial shows you how to install the latest Yarn version and the classic 1.x version on Ubuntu.

How to install Yarn on Ubuntu.

Prerequisites

Install the Latest Yarn Version

The recommended way to install and manage the latest Yarn version is through Corepack. This binary is included in the newer Node.js releases and serves as a point of contact between the user and Yarn.

Follow the steps below to install Yarn using Corepack:

1. Ensure your Node.js version is up-to-date.

node -v

Corepack requires Node.js 16.10 or later. If the output shows an older version, update Node.js.

Checking the Node.js version.

2. Start Corepack by typing:

corepack enable

Note: If Corepack does not exist on your system, install it by typing:

sudo npm install -g corepack

3. Install the latest version of Yarn with the command below:

corepack prepare yarn@stable --activate
Installing Yarn with Corepack.

4. Type the following command to test the installation and check the Yarn version:

yarn --version
Checking Yarn version.

This process installs the latest stable version of Yarn. Run the following command to update the binary to the latest version:

yarn set version stable

Install Classic Yarn Version

The classic versions of Yarn before 2.0 are in maintenance mode, and their support will end soon. However, you can still install Yarn 1.x using the repositories and NPM.

Option 1: Install Yarn Classic Via Repository

Yarn developers maintain a repository that simplifies the installation process for older versions. For detailed instructions, follow the steps below.

1. Add the GPG key:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/yarn.gpg

The GPG key ensures that you are installing authentic software.

2. Next, add the Yarn repository:

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

This command adds the Yarn repository to the master repository list, allowing the package manager to access and manage Yarn.

Add Yarn repository to your Ubuntu system.

3. Update your local repository listings:

sudo apt update

4. Install Yarn:

sudo apt install yarn

This command installs Yarn on your system. If you don’t already have Node.js installed, your package manager will install it for you.

Install Yarn from the official repository.

To verify the installation, check the Yarn version:

yarn ––version
Checking the classic Yarn version.

Option 2: Install Yarn Classic Using NPM

NPM stands for Node Package Manager. It’s commonly used to develop and share JavaScript code. Use NPM to install the classic Yarn version by following the steps below:

1. Check the version to see if NPM is installed:

npm ––version

If you don’t have NPM, install it by running sudo apt install npm.

2. To install Yarn with NPM, enter:

sudo npm install -g yarn
Example of using NPM to install Yarn on Ubuntu.

Yarn is very similar to npm in many ways. Yarn adds a yarn.lock file that restricts packages to a specific version which is especially helpful for maintaining a consistent development environment.

Note: If you want to learn the differences between Yarn and NPM, check our article yarn vs. npm.

Upgrade Yarn from Classic to Latest

Follow the below procedure to upgrade Yarn from the classic to the latest version:

1. Run the npm install command to ensure that the classic Yarn is updated to the latest 1.x version:

sudo npm install -g yarn

2. Switch to the modern Yarn version by typing:

yarn set version berry
Switching to the modern Yarn version.

Note Visit our guide on how to install FFmpeg on Ubuntu, a tool that enables you to work with multimedia content.

Basic Yarn Usage

Now that you have Yarn installed, the sections below dive into how to use basic Yarn commands.

Create a New Yarn Project

To create a new project, go to the project directory and enter:

yarn init

Yarn initializes the project directory by creating two files. One is a package.json file that contains the project configuration, and the other is a yarn.lock file that stores the information about dependencies.

Creating a Yarn project.

Add a Dependency to Your Project

A package (sometimes called a module) is a self-contained piece of code. To function correctly, a package may rely on other packages called dependencies.

To Install a dependency, use the following syntax:

yarn add [package-name]

The command above adds the latest version of the specified package.

Command to add dependencies to a Yarn project.

To install a specific version, enter the following:

yarn add [package-name]@version

If the package has a tag, substitute version with tag:

yarn add [package-name]@tag

This process automatically updates the package.json file. It also updates the yarn.lock file and ensures that everyone using the package is working with the same software.

Upgrade a Yarn Dependency

To upgrade a dependency, use the command:

yarn upgrade [package-name]

Like the add command above, Yarn can be upgraded by specifying a version or tag:

yarn upgrade [package-name]@version

Or:

yarn upgrade [package-name]@tag

Remove a Yarn Dependency

Remove a dependency by using this command:

yarn remove [package-name]
Remove a dependency from your Yarn project.

Install Additional Dependencies

Yarn works through the package.json configuration file. This file keeps a record of all the packages required for your project. When you are working on a code project, you may add a package with additional dependencies.

To install these dependencies, enter the following:

yarn install

This will update and load all the dependencies for your project configuration.

Install additional dependencies for your Yarn project.

Yarn is set up to allow multiple users and control versions. Sometimes, a different developer may add a package to the master package.json file. If that’s the case, run the yarn install command as soon as possible. This updates the project version and ensures that you are working in the same environment.

Note: If you need to use Yarn on Windows, refer to our installation guide How to Install Yarn on Windows

Conclusion

You should now have a good understanding of how to install Yarn on an Ubuntu system. We also covered basic commands for working with Yarn projects to get you going.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Uninstall or Remove Software Packages From Ubuntu
September 4, 2019

This guide will walk you through several methods for removing old or unwanted software from an Ubuntu Linux...
Read more
How to Install Java on Ubuntu
December 8, 2022

Java is one of the most popular programming languages used for developing anything from lightweight mobile to...
Read more
How to Install & Setup MEAN Stack on Ubuntu
March 28, 2024

The MEAN stack is an open-source JavaScript (JS) framework used for developing robust web applications. It is...
Read more
How To Install Node.js & NPM on Ubuntu 18.04
March 18, 2019

Node.js is an open-source cross-platform JavaScript (JS) runtime environment. It is used for building fast...
Read more