How to Install Yarn on Ubuntu 22.04

Choose a different version or distribution

Introduction

Before we begin talking about, how to install Yarn on Ubuntu 20.04, let's briefly understand what Yarn is?

Yarn is a popular package manager for JavaScript projects that was developed by Facebook. It helps developers manage their dependencies more efficiently by allowing them to install, update, and remove packages easily. With Yarn, developers can also take advantage of its caching and parallel installation features to speed up their project builds.

Yarn uses a lockfile to ensure that all team members are using the same versions of packages, which helps prevent conflicts and makes the development process more streamlined. It also provides a command-line interface that is similar to npm, another popular package manager, making it easy for developers to switch between the two.

If you're working on a JavaScript project and want to simplify your dependency management, Yarn is definitely worth considering. Its popularity and ease of use have made it a go-to tool for many developers in the JavaScript community.

In this tutorial, you will install Yarn on Ubuntu 20.04. We will also cover the core Yarn commands and options.

Install Yarn on Ubuntu 22.04

To install Yarn on Ubuntu 22.04, follow the step-by-step instructions provided below.

Step 1: Update system repositories

Open the Ubuntu 22.04 terminal by pressing CTRL+ALT+T then enter the command shown below to update the system repositories:

sudo apt update

Step 2: Install CURL on Ubuntu 22.04

Execute the following command to install CURL after updating the Ubuntu 22.04 system repositories:

sudo apt install curl

You will get an output confirming that the CURL has been installed successfully.

Step 3: Import Yarn GPG key

Then, use the curl command to add the Yarn GPG key to the Ubuntu system repositories:

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

Step 4: Enable Yarn repository

You must enable the Yarn repository on your Ubuntu 22.04 system after importing the GPG key. In order to accomplish this, type the following command into the terminal:

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

Step 5: Install Yarn on Ubuntu 22.04

At this point, your Ubuntu 22.04 system is prepared for Yarn installation if you carefully followed the previous instructions. Execute the following command to do so:

apt update
sudo apt install yarn

Enter y to allow the installation to proceed, then hang on for a few minutes.

Step 6: Check Yarn version

Check the Yarn version installed on your Ubuntu 22.04 system to see if it was installed correctly:

yarn --version

How to upgrade Yarn on Ubuntu 22.04

Want to update Yarn to the newest version? If so, you should first install npm on your system, if you don't already have it:

sudo apt install npm

Finally, use the subsequent command in the Ubuntu 22.04 terminal to download and install the most recent version of Yarn.

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

Yarn on our system has been updated to the most recent 1.22.18 version, as can be seen from the highlighted portion of the output below:

Finally, run yarn with the -version option to ensure Yarn is updated to the most recent version:

export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
yarn --version

We've compiled the most straightforward approach for installing Yarn on Ubuntu 22.04. Furthermore, you are given instructions on how to upgrade your current Yarn to the newest version.

FAQs to Install Yarn on Ubuntu 22.04

What is Yarn Install v/s NPM install?

NPM and Yarn differ significantly in terms of security performance. Yarn is faster and more efficient, as it installs packages in parallel, whereas NPM does so sequentially.

Is Yarn secure?

Yes, Yarn is secure as it uses checksums to verify the integrity of packages downloaded from the registry.

Is Yarn free?

Yes, Yarn is free and open source.

Does Yarn have an offline mirroring feature?

Yes, Yarn has an offline mirroring feature, which allows packages to be installed without an internet connection.

How to install Yarn globally?

The NPM package manager, which comes installed by default with all Node.js installations, is advised by the Yarn maintainers for installing Yarn globally. To accomplish this, run npm install with the -g flag:

sudo npm install -g yarn

Conclusion

You now know how to install Yarn on an Ubuntu. Visit their documentation page for more details about Yarn.

If you have any queries, feel free to post a comment below, and we'll be happy to answer them.