Install Yarn package manager on Ubuntu

Install Yarn on Ubuntu

Sagar Sharma
Sagar Sharma

Table of Contents

Yarn is a package manager for JavaScript and an alternative to npm.

And like any other modern package manager, Yarn insures the dependencies and packages are installed correctly.

So in this guide, I will show you how you can install Yarn on Ubuntu.

How to install Yarn on Ubuntu

Yarn is not available in the default repository and as per the official documentation, using npm is a recommended way to install yarn.

And in this guide, I will be using the npm package packager.

So let's start with how you can install the npm package manager (if you haven't already).

Use the following command to update the repository index and install npm on Ubuntu:

sudo apt update && sudo apt install npm

Once done, you can check the installed version of npm using the following:

npm --version
check npm version in ubuntu

Once done, you can install the Yarn using the following:

sudo npm install --global yarn

That's it!

Install and remove packages using Yarn

First, create a new directory to store the project using the mkdir command:

mkdir /path/to/new/dir

Here, I have created a new directory named LU:

mkdir LU

Next, change your current working directory to the newly created project directory using the cd command:

cd /path/to/dir

Here, you'd have to initialize the new project using the following command:

yarn init

Here, it will ask you simple questions related to new project, you can also choose the default ones by pressing the Enter key:

initialize the new project

Once done, you can use the following command syntax to install the new package:

yarn add [package-name]

For example, here, I installed a package named lodash:

yarn add lodash
install packages using yarn in ubuntu

Installing a specific version of the package using Yarn

To install the specific version of a package, you'd have to make a slight change in the above command by adding the version number:

yarn add [package-name]@[version-number]

For example, if I want to install lodash version 4.17.21, then, I will have to execute the following command:

yarn add [email protected]

Remove packages using Yarn package manager

To remove a package, you'd have to use the following command syntax:

yarn remove [package-name]

So let's say I want to remove a recently installed package lodash, then I will be using the following:

yarn remove lodash
remove yarn package from ubuntu