How to install Yarn on Ubuntu 18.04

s2 0

Yarn is an npm compatible JavaScript package manager that helps you automate the process of installing, updating, configuring and removing npm packages.

It is designed to solve a number of problems with the npm such as: B. Speeding up the package installation process by parallelizing operations and reducing errors related to network connectivity.

In this tutorial we will discuss the installation yarn
on your Ubuntu 18.04 system via the Yarn APT package repository. The official yarn repository is consistently maintained and offers the latest version. We’ll also go through the basic yarn commands and options.

requirements

Before proceeding with this tutorial, make sure that you are logged in as a user with sudo permissions.

Installing Yarn on Ubuntu

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

  1. The first step is to activate the yarn repository. Start by importing the repository’s GPG key as follows: curl Command:

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

    Add the Yarn APT repository to your system’s software repository list by entering:

    echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
  2. Once the repository has been added to the system, update the package list and install Yarn with:

    sudo apt updatesudo apt install yarn

    If Node.js is not already installed on your system, the above command will install it. Those using nvm can skip the Node.js installation with:

    sudo apt install --no-install-recommends yarn
  3. To verify that Yarn was installed successfully, run the following commands that will print out the version number of Yarn:

    yarn --version

    At the time of this writing, the latest version of Yarn is Version 1.17.3.

    1.17.3

Use of yarn

After you’ve installed Yarn on your Ubuntu system, the next step is to explore some of the most common Yarn commands.

Create a new project

To create a new project, use the yarn init Command as shown below:

yarn init my_yarn_project

The init script asks you several questions. You can either reply or press enter to use the default values.

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.

When done, the script creates a basic one package.json File with the information you provided. You can open and edit this file later.

Add dependency

If you want to use a different package in your project, you have to add it to the project dependencies. Use the yarn add Command followed by the package name:

yarn add [package_name]

The above command also updates the package.json and yarn.lock Files, so anyone working on this project while it is running yarn get the same dependencies.

You can also specify the package version or the package tag:

yarn add [package_name]@[version_or_tag]

Upgrade dependency

To update the packages, use one of the following commands:

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

If a package name is not specified, the command updates the project dependencies to their latest version based on the version range specified in the package.json file. Otherwise, only the specified packages will be updated.

Remove dependency

Use the yarn remove Command followed by the package name to remove a dependency:

yarn remove [package_name]

This command also updates that of the project package.json and yarn.lock Files.

Install all project dependencies

To install all project dependencies that are included in the package.json File run:

yarn

or

yarn install

diploma

We showed you how to install yarn on your Ubuntu 18.04 machine. For more information on yarn visit the Yarn documentation
Page.

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