How To install yarn on ubuntu 20.04 and Debian Linux

The yarn is an open-source JavaScript package manager (developed by Facebook). It is an alternative or should I say improvement to the popular npm package manager that helps you automate the process of installing, updating, configuring, and removing packages retrieved from a global registry. As per Facebook Developers Yarn is faster, reliable and more secure than npm, which caches every package it downloads so it doesn’t need to download it again. Here the official ways to install Yarn on your Ubuntu 20.04 system via the Yarn APT package repository. Before go ahead make sure you are logged in as a user with sudo privileges.

Note, this instruction mentioned to install yarn is applicable to all version or Ubuntu as valid for Debian and other Debian based Distributions.

The first step is to enable the Yarn repository. Start by importing the repository’s GPG key using the following curl command:

curl -sS | sudo apt-key add –

Add the Yarn APT repository to your system’s software repository list by typing:

echo “deb stable main” | sudo tee /etc/apt/sources.list.d/yarn.list

Installing Yarn on Ubuntu

Once the repository is added to the system, update the package list and install Yarn, with:

sudo apt update

sudo apt install yarn

Once the process completes, verify that Yarn has been installed successfully. You can do that by checking the Yarn version.

yarn –version

Using Yarn (Creating a new project)

To create a new Yarn project use the yarn init command as shown below:

yarn init
yarn init v1.12.3
question name (test_yarn): test_yarn_proect
question version (1.0.0): 0.1
question description: Test Yarn
question entry point (index.js): 
question repository url: 
question author: abhishek
question license (MIT): 
question private: 
success Saved package.json
Done in 82.42s.

It will ask you a number of questions. You can skip the questions r go with the defaults by pressing enter. With this, you get a package.json file of this sort:

  "name": "test_yarn_proect",
  "version": "0.1",
  "description": "Test Yarn",
  "main": "index.js",
  "author": "abhishek",
  "license": "MIT"

Now that you have the package.json, you can either manually edit it to add or remove package dependencies or use Yarn commands (preferred).

Adding dependency

If you want to use another package in your project, you need to add it to the project dependencies.

yarn add <package_name>

For example, if you want to use Lodash in your project, you can add it using Yarn like this:

yarn add lodash
yarn add v1.12.3
info No lockfile found.
[1/4] Resolving packages…
[2/4] Fetching packages…
[3/4] Linking dependencies…
[4/4] Building fresh packages…
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ [email protected]
info All dependencies
└─ [email protected]
Done in 2.67s.

And you can see that this dependency has been added automatically in the package.json file:

  "name": "test_yarn_proect",
  "version": "0.1",
  "description": "Test Yarn",
  "main": "index.js",
  "author": "abhishek",
  "license": "MIT",
  "dependencies": {
    "lodash": "^4.17.11"

By default, Yarn will add the latest version of a package in the dependency. If you want to use a specific version, you may specify it while adding.

yarn add [email protected]

As always, you can also update the package.json file manually.

To upgrade a dependency use one of the following:

yarn upgrade [package_name]
yarn upgrade [package_name]@[version_or_tag]

 And to remove a dependency

yarn remove [package_name]

uninstall yarn

If you ever realized that you don’t need Yarn anymore,

Use the following command to remove Yarn and its dependencies.

sudo apt purge yarn

You should also remove the Yarn repository from the repository list:

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

Hope this post helps you to install Yarn on Ubuntu, Debian, Linux Mint, elementary OS etc.

Source Yarnpkg

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More