How to Install Yarn on CentOS 8

Posted 

3 min read

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 npm, such as speeding up the packages installation process by parallelizing operations and reducing errors related to network connectivity.

This tutorial will guide you through the installation of Yarn on CentOS 8. We will also cover the basics of how to use Yarn to create a new project and add/remove dependencies.

Installing Yarn on CentOS 8

Perform the following steps as root or user with sudo privileges to install Yarn on CentOS 8:

  1. If Node.js is not installed on your system, install the Node.js package by typing:

    sudo dnf install @nodejs

    At the time of writing, the Node.js version in the Centos8 repositories is v10.x.

  2. Enable the Yarn repository and import the repository’s GPG key:

    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

    The official Yarn repository is consistently maintained and provides the most up-to-date version.

  3. Once the repository is enabled, install Yarn:

    sudo dnf 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.21.1:

    1.21.1

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.21.1
question name (alex): 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.

All that the command does is creating a basic package.json file containing the information you provided. This file can be modified at any time.

You can also initiate a Yarn project in an existing directory. To do so, navigate to the directory and execute:

yarn init

Adding dependency

To add a package as a dependency to your project, run yarn add followed by the package name:

yarn add [package_name]

The command will install the package and any packages that it depends on, and update the project’s package.json and yarn.lock files.

By default, if only the package name is given, Yarn installs the latest version. To install a specific version or tag, use the following syntax:

yarn add [package_name]@[version_or_tag]

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

Removing dependency

To remove a package from the project’s dependencies invoke the yarn remove command followed by the package name:

yarn remove [package_name]

This command also updates 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 8 machine. For more information about yarn visit the Yarn documentation page.

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