Introduction

Yarn is a tool for managing, updating, and sharing your Java code. Yarn works through node.js helping to track libraries, dependencies, and even enables you to share solutions with other developers.

In this tutorial learn how to install Yarn on Ubuntu 18.04.

how to install yarn on ubuntu 18.04

Prerequisites

  • A user account with sudo privileges
  • Access to a terminal window / command line (Ctrl-Alt-T)

Option 1: Install Yarn Using Ubuntu Repositories

Yarn developers maintain a repository for installing directly to Ubuntu that simplifies the installation process. For detailed inustrctions, follow the steps below.

Step 1: Configure the Yarn Repository

Open a terminal window, and add the GPG key:

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

The GPG key ensures that you’re installing authentic software. Next, add the Yarn repository:

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

This adds the Yarn repository to your master repository list.

add yarn repository to your Ubuntu system
Now, your standard package manager can access and manage Yarn.

Step 2: Install Yarn

Update your local repository listings:

sudo apt-get update

Install Yarn:

sudo apt-get install yarn

This command installs Yarn on your system. If you don’t already have node.js installed, your package manager will install it for you.

install yarn from the official repository

To verify a successful installation, display the Yarn version:

yarn ––version

Option 2: Install Yarn Using NPM

Npm stands for Node Package Manager. It’s commonly used to develop and share javascript code.

1. Start by listing the version to check if NPM in installed:

npm ––version

If you don’t have npm, install it by running the following command:

sudo apt install npm

2. To install Yarn with npm:

sudo npm install yarn –g

example of using NPM for installing yarn on Ubuntu

Upon installing, the output should notify you which version of Yarn has been installed. In the example above, it is version 1.17.3.

In many ways, Yarn is very similar to npm. Yarn adds a yarn.lock file, which restricts packages to a specific version. This is especially helpful for maintaining a consistent development environment.

How to Upgrade Yarn Version

To upgrade Yarn to the latest version, run the following command from the terminal:

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

The system will download the Yarn tarball and install the latest version. The image below displays the output you can expect.

upgrading yarn version screenshot example

Basic Yarn Usage

Now that you have Yarn installed, the sections below dive into how to use basic Yarn commands.

Create a New Yarn Project

To create a new project, enter the following:

yarn init my_project

The system will launch a script that will ask questions about the project. Press enter to use the default for each entry.

creating a yarn project

Add a Dependency to Your Project

A package (sometimes called a module) is a self-contained piece of code. It holds all the components needed to run or work on the code. A package may rely on other packages. These are called dependencies, because the package needs them to function.

To Install a dependency, enter the following:

yarn add insert_package_name

The command above adds the latest version of the named package.

command to add dependencies to a yarn project

To install a specific version, enter the following:

yarn add insert_package_name@version

If the package has a tag, simply substitute [version] for [tag]:

yarn add insert_package_name@tag

This process automatically updates the package.json file. It also updates the yarn.lock file. This ensures that everyone using the package is working with the same software.

Upgrade a Yarn Dependency

To upgrade a dependency, use the command:

yarn upgrade insert_package_name

Like the add command, Yarn can be upgraded by version or tag:

yarn upgrade insert_package_name@version
yarn upgrade insert_package_name@tag

Remove a Yarn Dependency

Remove a dependency by entering the command:

yarn remove insert_package_name

remove a dependency from your yarn project

Install Additional Dependencies

Yarn works through the package.json configuration file. This file keeps a record of all the packages required for your project. When you’re working on a code project, you may add a package that has additional dependencies.

To install these dependencies, enter the following:

yarn install

This will update and load all the dependencies for your project configuration.

install additional dependencies for your yarn project

Yarn is set up to allow multiple users, and to control versions. In some cases, a different developer may add a package to the master package.json file. If that’s the case, you should run the yarn install command as soon as possible. This will update your version of the project, and ensure that you’re working in the same environment.

Conclusion

You should now have a good understanding of how to install Yarn on an Ubuntu system. We also covered basic commands for working with Yarn projects.