Home Yarn How To Install And Use Yarn Package Manager In Linux

How To Install And Use Yarn Package Manager In Linux

By sk
Published: Updated: 3.6K views

In this tutorial, we will discuss what is Yarn, how to install Yarn package manager in Linux and finally we will take a look at the basic usage of Yarn.

Introduction to Yarn

Yarn, stands for Yet Another Resource Navigator, is a modern package manager to install and manage JavaScript programs in Linux, macOS and Windows operating systems.

Yarn is a fast, secure and reliable package manager. Yarn is fully compatible with the npm registry and can work alongside npm package manager.

It does everything concurrently to maximize resource utilization and caches every downloaded packages. Hence there is no need to download the same package over and over. Yarn is also secure by design, because it makes use of checksums before installation to ensure the integrity of each package.

By default, Yarn supports Node. You can also extend the support to other programming languages using plugins. Even though, Yarn command usage is similar to Npm, it has a few additional commands. For instance, it has "yarn why" command which tells you why a certain package is installed.

Yarn was initially developed by Facebook to address and fix performance and security concerns with npm. Now Yarn is an independent open source project tied to neither Facebook nor any other company.

Install Yarn package manager in Linux

Yarn is available in the default repositories of my many Linux distributions. However, the officially recommended way to install Yarn is using Npm, the Nodejs package manager.

Npm will be installed by default with Nodejs installation. To Nodejs on Linux, refer the following guide:

After installing Nodejs, use npm to install Yarn on your Linux machine:

$ npm install -g yarn

Check the installed Yarn version:

$ yarn --version

As you can see in the above output, the installed Yarn version is 1.22.10.

Upgrade Yarn 1.x to Yarn 2

The developers of Yarn recommends to migrate from Yarn 1.x version to 2.x version to get a fast and stable Yarn experience.

To migrate Yarn 1.x to Yarn 2, go to your project folder and run the following command to upgrade to Yarn 2 version:

$ yarn set version berry

Here, berry is the code name to Yarn 2 release version. You can check the version using command:

$ yarn --version

You can also use the following command to permanently upgrade Yarn 1.x version to Yarn 2.x version:

$ yarn set version latest

Check if Yarn is successfully upgraded or not using command:

$ yarn --version

Great! Yarn has been migrated to 2.x release. As of writing this guide, the latest Yarn release was 2.4.1.

Getting started with Yarn

We have installed Yarn and upgraded it to latest version. Now, let us learn a few basic Yarn commands.

1. Display help section

To view the list of of available Yarn commands, refer the Yarn help section by running the following command:

$ yarn help

This command will list all Yarn commands including their options/flags along with a small description of each command. You can also display help section of a any sub-command like below:

$ yarn add -h


$ yarn add --help

I am really impressed by the Yarn help section. The help section of each Yarn sub-command is neatly written with example commands. All commands' help section is very detailed and all options are explained thoroughly. I guess you don't need any online documentation. Everything can be get from offline.

2. Start a new project

Run the following command to start a new project:

$ yarn init

This command will create a new project with the name of the current user.

Sample output:

   name: 'ostechnix'

3. Install dependencies

To install all necessary dependencies for our new project, run:

$ yarn


$ yarn install

4. Install packages

We can install additional packages or dependencies using yarn add command. There are several ways to add a new package using yarn command.

To add a regular package, for example lodash, to the current workspace, run:

$ yarn add lodash

You can add a specific version of a package to the current workspace with command:

$ yarn add lodash@1.2.3

You can also add a package from the master branch of a GitHub repository to the current workspace using a URL:

$ yarn add lodash@https://github.com/lodash/lodash

Or using GitHub protocol like below:

$ yarn add lodash@lodash/lodash

If you want to add a different category of a dependencies, use --dev flag to add dev dependencies and --peer to add peer dependencies.

$ yarn add lodash --dev
$ yarn add lodash --peer

Like I already said, we can display the help section of a Yarn sub-command using -h/--help flag:

$ yarn add -h

5. Upgrade packages

To upgrade a package using Yarn, run:

$ yarn up lodash

6. Display package information

To display information related to a package, run:

$ yarn info lodash
└─ lodash@npm:4.17.21
└─ Version: 4.17.21

If you want to show all available information about a package using yarn, the command would be:

$ yarn npm info lodash

7. Execute a shell command

We can execute a shell command with yarn exec command.

For example, let us display a Hello World text with Yarn like below:

$ yarn exec echo Hello World
Hello World

Display Linux Kernel version with Yarn command:

$ yarn exec uname -mrs
Linux 5.4.0-72-generic x86_64

8. Display why a package is installed

This is one of the unique feature of Yarn. Yarn can display the reason why a package is installed.

$ yarn why lodash

9. Display Workspaces

The another notable feature of Yarn is Workspaces. Workspace allows users to install dependencies from multiple package.json files in subfolders of a single root package.json file, all in one go.

To print all available workspaces, run:

$ yarn workspaces list

10. Display current configuration settings

To print the current active configuration settings, run:

$ yarn config

11. View plugins

To print the plugins available directly from the Yarn repository, run:

$ yarn plugin list

The above command displays all available official plugins. If you want to display only the currently active plugins, run:

$ yarn plugin runtime

12. Download plugins

We can download and activate plugins from the official Yarn repository or local or third party repositories. If you add a plugin from third-party repository, enter the plugin's URL. If you add the plugin from your local system, simply enter its path.

To download and activate plugin, for example plugin-version, from Yarn repository, run:

$ yarn plugin import @yarnpkg/plugin-version

13. Remove plugins

To remove a plugin imported from the Yarn repository, run:

$ yarn plugin remove @yarnpkg/plugin-stage

If you have imported it from the local system, simply mention its name.

14. Remove packages

To uninstall a package using Yarn, run:

$ yarn remove lodash

15. Remove cache

To remove all the local archives, run:

$ yarn cache clean

You can also remove all the archives stored in the ~/.yarn directory using command:

$ yarn cache clean --mirror

These are just enough to get started with Yarn package manager. However, there are many commands available. I will leave them to you to learn and experiment. You can view the complete list of Yarn commands using yarn --help command. If you don't know the usage of a specific sub-command, just run "yarn sub-command -h" to bring up that particular command's help section.


You May Also Like


Aliason May 12, 2021 - 2:53 pm

Thanks for the guide.

sk May 12, 2021 - 2:55 pm

You’re welcome. Glad it helped.

geek May 22, 2021 - 11:08 pm

Magizhchi Nanba!!

sk May 23, 2021 - 7:14 pm


joe July 2, 2021 - 8:10 am

pgp key import fails on Fedora 34 Workstation?

sk July 2, 2021 - 11:55 am

Can you be more specific? When did you get this error? While installing Yarn or installing packages using Yarn?


Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More