# How to install Yarn on Ubuntu 20.04?

{% embed url="<https://youtu.be/lwmce-Wv_sY>" %}

### **Overview**  <a href="#howtoinstallyarnonubuntu20.04-overview" id="howtoinstallyarnonubuntu20.04-overview"></a>

Yarn refers to a lightweight and configurable package manager for Node.js that supports both binary and source packages and can easily be embedded into your own Node application. It is often used by JS developers to manage their dependencies and avoid version complexity needed to install new versions of JavaScript packages.&#x20;

This tutorial will demonstrate how to install and use Yarn on Ubuntu 20.04.&#x20;

### **Prerequisites** <a href="#howtoinstallyarnonubuntu20.04-prerequisites" id="howtoinstallyarnonubuntu20.04-prerequisites"></a>

There are certain prerequisites that need to be met before you begin.&#x20;

* Ubuntu 20.04 LTS configured on your system.
* Non-root sudo user privileges.

### **Get Started** <a href="#howtoinstallyarnonubuntu20.04-getstarted" id="howtoinstallyarnonubuntu20.04-getstarted"></a>

#### **Step 1: Installation** <a href="#howtoinstallyarnonubuntu20.04-step1-installation" id="howtoinstallyarnonubuntu20.04-step1-installation"></a>

* Add the repository's GPG key:&#x20;

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

![](http://customer.acecloudhosting.com/index.php?rp=/images/kb/890_image-2023-2-15_16-9-56.png)

* Import the Yarn APT repository:&#x20;

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

![](http://customer.acecloudhosting.com/index.php?rp=/images/kb/891_image-2023-2-15_16-10-44.png)

* After successful addition of key and the repository, update the package index and begin the installation of Yarn:

```
sudo apt update && sudo apt install yarn
```

{% hint style="info" %}
**Note**: The command in the previous step includes the Node.js installation. &#x20;
{% endhint %}

* You can skip the installation by running the following command in case you have already installed Node using npm:

```
sudo apt install --no-install-recommends yarn
```

* Print the Yarn version to verify the installation:

```
yarn --version
```

![](http://customer.acecloudhosting.com/index.php?rp=/images/kb/892_image-2023-2-15_11-8-55.png)

{% hint style="info" %}
**Note:** Your system's version of Yarn might be different. &#x20;
{% endhint %}

The installation of Yarn is successful.

#### **Step 2: Project Creation** <a href="#howtoinstallyarnonubuntu20.04-step2-projectcreation" id="howtoinstallyarnonubuntu20.04-step2-projectcreation"></a>

Once the installation is complete, you can explore yarn commands to initiate a new project with yarn.&#x20;

* First, create a directory for your application with the respective name:

```
mkdir ~/yarn_project
```

* Enter into that directory:

```
cd ~/yarn_project
```

* Initialize an entirely new project of Yarn after which you will be asked a series of prompts. You can either enter specific information or press **'Enter'** to accept the defaults:

```
yarn init yarn_project
```

![](http://customer.acecloudhosting.com/index.php?rp=/images/kb/893_image-2023-2-15_16-16-20-1.png)

Once you have responded to all the prompts, the script creates a *package.json* file with the data you have entered.

#### **Step 3: Manage D**ependencies <a href="#howtoinstallyarnonubuntu20.04-step3-managedependencies" id="howtoinstallyarnonubuntu20.04-step3-managedependencies"></a>

This section will demonstrate how to add, remove and install the project dependencies.&#x20;

* Import a fresh package dependency:

```
yarn add react
```

{% hint style="info" %}
**Note:** You can add any other package using the syntax: **`yarn add package_name'`**
{% endhint %}

<figure><img src="http://customer.acecloudhosting.com/index.php?rp=/images/kb/894_image-2023-2-15_16-18-14.png" alt=""><figcaption></figcaption></figure>

The above command will install the latest version of the package specified. To install a particular version of the package, follow this command:

```
yarn add react@18.0.0
```

{% hint style="info" %}
**Note:** You can add any other package with a specific version using the syntax: **`yarn add [package_name]@[package_version]`**
{% endhint %}

<figure><img src="http://customer.acecloudhosting.com/index.php?rp=/images/kb/895_image-2023-2-15_11-28-0.png" alt=""><figcaption></figcaption></figure>

* To upgrade the project dependency, use the following command:

```
yarn upgrade
```

This command will automatically upgrade all project dependencies and packages to the most recent version based on the range specified in the `package.json` file.

* To remove the project dependency, use the following command:

```
yarn remove react
```

<figure><img src="http://customer.acecloudhosting.com/index.php?rp=/images/kb/898_image-2023-2-15_11-28-49.png" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
**Note:** You can remove any other package that you have added using the syntax: **`yarn remove package_name`**
{% endhint %}

* Run the following command to install all project dependencies listed in the *`package.json`* file:

```
yarn install
```

<figure><img src="http://customer.acecloudhosting.com/index.php?rp=/images/kb/897_image-2023-2-15_16-23-44.png" alt=""><figcaption></figcaption></figure>

### Conclusion <a href="#howtoinstallyarnonubuntu20.04-conclusion" id="howtoinstallyarnonubuntu20.04-conclusion"></a>

In this tutorial, you have learned how to install and utilize Yarn and its commands on Ubuntu. With Yarn, you get a centralized command for installing packages in your project and dependencies are installed much faster than before. To know more about the Yarn subcommands, access the[ official Yarn documentation](https://yarnpkg.com/cli/add).&#x20;
