Reading Time: 5 minutes

What is Yarn?


Yarn is a package management tool expressly designed to speed up the installation of packages from the npm registry by running operations in parallel to increase speed, improve reliability, and boost security. Yarn allows us better to automate the installation processes of npm software packages and is also advantageous when upgrading, making configuration changes, or removing npm packages. 

The Yarn package manager was created by Facebook in collaboration with Google, Tilde, and Exponent, to better manage and integrate javascript code consistency. Because Yarn is compatible with the NPM registry, it caches downloaded package, so there is no need to download the same package twice. Using yarn guarantees that any installation that works on one system will work on another system as well. When it comes to security, yarn checks the integrity of every installed package before executing its code. 

What is NPM?


To contrast yarns abilities, we must look at NPM. NPM is the default javascript package management tool used in conjunction with the NPM registry. It uses a command line interface client along with access to the online database of javascript software. It allowed the sharing of Javascript code with other developers, making it easy for others to reuse common code snippets in their applications. 

With the ever-growing increase in the size of their codebase over the years, Facebook software engineers ran into issues with performance, consistency, and security. They needed a way to install packages more quickly as well as manage dependencies consistently across different environments securely. The answer to this was Yarn.


In this tutorial, we will explain how to install the Yarn package manager via the Yarn repository on your Ubuntu 18.04 system. This repo is well maintained and consistently provides the most up-to-date version available. We will also present some of the basic Yarn commands and options.

Yarn can be installed using the following npm command although it is not recommended. We only mention this to show how it can be installed using nmp.

root@host:~# npm install -g yarn

Ubuntu Install

The preferred method of installation on Ubuntu is using the apt package manager. This option allowing us to separate Yarn from npm, so we do not have to reply on npm. Using the apt package manager also will enable us to keep Yarn updated regularly. Before we begin the installation process, we should be logged in as root or a user with sudo privileges. 

As a side note, if your version of Ubuntu does not have curl installed, we can install it quickly using this command.

root@host:~# apt-get install curl

The first step is to download the security key for the repository. We will use the following curl command to import the GPG key.

root@host:~# curl -sS | apt-key add -

Next, we will enable the Yarn repository itself and add it to our repo list using this command.

root@host:~# echo "deb stable main" | tee /etc/apt/sources.list.d/yarn.list

Once the repository is added to our system, we need to update our package list. 

root@host:~# apt update

Next, we can install the latest version of Yarn. 

root@host:~# apt install yarn
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libc-ares2 libhttp-parser2.7.1 nodejs nodejs-doc
The following NEW packages will be installed:
  libc-ares2 libhttp-parser2.7.1 nodejs nodejs-doc yarn
0 upgraded, 5 newly installed, 0 to remove and 161 not upgraded.
Need to get 6497 kB of archives.
After this operation, 30.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Selecting previously unselected package nodejs-doc.
(Reading database ... 150019 files and directories currently installed.)
Preparing to unpack .../nodejs-doc_8.10.0~dfsg-2ubuntu0.4_all.deb ...
Unpacking nodejs-doc (8.10.0~dfsg-2ubuntu0.4) ...
Selecting previously unselected package yarn.
Preparing to unpack .../archives/yarn_1.22.4-1_all.deb ...
Unpacking yarn (1.22.4-1) ...
Selecting previously unselected package libc-ares2:amd64.
Preparing to unpack .../libc-ares2_1.14.0-1_amd64.deb ...
Unpacking libc-ares2:amd64 (1.14.0-1) ...
Selecting previously unselected package libhttp-parser2.7.1:amd64.
Preparing to unpack .../libhttp-parser2.7.1_2.7.1-2_amd64.deb ...
Unpacking libhttp-parser2.7.1:amd64 (2.7.1-2) ...
Selecting previously unselected package nodejs.
Preparing to unpack .../nodejs_8.10.0~dfsg-2ubuntu0.4_amd64.deb ...
Unpacking nodejs (8.10.0~dfsg-2ubuntu0.4) ...
Setting up nodejs-doc (8.10.0~dfsg-2ubuntu0.4) ...
Setting up libhttp-parser2.7.1:amd64 (2.7.1-2) ...
Setting up yarn (1.22.4-1) ...
Setting up libc-ares2:amd64 (1.14.0-1) ...
Setting up nodejs (8.10.0~dfsg-2ubuntu0.4) ...
update-alternatives: using /usr/bin/nodejs to provide /usr/bin/js (js) in auto mode
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...

