How to Install Yarn on Ubuntu 20.04?

Yarn is a JavaScript package installer and dependency manager originally released by Facebook in collaboration with Google.

Yarn comes with changes intended to rival npm being the most popular of its branch. If you are coming from npm, Yarn replaces the existing workflow for the npm client or other package managers while still being compatible with the npm registry.

In addition, Yarn has attracted a lot of attention not only for its creators but also for its change in approach to downloading and installing packages and managing dependencies.

So many are using Yarn as an alternative to npm. So, let’s learn how to install it and see how it works.

Install Yarn on Ubuntu 20.04

There are two methods to install yarn. You can either use the yarn repository or use npm to install it. Both methods are covered below.

Method 1 – Using the Yarn repository for Ubuntu

There is a repository that we can use in Ubuntu 20.04 that guarantees integration with the system as well as being able to update it from the update manager.

So, open a terminal and make sure Ubuntu is up to date.

sudo apt update
sudo apt upgrade

After that, download and add the GPG key from the repository to the system. This process makes the system consider the external repository we are about to add as safe.

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

If you don’t have curl installed, you can install it from the official repositories, by running

sudo apt install curl

Next, add the Yarn repository to the system by running:

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

Now, all that’s left to do is update APT and install Yarn.

sudo apt update
sudo apt install yarn

This will bring many different dependencies. If you already have NodeJS and npm installed, then you may find it convenient to run this command instead of the previous one

sudo apt install --no-install-recommends yarn

All that remains is to verify the installed version with the following command:

yarn --version

Method 2 – Install Yarn on Ubuntu 20.04 using NPM

If you are going to use Yarn, then you should already have NodeJS on Ubuntu 20.04 and npm installed.

In this sense, once they are already installed, just run this command to install Yarn.

sudo npm install --global yarn

This method is the one recommended by the official documentation of the project.

Removing Yarn on Ubuntu 20.04

If you want to uninstall Yarn on Ubuntu 20.04 you can do it easily from the terminal.

If you installed Yarn using method 1, then you have to uninstall it by running

sudo apt remove yarn

Also, it is recommended to remove the repository from the system.

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

In case you installed Yarn using npm, then you have to uninstall it by running

sudo npm uninstall yarn

This way there will be no trace of it left on the system.

Conclusion

Yarn is an innovative package manager that allows Javascript developers to get their work done quickly and efficiently, especially on large projects.

Scroll to Top