JavaScript Ubuntu

How to Install Yarn on Ubuntu 20.04

Yarn is a JavaScript dependency manager that automates tasks related to managing npm packages, including installation, downloading updates, and time-bound configuration. Yarn saves the need to re-download every package since it caches each and every one of them. This means that a JavaScript programmer can save a lot of time deferring these routine tasks to Yarn.

Yarn is a reliable, well-maintained, and time-tested utility that will work wonders when it comes to managing software on your system.

This article shows you how to get Yarn up and running on your Ubuntu 20.04 system. Read carefully and follow the instructions below.

Step 1: Install Yarn’s Package Repository

Before installing Yarn, you must add the Yarn ATP repository on your system.

First, verify that the packages are coming from the official sources by adding the repository’s GPG key. Enter the following commands to include the Yarn repository’s GPG key in your system.

To add the Yarn repository with the GPG key, fire up a terminal and enter the following commands:

$ sudo apt update

$ sudo apt install curl

$ curl -sS | sudo apt-key add

Then, to add to the repository, enter the following:

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

The system is now ready to add Yarn to its active programs.

Step 2: Install Yarn on Your Ubuntu 20.04 System

With the above step taken care of, you can now move on to installing Yarn. Check whether your system has Node.js on it. If Node.js is preinstalled, you can custom install yarn by running the following command:

$ sudo apt install --no-install-recommends yarn

Otherwise, if Node.js is not pre-installed, you will need to download the Node.js dependencies with Yarn.

$ sudo apt update && upgrade

$ sudo apt install yarn

This is how you can install Yarn on your Ubuntu 20.04 machine.

Step 3: Verify the Install

With Yarn now installed on your system, all there is left to do is verify that the install was up-to-date. Type in the following commands to begin the installation.

$ yarn -version

This should print out the version of Yarn that your system has, displaying the number and thereby verifying the install.

We will now look at some of the basics to get you started with Yarn.

Step 4: Getting Started with Yarn

Now that you are done with the installation, it is time to familiarize yourself with some fundamental commands used in Yarn.

The commands you will frequently need when using Yarn will include the following.

Start a New Project

Type in the following command to create a directory for your new project:

$ mkdir ~/my_project && cd ~/my_project

Then, create your project with the following line:

$ yarn init my_project

You will be queried about the specifications of your project, which you can answer yourself, or simply use the default values.

The script will make your project’s package.json file to get you started.

Adding and Upgrading Dependencies

You can append an npm package to a file you have created with the following command:

To add an npm package to the project dependencies, use the yarn add command, followed by the package name:

$ yarn add [package_name]

This should update your package.json file. Keep in mind that Yarn installs the latest version when you only add the package name. Type in the following code to update to a version of your choice.

$ yarn add [package_name]@[version_or_tag]

Removing a Dependency

You can also remove a dependency. Type in the following command:

$ yarn remove [package_name]

Note that your project’s package.json and yarn.lock files will also be updated.

Automate Installation of All Dependencies

You can also automate the installation of all project dependencies included in your package.json project file. Type in the following command to do so:

$ yarn install

Wrapping Up

This article discussed the installation of Yarn on the latest version of Ubuntu, 20.04. The article also discussed creating new project files and updating them and their dependencies.

With Yarn installed on your system, you can manage npm packages much efficiently with the Yarn APT repository. Yarn records what version update has worked on which systems and works out the optimum solution to upgrade your dependencies. The official repository for Yarn is regularly updated and maintained to give you the latest versions of the software you download.

To learn more about Yarn, check out their official website.

About the author

Younis Said

Younis Said

I am a freelancing software project developer, a software engineering graduate and a content writer. I love working with Linux and open-source software.