Easy Guide Steps To Install Yarn on Rocky Linux 8 – OrcaCore

Posted on

Easy Guide Steps To Install Yarn on Rocky Linux 8 – OrcaCore

In this comprehensive guide, we’ll walk you through the process of How To Install Yarn on Rocky Linux 8. Yarn, short for Yet Another Resource Negotiator, is a JavaScript package manager developed by Facebook. It serves a similar purpose to NPM (Node Package Manager), providing a robust alternative for installing, uninstalling, and managing project dependencies from the NPM registry or GitHub repositories. Yarn is often favored for its speed, reliability, and enhanced security features. This article will serve as your complete resource for installing Yarn on Rocky Linux 8.

Ready to get started? Follow the step-by-step instructions provided below on the Orcacore website to successfully set up the Yarn package manager on your Rocky Linux 8 system.

Before proceeding, ensure you are logged into your server as a non-root user with sudo privileges. If you haven’t already configured this, refer to our guide on Initial Server Setup with Rocky Linux 8.

1. Install Node.js for Yarn Package Manager

Yarn relies on Node.js to function correctly. Therefore, you must first install Node.js on your server. Let’s begin by updating your local package index. Open your terminal and execute the following command:

sudo dnf update -y

This command synchronizes your package lists with the repositories, ensuring you’re working with the latest information.

Next, install Node.js using the following command:

sudo dnf install @nodejs

This command installs the default Node.js version available in the Rocky Linux 8 repositories.

To confirm that Node.js has been installed successfully, check its version by running:

node --version
**Output Example:**
v16.17.1
Node.js for Yarn Package Manager

Your Node.js version may differ from the example shown above.

2. Install Yarn on Rocky Linux 8

With Node.js installed, you can now proceed to install Yarn. This involves importing the Yarn GPG key and adding the Yarn repository to your Rocky Linux 8 system.

Import Yarn GPG Key

To ensure the authenticity of the Yarn packages, import the Yarn GPG key using the following command:

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

This command adds the key to your system’s RPM database.

Add Yarn Repository

Add the Yarn repository to your system’s package manager configuration using the following command:

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

This command downloads the Yarn repository file and saves it to the /etc/yum.repos.d/ directory.

Update your system repository cache to include the newly added Yarn repository:

sudo dnf update

Finally, install Yarn on Rocky Linux 8 using the following command:

sudo dnf install yarn

Verify the installation by checking the Yarn version:

yarn --version
**Output Example:**
1.22.19

3. How To Use Yarn Package Manager?

Now that Yarn is installed, let’s explore some basic Yarn commands. We’ll create a new project and demonstrate how to add and remove dependencies. To create a new Yarn project, use the yarn init command.

Here, we’ll create a project named my_project. Feel free to choose a different name:

yarn init my_project

This command will prompt you with a series of questions about your project. You can press Enter to accept the defaults or provide your own answers.

The output will resemble the following:

**Output**
yarn init v1.22.19
question name (root): orca
question version (1.0.0):
question description:
question entry point (index.js):
question repository url:
question author:
question license (MIT):
question private:
success Saved package.json
Done in 26.68s.

This command generates a basic package.json file containing the information you provided.

You can modify this file at any time. You can also initialize a Yarn project in an existing directory on Rocky Linux 8. To do so, navigate to the directory and run:

yarn init

Add a Package with Yarn

To add a package as a dependency to your project, use the yarn add command:

yarn add [<package_name>]

This command installs the specified package and its dependencies. It also updates the project’s package.json and yarn.lock files.

If you only specify the package name, Yarn will install the latest version.

To install a specific version or tag, use the following syntax:

yarn add [<package_name>]@[<version_or_tag>]

You can upgrade packages using the following commands:

yarn upgrade
yarn upgrade [<package_name>]
yarn upgrade [<package_name>]@[<version_or_tag>]

If no package name is specified, the command updates all project dependencies to their latest versions, based on the version ranges defined in the package.json file. Otherwise, only the specified packages are updated.

To remove a package from your project’s dependencies, use the yarn remove command on Rocky Linux 8:

yarn remove [<package_name>]

This command updates the project’s package.json and yarn.lock files.

To install all dependencies listed in the package.json file of an existing project, use either of the following commands:

yarn

Or:

yarn install

Alternative Installation Methods for Yarn on Rocky Linux 8

While the above method is the recommended and most straightforward way to install Yarn on Rocky Linux 8, alternative methods exist that might be suitable depending on your specific needs or preferences. Here are two alternative approaches:

1. Installing Yarn via NPM (Node Package Manager):

Since Yarn is a JavaScript package manager, it can ironically be installed using NPM, which comes bundled with Node.js. This approach is useful if you prefer a more global installation or if you’re already familiar with NPM.

  • Explanation: This method leverages the global installation capability of NPM. Installing Yarn globally makes it accessible from any directory on your system.
  • Code Example:
sudo npm install -g yarn

After running this command, verify the installation by checking the Yarn version:

yarn --version

2. Using corepack (Experimental Feature – Node.js >= 16.17):

Node.js introduced corepack, a tool to manage package managers like Yarn and pnpm. corepack isn’t enabled by default; you need to explicitly enable it.

  • Explanation: corepack allows you to use Yarn (or pnpm) without explicitly installing it. It automatically fetches the correct version of the package manager as defined in your project’s package.json file. This ensures consistency across different development environments.
  • Code Example:

First, enable corepack:

sudo corepack enable

Now, you can directly use Yarn commands in your project. If Yarn isn’t already in the project, corepack will download and cache it for you. Make sure your package.json specifies the "packageManager" field to leverage this correctly.
For example:

{
  "name": "my-project",
  "version": "1.0.0",
  "packageManager": "yarn@^1.22.0"
}

Then, you can run yarn commands as usual:

yarn install

These alternative methods offer flexibility depending on your environment and preferences for managing JavaScript package managers. The direct dnf install, though, is often preferred for system-wide availability and consistency.

Conclusion

You have now successfully learned How To Install Yarn on Rocky Linux 8 and some basic usage commands. Yarn is a valuable tool for managing JavaScript dependencies in web development. Its speed, reliability, and security features make it a popular choice among developers.

We hope you found this guide helpful! Please subscribe to us on Facebook and YouTube for more helpful tutorials and tips.

You may also find these articles useful:

Install and Use Yarn on Ubuntu 22.04

How To Install and Use Yarn on Centos 7

Install Node.js LTS on Debian 12