How to Install and Use Yarn on Ubuntu 20.04

install yarn ubuntu

Yarn is an open-source dependency manager for javascript developed by Facebook. It is an alternative to the popular npm package manager. Yarn provides an easier way to automate the process of installing, updating, configuring, and removing packages from the system. Yarn is faster because it caches every package it downloads. So you don’t need to download it again.

In this tutorial, you will learn –

Prerequisites

  • A server running Ubuntu 20.04.
  • A root password is configured on your server.

Install Yarn from Ubuntu Repository

By default, Yarn is not included in the Ubuntu standard repository. So you will need to add the Yarn official repository to the APT source list.

First, install the curl command-line tool with the following command:

apt-get install curl -y

Once installed, add the GPG key and Yarn repository using the command below:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list

Once the repository is added to the APT, update the repository cache and install the Yarn package with the following command:

apt-get update -y
apt-get install yarn -y

Once installed, verify the Yarn version using the command below:

yarn --version

Output:

1.22.5

To remove or uninstall the Yarn from your system, run the following command:

apt-get remove yarn --purge
apt-get autoremove
apt-get clean

Install Yarn Using NPM

If Node.js is already installed in your system. Then, you don’t need to install Yarn from the Ubuntu repository. You can simply install it using the NPM.

You can install the Yarn using the NPM command as shown below:

npm install yarn -g

Once installed, close and reopen the terminal then verify the Yarn version using the command below:

yarn --version

Output:

1.22.10

To remove the Yarn from your system, run the following command:

npm remove yarn -g

Upgrade Yarn to the Latest Version

It is always recommended to upgrade Yarn package to the latest version. You can upgrade it using the Yarn installation script.

You can run the following command to upgrade the Yarn to the latest version:

curl --compressed -o- -L https://yarnpkg.com/install.sh | bash

This will download the Yarn tarball and install the latest version in your system.

Next, close and reopen the terminal then verify the Yarn version using the command below:

yarn --version

Output:

1.22.5

Yarn Basic Usage

In this section, we will show you how to create a Yarn project, and add dependencies to the Yarn project.

First, create a Yarn project using the yarn init command as shown below:

yarn init

You will be asked about the project as shown below:

yarn init v1.22.5
question name (root): 
question version (1.0.0): 
question description: 
question entry point (index.js): 
question repository url: 
question author: 
question license (MIT): 
question private: 
success Saved package.json
Done in 84.06s.

This will create a package.json file in your current directory. You can check it with the following command:

ls -l 

Output:

-rw-r--r-- 1 root root 93 Jan 17 16:07 package.json

Next, you can use yarn add command to install a dependency for your project.

For example, to add an NPM dependency run the following command:

yarn add npm

This will install the latest version of NPM in the project:

If you want to upgrade any dependency to the latest version, run the following command:

yarn upgrade npm

If you want to remove a dependency, run the following command:

yarn remove npm

Yarn uses package.json file to keeps a record of all the packages required for your project. If you want to install all dependencies specified in the package.json file, use the command below:

yarn install

Conclusion

Congratulations! you have successfully installed Yarn on Ubuntu 20.04. I hope you have now enough knowledge of how to install and use Yarn package manager to manage your project dependency. I would also recommend to read my article on How to Install Node.js and NPM on Ubuntu 20.04.

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva