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

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


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

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!

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.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

1 Comment

Leave a Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.