Feb 21, 2023 5 min read

How to Install Yarn on CentOS 7

Install Yarn on CentOS 7 with our easy-to-follow tutorial. Get started with the popular package manager for your JavaScript projects today!

Install Yarn on CentOS 7
Table of Contents

Choose a different version or distribution

Introduction

Before, we begin talking about how to install Yarn on CentOS 7. First, let's understand – What is Yarn?

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

It speeds up the package installation process as it parallelizes the processes and reduces network connectivity errors.

In this tutorial, you will install Yarn on a CentOS 7 system. It will be from the Yarn RPM package repository. We will also address few FAQs related to the Yarn installation.

Here are the top 5 features of Yarn:

  1. Dependency Resolution: Yarn uses a lockfile to manage dependencies, ensuring that each developer has the same package versions installed. This helps prevent conflicts and ensures that the code runs consistently across different environments.
  2. Speed: Yarn is known for its speed, particularly when installing packages. Yarn caches packages on the local machine, which means that subsequent installs can be significantly faster.
  3. Workspaces: Yarn has a feature called workspaces that make it easy to manage multiple packages in a single repository. Workspaces allow developers to share code and dependencies between packages, which can be particularly helpful for monorepos.
  4. Scripts: Yarn allows developers to define custom scripts that can be run using the yarn run command. This is useful for automating common tasks, such as running tests, building the application, or deploying the code.
  5. Offline Support: Yarn has a built-in offline cache that allows developers to work on projects without an internet connection. Yarn can use this cache to install packages even when offline, which can be particularly useful for developers working in remote or low-bandwidth environments.

Prerequisites to Install Yarn on CentOS 7

1) Make sure to log in as a user with sudo privileges.

Install Yarn on CentOS 7

1) If you don't have Node.js installed on your system, you need to enable the Nodesource repository with the following curl command:

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

2) After that, install the Node.js package using the following command:

sudo yum install nodejs

3) There is consistent maintenance of the official Yarn repository. It further, provides the most up-to-date version. To enable the Yarn repository and import the repository’s GPG key, run the below commands:

curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo

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

4) After the addition of the repository, install Yarn by running the following command:

sudo yum install yarn

5) Now, run the below command to confirm if yarn is installed successfully:

yarn --version

Here, the latest version of Yarn is 1.17.3:

Output

1.17.3

Install Yarn with NVM

You can install Yarn with NVM (Node Version Manager) on CentOS 7 by following these steps:

  1. Install NVM by running the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

This will download and run the NVM installation script, which will add the NVM executable to your shell's PATH.

2. Close and reopen your terminal, or run the following command to apply the changes to your current shell:

source ~/.bashrc

3. Install a version of Node.js using NVM. For example, you can install the latest LTS version by running:

nvm install --lts

4. Install Yarn using npm (which is installed with Node.js):

npm install -g yarn

5. Verify that Yarn is installed by running:

yarn --version

This should print the version number of Yarn.

That's it! You now have Yarn installed with NVM on CentOS 7.

Using Yarn

Now that Yarn is installed, let's explore some common Yarn commands.

Creating a New Project

To create a new project, use yarn init command along with the project name.

yarn init my_project

The script will ask you few questions, either answer or press enter to use default values:

Output

yarn init v1.17.3
question name (vagrant): Vegastack
question version (1.0.0): 0.0.1
question description: Testing Yarn
question entry point (index.js): 
question repository url: 
question author: Vegastack
question license (MIT): 
question private: 
success Saved package.json
Done in 20.18s.

Once completed, package.json file is created by the script, and it contains the information you provided above. You can open the file and edit it at any time.

Adding Dependency

You can use any one of the following commands to upgrade the packages:

yarn upgrade
yarn 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 as per the version range given in the package.json file. Otherwise, only the mentioned packages will be updated.

Upgrade the Dependencies

Use one of the following commands to upgrade the dependencies:

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

Removing the Dependencies

You can remove a dependency using the following command:

yarn remove [package_name]

The above command will also update both package.json and yarn.lock files.

Install all Project Dependencies

You can install all the dependencies of an existing project mentioned in the package.json file using:

yarn

or

yarn install

FAQs to Install Yarn on CentOS 7

How to clean cache in Yarn?

Use the following command to clear cache in Yarn: yarn cache clean

How to install package from Github Repo in Yarn?

Any Git repository of your choice can be added as a dependency to yarn by specifying the remote URL (either HTTPS or SSH):

yarn add <git remote url> installs a package from a remote git repository.

yarn add <git remote url>#<branch/commit/tag> installs a package from a remote git repository at specific git branch, git commit or git tag.

yarn add https://my-project.org/package.tgz installs a package from a remote gzipped tarball.

How to force Yarn to reinstall all packages?

Yes, you can force Yarn to reinstall all packages by running the following command:
yarn install --force 

The --force flag tells Yarn to ignore the existing cache and reinstall all packages. This can be useful if you suspect that there are issues with the installed packages, or if you want to ensure that you have the latest versions of all packages.

Note that this command will remove the existing yarn.lock file and generate a new one based on the latest versions of the packages. This means that the versions of some packages may be updated if a newer version is available in the registry. If you want to reinstall the exact same versions of all packages, you can delete the yarn.lock file before running the command.

Conclusion

We hope this detailed guide helped you to install Yarn on CentOS 7.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.