How to Install Yarn on CentOS 8

Created by Facebook, Yarn is the coolest and latest package manager for NodeJS which has come to replace npm. While npm works just okay, Yarn ships with some improvements that give it a competitive edge over npm. In fact, developers are now migrating their Node.JS projects over to Yarn.

Recommended Read: 18 Best NodeJS Frameworks for Developers in 2019

So, why are developers so giddy about Yarn?

Firstly, Yarn dwarfs npm in terms of speed of package installation. Yarn is much faster than npm and installs packages simultaneously making it a better choice than npm.

Additionally, when a package is installed, a global cache is installed containing all the dependencies. This does away with the need to going back online to download them again and makes subsequent installation much faster

Secondly, Yarn is considered more secure than npm. This is because it installs packages from the package.json or yarn.lock files.

Yarn.lock guarantees that the same package is installed across all the devices thereby keeping away bugs arising from the installation of different versions. In contrast, npm installs packages from dependencies that raise security concerns due to inconsistencies in the package versions installed.

In this tutorial, you will learn how to install Yarn on CentOS 8. Let’s get started.

Step 1: Install NodeJS in CentOS 8

To begin with log in to your CentOS 8 system as a root user and install the EPEL repository as shown.

# yum install epel-release
Install EPEL Repo on CentOS 8

Install EPEL Repo on CentOS 8

Next, install NodeJS on CentOS 8 using the command.

# yum module install nodejs
Install Nodejs in CentOS 8

Install Nodejs in CentOS 8

To confirm the installation of Node.JS run.

# node -v
# node --version
Check Nodejs Version

Check Nodejs Version

From the output, we have installed Node version 10.16.3.

Step 2: Enable Yarn Repository

After successfully installing Node.js in the previous step, we need to enable the Yarn repository using the following curl command.

# curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | tee /etc/yum.repos.d/yarn.repo
Install Yarn Repo in CentOS 8

Install Yarn Repo in CentOS 8

Next, add the GPG key using rpm command.

# rpm --import https://dl.yarnpkg.com/rpm/pubkey.gpg

Step 3: Install Yarn in CentOS 8

Now install Yarn using the command.

# yum install yarn
Install Yarn in CentOS 8

Install Yarn in CentOS 8

To check the version of Yarn that we have installed, run.

# yarn --version

1.21.1

From the output, we can see that the latest version of Yarn installed is Yarn v. 1.21.1.

Step 4: Create a New Project in Yarn

You can create a new project using the yarn init command and later followed by the project’s name. For example:

# yarn init my_first_project
Create Project in Yarn

Create Project in Yarn

You will be prompted to answer a couple of questions. You can decide to answer Yes or No or simply hit ENTER to proceed to the next question.

A package.json file is created at the end and you can confirm it using ls command as shown.

# ls -l package.json
Confirm package.json File

Confirm package.json File

This file contains all the info you have just provided, and you view the contents of it using cat command.

# cat package.json
View package.json File

View package.json File

Step 5: Install Packages Using Yarn

To install package, simply use the syntax.

# yarn add [package_name]

For example,

# yarn add express
Install Package Using Yarn

Install Package Using Yarn

To remove the package, simply run.

# yarn remove express
Remove Package Using Yarn

Remove Package Using Yarn

Conclusion

Yarn comes with useful benefits that seek to compensate for npm’s shortcomings. It’s much faster, secure and is gradually overtaking npm as Node’s favorite package manager.

With Yarn, you can deploy your projects with ease and comfort while avoiding the inconveniencies that are occasioned with npm. In a nutshell, Yarn is the better of the two. Give it a try and let us know your experience!

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

James Kiarie

This is James, a certified Linux administrator and a tech enthusiast who loves keeping in touch with emerging trends in the tech world. When I'm not running commands on the terminal, I'm taking listening to some cool music. taking a casual stroll or watching a nice movie.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

1 Response

  1. Jalal says:

    Hi,

    Very useful article

    Thanks a lot…

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.