How to install Yarn on Ubuntu 20.04?

Overview

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.

This tutorial will demonstrate how to install and use Yarn on Ubuntu 20.04.

Prerequisites

There are certain prerequisites that need to be met before you begin.

  • Ubuntu 20.04 LTS configured on your system.

  • Non-root sudo user privileges.

Get Started

Step 1: Installation

  • Add the repository's GPG key:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
  • Import the Yarn APT repository:

echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
  • 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

Note: The command in the previous step includes the Node.js installation.

  • 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

Note: Your system's version of Yarn might be different.

The installation of Yarn is successful.

Step 2: Project Creation

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

  • 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

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

Step 3: Manage Dependencies

This section will demonstrate how to add, remove and install the project dependencies.

  • Import a fresh package dependency:

yarn add react

Note: You can add any other package using the syntax: yarn add package_name'

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

Note: You can add any other package with a specific version using the syntax: yarn add [package_name]@[package_version]

  • 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

Note: You can remove any other package that you have added using the syntax: yarn remove package_name

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

yarn install

Conclusion

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.

Last updated