Read in 3 minutes

last updated 

How to install Yarn on Ubuntu 18.04

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 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.

Installing Yarn on Ubuntu

Follow the steps below to install Yarn on your Ubuntu system:

  1. The first step is to install the Yarn repository and import the repository’s GPG key. To do so, run the following commands:

    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
  2. Once the repository is added to our system, 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.9.4.

    1.9.4
Advertisement

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 yarn init:

yarn init my_yarn_project

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

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

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

To add a dependency use:

yarn add [package_name]

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]

Removing dependency

To remove a dependency simply type:

yarn remove [package_name]

Installing all project dependencies

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

yarn

Conclusion

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

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