How to install Yarn in Ubuntu

Endrit Qerreti

Endrit Qerreti

In this tutorial, you will learn how to install Yarn in Ubuntu.

Yarn is a Javascript package manager that allows you to manage javascript packages in your Ubuntu machine.

When it comes to installing Yarn in Ubuntu or Debian based distros, the installation can be done via two different methods.

In the first method, we'll be installing Yarn via the official repository, in the second method we'll be installing Yarn via corepack.

Method 1: Install Yarn in Ubuntu via official repository

Yarn provides its own repository and GPG key that you can use to download Yarn in Ubuntu and debian based distros. To install Yarn via its official repository, simply follow the steps below.

Step 1 - Download Yarn GPG

In order to be able to install Yarn when using the official repository, first you must add Yarn's GPG key in your system.

Downloading Yarn's GPG key in your system, is quite simple, and it can be done by using curl.

To do so, simply run the command below.

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

Step 2 - Setup Yarn Repository

Yarn's GPG key should be added now in your system, that means you can now proceed to setup Yarn's repository in your system.

Now let's create the Yarn's repository file yarnrepository.list in the /etc/apt/sources.list.d/ directory.

Run the command below to do so.

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

Step 3 - Update your system

We added Yarn repository in the system, so it's time to update the package index so that our system can use the new repository.

sudo apt update

Step 4 - Install Yarn

Now that Yarn repository is added and active in your system, you should be able to install yarn by using apt.

sudo apt install yarn -y

Step 5 - Verify Yarn installation

Yarn should be now installed in your Ubuntu system, before you start using Yarn in your projects, ensure that Yarn works.

To do a quick test to see if Yarn is working, simply check Yarn version via the command below.

yarn --version

Method 2: Install Yarn in Ubuntu via corepack

corepack allows you to install Yarn without having to add the yarn repository manually in your Ubuntu machine.

corepack comes with Node.js, so if you have Node.js installed in your system, then you should be able to install Yarn by using corepack.

If you don't have Node.js installed in your system, then make sure to install Node.js first before proceeding to install Yarn.

Step 1 - Enable corepack

Now proceed to enable corepack, so that you can download Yarn in your system. To do so, simply run the command below.

sudo corepack enable

Step 2 - Install git

git will be used by corepack when downloading Yarn, so in order to be able to install Yarn via corepack, ensure that you have git installed in your system.

If git is not installed in your system, you can easily install it by using apt.

sudo apt install git

Step 3 - cd to Yarn project

Once you have enabled corepack and installed git in your system, now you need to navigate to the project directory where you will be using Yarn.

cd yarn-project

Step 4 - Install Yarn

Finally, install yarn by running the command below.

sudo yarn init -2

Step 5 - Verify Yarn Installation

In this method, we installed yearn via corepack. To verify Yarn installation, simply run the command below to check if Yarn is installed.

yarn --version

Conclusion

In this tutorial, we showed you two different methods how to install Yarn in Ubuntu and any Debian based distro.

In the first method, we used the official Yarn repository to install Yarn.

In the second method, we used the corepack tool to install Yarn without having to setup Yarn repository, this is also the method that Yarn team recommends installing Yarn.