Read in 3 minutes

last updated 

How to install Yarn on Debian 9

Yarn is a JavaScript package manager compatible with npm. It was created to solve a set of problems with the npm such as speeding up the packages installation process by parallelizing operations and reducing errors related to network connectivity.

In this tutorial we will guide you on how to install Yarn on a Debian 9 system via the Yarn APT package repository. The official Yarn repository is consistently maintained and provides the most up-to-date version.

Prerequisites

Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges.

Installing Yarn on Debian

Follow these step to install Yarn on your Debian 9 system:

  1. The first step is to enable the Yarn repository. Start by importing the repository’s GPG key using the following curl command:

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

    Use the following command to enable the Yarn APT repository:

    echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
  2. Once the repository is enabled, update the package index 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
  3. Verify the installation by printing the Yarn version number:

    yarn --version

    At the time of writing this article, the latest version of Yarn is version 1.10.1.

    1.10.1

Using Yarn

Now that you have Yarn installed on your Ubuntu system, the next step is to explore some of the most common Yarn commands.

Creating a new project

To create a new Yarn project use the yarn init command as shown bellow:

yarn init my_yarn_project

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

yarn init v1.10.1
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.

Once completed, the script will create a basic package.json file that will contain the information you provided. You can later open and edit this file.

Adding 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]

Upgrading dependency

To upgrade a dependency use one of the following:

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

The command above will update the project dependencies to their latest version according to the version range specified in the package.json file.

Removing dependency

To remove a dependency use the yarn remove command followed by the package name:

yarn remove [package_name]

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

Installing all project dependencies

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

yarn

or

yarn install

Conclusion

We have shown you how to install yarn on your Debian 9 machine. For more information about yarn visit the Yarn documentation page.

If you have any questions or feedback, feel free to comment below.