How to Install Yarn on Ubuntu 18.04
3 min read

How to Install Yarn on Ubuntu 18.04

In this tutorial, you will install Yarn on Ubuntu 18.04.Yarn is a JavaScript package manager compatible with the npm which helps to automate the processes like installing, and updating, configuring and removing the npm packages.
How to Install Yarn on Ubuntu 18.04

Introduction

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

Yarn is a JavaScript package manager compatible with the npm which helps to automate the processes like installing, and updating, configuring and removing the npm packages. It is useful to solve a set of problems with npm like speeding up of package installation by parallelizing the operations. It further, helps in reducing errors related to network connectivity. Also, The official Yarn repository has constant maintenance and provides the most up-to-date version.

In this tutorial, you will install Yarn on Ubuntu 18.04 system. We will also address few FAQs related to Yarn installation.

Prerequisites

1) Make sure to log in as a user with the sudo privileges.

Installing Yarn on the Ubuntu

1) First you need to enable the Yarn repository. Start by importing the repository’s GPG key using the below curl command:

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

2) Continue to adding the Yarn APT repository to the system’s software repository list. Do it by typing:

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

3) After there is an addition of repository to the system, update the package list and install Yarn, by:

sudo apt update
sudo apt install yarn

4) If you do not have Node.js installed on your system, the above command will install it. Those using nvm can skip the Node.js installation, by:

sudo apt install --no-install-recommends yarn

5) You will now need to verify Yarn's successful installation. Run the below command, it will print the Yarn version number:

yarn --version

6) Here, the latest version of Yarn is version 1.17.3:

Output

1.17.3

Using Yarn

Creating a New Project

1) Next, you will create a new project. So, use the yarn init command as below:

yarn init my_yarn_project

2) Then, the init script will ask you few questions. You can either answer or press enter  key. It will enable you to use the default values:

Output

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

3) The script will create a basic package.json file. It contains the information you earlier provided. You can open and modify the file later.

Adding Dependency

1) Now, in case you want to use another package in your project then you will need to add it to project dependencies. For that, use yarn add command with the package name:

yarn add [package_name]

2) The command above will update the package.json and yarn.lock files as well. So anyone working on this project, while running yarn, will get the same dependencies. You will even specify the package version or package tag, by:

yarn add [package_name]@[version_or_tag]

Upgrading Dependency

1) To upgrade the dependency packages, use one of the below commands:

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

If no package name is given, the command will update all the project dependencies. It will give the latest version as per the version range given in the package.json file otherwise, only the specified packages will get updated.

Removing Dependency

1) Next, use yarn remove command along with the package name, it will remove a dependency:

yarn remove [package_name]

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

2) Next, to install all project dependencies given in the package.json file. Do it by running:

yarn

Or

yarn install

Conclusion

We hope this detailed guide helped you to install Yarn on Ubuntu 18.04.

If you have any queries, please leave a comment below and we’ll be happy to respond to them for sure.