How to Install Yarn on CentOS 7

Updated on

3 min read

Install Yarn on CentOS 7

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

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 show you how to install Yarn on a CentOS 7 system from the Yarn RPM package repository.

Prerequisites

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

Installing Yarn on CentOS

To install Yarn on your CentOS 7 system, follow the steps below:

  1. If you already don’t have Node.js installed on your system, enable the Nodesource repository with the following curl command :

    curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash -

    Install the Node.js package by typing:

    sudo yum install nodejs
  2. The official Yarn repository is consistently maintained and provides the most up-to-date version. To enable the Yarn repository and import the repository’s GPG key issue the following commands:

    curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.reposudo rpm --import https://dl.yarnpkg.com/rpm/pubkey.gpg
  3. Once the repository is added, you can install Yarn, by running:

    sudo yum install yarn
  4. 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.17.3.

    1.17.3

Using Yarn

Now that you have Yarn installed on your CentOS system, we’ll explore some of the most common Yarn commands.

Creating a new project

To create a new Yarn project use the yarn init command followed by the project name. For example, to create a project named my_project you would type:

yarn init my_project

The script will ask you several questions. You can either answer 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.

Once completed, the script will create a basic package.json file that contains the information you provided. You can open and edit this file at any time.

Adding dependency

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

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

If no package name is given, the command will update the project dependencies to their latest version according to the version range specified in the package.json file. Otherwise, only the specified packages are updated.

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 simply type:

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 the dependencies of an existing project that are specified in the package.json file run:

yarn

or

yarn install

Conclusion

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

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