If you do not have Node.js installed, the above apt command will install it. Should we want to skip the installation of Node.js, we will use this command. 

root@host:~# apt install --no-install-recommends yarn

To confirm Yarn is installed successfully, we will check the version by running the following command.

root@host:~# yarn --version 

Configure Yarn   

In order to create a new project, we will use this command.

root@host:~# yarn init

The init script will ask some questions, but you don't have to answer those, you can press enter for default values. 

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

Once the init script completes, we will have a basic package.json file with the information provided earlier. You can open and edit this file using vim or nano. We will simply view it using the cat command.

root@host:~# cat package.json
  "name": "",
  "version": "1.0.0",
  "description": "Yarn Test",
  "main": "index.js",
  "author": "Leo",
  "license": "MIT"

If we choose to use a different package, we initially have to add it to our dependencies. This suggests that running yarn add [package-name] will add it into your project easily. 

yarn add [package]
yarn add [package]@[version]
yarn add [package]@[tag]

The command above also will update the package.json and yarn.lock files so that everyone who is working on this yarn project can use the same dependency. 

We can also select different packages from multiple locations. Using the following command.

yarn add package-name 

This will install the package from the primary npm registry unless another location is specified in our package.json file.

Next, the following command will install a package that is local to our system. This command is often useful to check out other packages of ours that have not been published to the registry yet.

yarn add file:/path/to/local/folder 

The following command installs a package from a gzipped tarball, which we might want to share in a packaged format before publishing it.

yarn add file:/path/to/local/tarball.tgz 

The next command installs a symlink to a package that's on our local file system. This is often useful to develop related packages in monorepo (or monolithic repository) environments.

yarn add link:/path/to/local/folder 

This command installs a package from a remote git repository.

yarn add <git remote url>  

This command installs a package from a remote git repository at a selected git branch, git commit, or git tag.

Yarn add <git remote url>#<branch/commit/tag> 

This command installs a package from a foreign gzipped tarball.

Yarn add 

Here, we can add to peer dependencies, optional dependencies and devDependencies respectively:

yarn add [package] --peer
yarn add [package] --optional
yarn add [package] --dev 

To upgrading one of those dependency, we can run one of the following commands.

yarn upgrade [package]
yarn upgrade [package]@[version]
yarn upgrade [package]@[tag]

If the package name is not specified, the command will update the project dependencies to their latest version, consistent with the version range laid out in the package.json file. Otherwise, only the required packages are updated. We can also remove dependencies by simply typing 

yarn remove [package]

To Install all the dependencies of project, we will use this command.


Or, we could use this to update dependencies.

yarn install


Yarn is an excellent option for the package management of our JavaScript code. It allows us to share and use other developers code with from around the world as it provides quick, reliable solutions when developing our software.

If you would like to know more about setting up a development or production environment to use Yarn, give us a call at 800.580.4985, or open a chat or ticket with us to speak with one of our knowledgeable Solutions or Experienced Hosting advisors.

Avatar for Leo Porter

About the Author: Leo Porter

I am a 26 years old Linux Technician with almost 2-year experience, learning new things every day. I love troubleshooting and the empowering feeling you get when you help someone to resolve the issue. I like to spend my free time watching Netflix, training in Krav Maga, cycling, or camping if the weather is nice.

Latest Articles

Controlling PHP settings with a custom php.ini file

Read Article

How to install Puppet Server on Linux (AlmaLinux)

Read Article

Email security best practices for using SPF, DKIM, and DMARC

Read Article

Linux dos2unix command syntax — removing hidden Windows characters from files

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article