Apr 6, 2023 4 min read

How to Install Yarn on Ubuntu 20.04

Install Yarn on Ubuntu with this step-by-step tutorial. Learn how to add and remove dependencies quickly and easily.

Install Yarn on Ubuntu 20.04
Table of Contents

Choose a different version or distribution

Introduction

Before, we begin talking about how to install Yarn on Ubuntu 18.04. Let's briefly understand – What is Yarn?

Yarn is a popular package manager for JavaScript projects that was developed by Facebook. It helps developers manage their dependencies more efficiently by allowing them to install, update, and remove packages easily. With Yarn, developers can also take advantage of its caching and parallel installation features to speed up their project builds.

Yarn uses a lockfile to ensure that all team members are using the same versions of packages, which helps prevent conflicts and makes the development process more streamlined. It also provides a command-line interface that is similar to npm, another popular package manager, making it easy for developers to switch between the two.

If you're working on a JavaScript project and want to simplify your dependency management, Yarn is definitely worth considering. Its popularity and ease of use have made it a go-to tool for many developers in the JavaScript community.

In this tutorial, you will install Yarn on Ubuntu 20.04. We will also cover the core Yarn commands and options.

Installing Yarn on Ubuntu

Yarn is very simple to install on Ubuntu. We'll enable the official Yarn repository, import the GPG key for the repository, and then install the package. The repository offers the most recent version and is consistently updated.

Use the following commands to import the repository's GPG key and add the Yarn APT repository to your system:

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

Update the package list after the repository has been enabled, then install Yarn.

sudo apt update
sudo apt install yarn

The above command will also install Node.js. If you installed Node using nvm, you can skip the Node.js installation by using:

sudo apt install --no-install-recommends yarn

After everything is done, print the Yarn version to confirm the installation:

yarn --version

Here is how the output will appear:

1.22.4

It's possible that the version on your machine is different from the one above.

All done! Yarn has been successfully installed on your Ubuntu system, and you can now begin using it.

Using Yarn

Let's examine some of the most popular Yarn commands now that Yarn has been installed on your Ubuntu machine.

Creating a new project

Begin by creating and navigating to a directory for your application:

mkdir ~/my_project && cd ~/my_project

To start a new project, type yarn init:

yarn init my_project

You'll be asked a number of questions by the command. Provide the following information as requested, or accept the defaults:

Output
yarn init v1.22.4
question name (vagrant): vega
question version (1.0.0): 0.0.1
question description: Testing Yarn
question entry point (index.js): 
question repository url: 
question author: vega
question license (MIT): 
question private: 
success Saved package.json
Done in 20.18s.

After finished, the script generates a simple package.json file containing the provided information. You can access and modify this file whenever you want.

Adding dependency

Use the yarn add command and the package name to add a npm package to the project dependencies:

yarn add [package_name]

The yarn.lock and package.json files will be updated by the above command.

By default, Yarn installs the most recent version when only the package name is provided. Use the following syntax in order to install a specific version or tag:

yarn add [package_name]@[version_or_tag]

Upgrading dependency

Use one of the commands below to upgrade the packages:

yarn upgrade
yarn upgrade [package_name]
yarn upgrade [package_name]@[version_or_tag]

When a package name is not specified, the command will update all project dependencies to the most recent version that falls inside the version range defined in the package.json file. Only the packages that are specified are updated otherwise.

Removing dependency

To remove a dependency, use the yarn remove command and the package name:

yarn remove [package_name]

The command will update the project's package.json and yarn.lock files and remove the package.

Installing all project dependencies

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

yarn

or

yarn install

FAQs to Install Yarn on Ubuntu

What is Yarn Install v/s NPM install?

NPM and Yarn differ significantly in terms of security performance. Yarn is faster and more efficient, as it installs packages in parallel, whereas NPM does so sequentially.

Is Yarn secure?

Yes, Yarn is secure as it uses checksums to verify the integrity of packages downloaded from the registry.

Is Yarn free?

Yes, Yarn is free and open source.

Does Yarn have an offline mirroring feature?

Yes, Yarn has an offline mirroring feature, which allows packages to be installed without an internet connection.

How to install Yarn globally?

The NPM package manager, which comes installed by default with all Node.js installations, is advised by the Yarn maintainers for installing Yarn globally. To accomplish this, run npm install with the -g flag:

sudo npm install -g yarn

Conclusion

You now know how to install Yarn on an Ubuntu 20.04. Visit their documentation page to learn more about Yarn.

If you have any suggestions or queries, kindly leave them in the comments section.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.