How to Install Yarn on Ubuntu 18.04

Photo of author

By rasho

Yarn is a adavanced package management tool for Javascript applications mostly used for Node.js applications. Yarn is compatible with npm used for installing, configuring, updating and removing npm packages. It helps to solve problems with npm like network connectivity issues, speeding up the installation process etc.
In this tutorial, we will discuss how to install how to install Yarn on your Ubuntu 18.04 system via the Yarn APT package repository. The official Yarn repository is consistently maintained and provides the most up-to-date version. We will also go through the basic Yarn commands and options.

Prerequisites

Before you start to install Yarn on Ubuntu 18.04. You must have the non-root user account on your system with sudo privileges.

Installing Yarn on Ubuntu

First you will need to add Yarn APT repository to your system. Then you will install Yarn on Ubuntu.
Now import Yarn repositories GPG key using following command to enable the Yarn repository using following curl command:

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

After importing key add Yarn APT repository using following command:

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

Once the repository is added to the system, update the package list and install Yarn, with:

sudo apt update
sudo apt install yarn

If you already don’t have Node.js installed on your system, the command above will install it. Those who are using nvm can skip the Node.js installation with:

sudo apt install --no-install-recommends yarn

Now confirm installation and check version of Yarn using following command:

yarn --version

The output should look like:

yarn --version
1.15.2

[ads]

Using Yarn

1. Create a new Project

You can create new project using Yarn running following command. This command will also ask you some questions please press ENTER to use default values or type answer.

sudo yarn init new_project

The init script will ask you several questions. You can either answer or press ENTER to use the default values.

sudo yarn init new_project
yarn init v1.15.2
question name (rasho): LinTut
question version (1.0.0): 0.0.1
question description: Testing Yarn
question entry point (index.js):
question repository url:
question author: lintut
question license (MIT):
question private:
success Saved package.json
Done in 19.10s.

[box type=”note” align=”” class=”” width=””]Now it will create package.json file saving information provided by you. You can also edit the information inside file anytime.[/box]

2. Add dependency

If you want to use another package in your project, you need to add it to the project dependencies. To do so, use the yarn add command followed by the package name:

yarn add [package_name]

The command above will also update the package.json and yarn.lock files so anyone working on this project when running yarn will get the same dependencies.
You can also specify the package version or package tag:

yarn add [package_name]@[version_or_tag]

To install all project dependencies that are specified in the package.json file run:

yarn
or
yarn install

3. Upgrading dependency

The basic syntax for upgrading dependency is given below:

yarn upgrade [package_name]
yarn upgrade [package_name]@[version_number_or_tag_name]

The above command will upgrade specific package to latest version in your project then updates yarn.lock and package.json files.

4. Removing dependency

Use the yarn remove command followed by the package name to remove a dependency:

yarn remove [package_name]

This command will also update the project’s package.json and yarn.lock files.

Conclusion

You have successfully learned how to install Yarn on Ubuntu 18.04. If you have any queries regarding this then please don’t forget to comment below.

Leave a Comment