How to install Yarn NPM Client on Ubuntu and Manage Dependencies through it

Yarn is an open-source npm client, developed at Facebook, that has many added benefits than the standard npm client.  With Yarn, you can opt for speed, license checks, robust installs, compatibility with npm, and multiple registries. With Yarn, you can also automate the management of packages or dependencies. This includes installation, upgrade, configuration and removal of these packages.

In this article, we will explain how to install the latest version of Yarn on your Ubuntu through the official Yarn APT repository. We will also tell you how to perform basic package dependency management through Yarn.

We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system.

Yarn Installation

Step 1: Install Curl for adding GPG key for Yarn

In this article, we will be using Curl in order to add the gpg key for the Yarn project.

Open your Ubuntu command line, the Terminal, either through the system Dash or the Ctrl+Alt+T shortcut. Then, enter the following command as sudo in order to install Curl on your system:

$ sudo apt install curl

Please note that only an authorized user can add, remove and configure software on Ubuntu. Enter the password for sudo, after which curl will be installed on your system if it is already not installed.

Step 2: Add Yarn gpg key

Use the following command in order to add the Yarn key which will be used to run a stable version of Yarn on your system:

$ curl -sS | sudo apt-key add -

Add Yarn GPG Key

Tip: Instead of typing the command, you can copy it from here and paste in the Terminal by using the Ctrl+Shift+V, or by using the Paste option from the right-click menu.

Step 3: Add Yarn APT repository to your system

Enter the following command in order to add the Yarn APT repository to your sources.list.d folder. This will help you in installing Yarn and also enabling your system to fetch upgrades and updates from the Yarn Internet repository in the future.

$ echo "deb stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

Add Yarn Debian Repository

Step 4: Upgrade your system’s Repository Index

The next step is to update your system’s repository index through the following command:

$ sudo apt-get update

This helps you in installing the latest available version of a software from the Internet.

apt-get update

Step 5: Install Yarn

Now that you are done with all the prerequisites required to install Yarn, use the following command as sudo in order to install the Yarn package on your system:

$ sudo apt-get install yarn

Install Yarn with apt

The system will prompt you with a Y/n option to proceed with the installation procedure. Enter Y to continue after which Yarn and nodejs will be installed on your system.

Step 5: Verify Yarn Installation

After you have installed Yarn, you can use one of the following two options to ensure that it is indeed installed on your system:

$ yarn --version
$ yarn -v

Check Yarn version

The above output shows that Yarn version 1.13.0, which is the latest version of Yarn, is installed on my system.

Managing NPM Dependencies with Yarn

We all know how important it is to manage dependencies while programming in JavaScript. So let us explore how to create a Yarn project and manage dependencies. This includes:

  • Adding a dependency
  • Upgrading a dependency
  • Upgrading all dependencies
  • Removing a dependency
  • Adding all dependencies from the package.json file

All Yarn packages contain a file, usually in the project root, called package.json – this file holds various metadata relevant to the project. This file is used to give information to Yarn that allows it to identify the project as well as handle the project’s dependencies. It can also contain other metadata such as a project description, the version of the project in a particular distribution, license information, even configuration data – all of which can be vital to both Yarn and to the end users of the package. The package.json file is normally located at the root directory of a Yarn project.

How to Create a New Yarn Project

In order to create a new Yarn project, enter the following command:

$ yarn init [project_name]

For example, I will create a new project by the name of “first_yarn_project” through the following command

$ yarn init first_yarn_project

When you run the command, the system will ask you various questions. The answers to these questions will be saved in the package.json file along with other important information.

You can enter your answers or else choose to skip the unimportant ones by simply hitting Enter.

Create a yarn project

This is how my package.json file looks like when opened in a text editor:

Package json file

For now, there is no “dependencies” section in this file, as no dependency is yet added to my project.

How to Add a Dependency

If you want to add a package as dependency to your Yarn project, you can use the following Yarn command:

$ yarn add [package_name]

For example, you can add Lodash as a dependency through the following command:

$ yarn add lodash

Add dependency to project

You can also add a specific version of a package by using the following syntax:

$ yarn add package@version-or-tag

For example, in order to add version 4.0.0 of Gulp, you can use the following command:

$ yarn add gulp@^4.0.0

Add dependency for a specific package version with Yarn

When you do not specify a package number in the “yarn add” command, Yarn automatically adds the latest version of the package to your system.

Your package.json file will now contain the added dependencies as follows:

Updated package .json file

How to Upgrade a Dependency

If you want to upgrade any of the dependency that you have already added to your project, you can use the following command syntax to upgrade it:

$ yarn upgrade [package_name]

This will update the package to it’s latest available version or according to the version range defined in the package.json file.

For example, if you have added Gulp you can upgrade it as follows:

$ yarn upgrade gulp

Upgrade dependency

If you wish to change the version number of an added dependency, you can do so through the following command syntax:

$ yarn upgrade [package_name]@[version_or_tag]

How to Upgrade all dependencies

The following command lets you upgrade all the dependencies added to your project.

$ yarn upgrade

Upgrade all dependencies with Yarn

The command checks the dependencies list in your package.json file and upgrades each, one by one, with the latest available version.

Remove a Dependency

If you want to remove a package as a dependency from your Yarn project, use the following command syntax:

$ yarn remove <package_name>

For example, the following command will remove Gulp from my project:

$ yarn remove gulp

After you remove a dependency, the dependencies list in your package.json file is also updated. Similarly, the yarn.lock file is also updated accordingly.

Remove dependency from yarn project file

Install all dependencies

You might know that you can also manually edit your package.json file. For example, you can manually add a dependency to the dependencies list in your package.json file. The dependencies that you have manually added to this file can be installed and added to your project when you run one of the following commands:

$ yarn


$ yarn install

Uninstall Yarn

If you ever want to completely remove Yarn from your system, you can do so through the following command as sudo:

$ sudo apt purge yarn

Remove Yarn

This command will remove Yarn along with all the added dependencies you might have added to it.

The following command will remove the Yarn repository from your sources.list.d folder:

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

Remove Yarn repository

If you want, you can even remove the gpg key that you added during installation.

To retrieve the key, use the following command:

$ apt-key list

List apt keys

Try to locate the Yarn package key and note down the last 8 characters.

Then use the following command in order to remove the key:

$ sudo apt-key del 86E50310

Delete Yarn apt key

Alternatively, you can remove the key through the UI through the Software & Updates utility. Open this utility through the system Dash or the Applications list and then open the Authentication tab.

Use Software and Updates instead of cli commands

Select for the Yarn packaging key and click the Remove button in order to remove the key from your system.

So this was all about installing Yarn and performing some basic dependency management through it. To learn more about Yarn, you can find its detailed official documentation at this page: