Install Yarn on AlmaLinux 9 with Easy Steps – OrcaCore
In this comprehensive guide, brought to you by Orcacore, we’ll delve into How To Install Yarn on AlmaLinux 9. Yarn has emerged as a compelling alternative to npm, offering a faster, more secure, and more reliable way to manage your project dependencies. While maintaining compatibility with the npm registry, Yarn optimizes the workflow, making it a favorite among developers.
Before we dive into the installation process, let’s ensure you have the necessary prerequisites. You’ll need to be logged into your AlmaLinux 9 server as a non-root user with sudo privileges. If you haven’t already configured this, you can refer to our guide on Initial Server Setup with AlmaLinux 9 for detailed instructions.
Install Yarn Package Manager on AlmaLinux 9
Let’s begin by updating your local package index to ensure you have the latest package information. Execute the following command in your terminal:
sudo dnf update
Installing Node.js for Yarn Package Manager
Yarn relies on Node.js, so if it’s not already present on your system, we’ll need to install it. Use the following command to install Node.js:
sudo dnf install @nodejs
Once the installation is complete, verify that Node.js is installed correctly by checking its version:
node --version
The output should display the installed Node.js version, similar to this:
**Output**
v18.12.1
Note that your Node.js version might differ from the example shown here.
Add Yarn GPG Key and Repository
Next, we need to add the Yarn repository and import its GPG key to ensure the integrity of the packages we’ll be installing. Run the following 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
After adding the repository, rerun the system update to include the new repository information:
sudo dnf update
Installing Yarn
With the Yarn repository enabled, we can now install Yarn itself. Use the following command:
sudo dnf install yarn
To confirm that Yarn is installed correctly, check its version:
yarn --version
The output will display the installed Yarn version, indicating a successful installation.
You’ve now successfully installed Yarn on your AlmaLinux 9 system. Let’s explore how to use it effectively.
How To Use Yarn on AlmaLinux 9
In this section, we’ll walk through the basic usage of Yarn, including creating a new project and managing dependencies.
Create a New Yarn Project
To create a new Yarn project, use the yarn init
command. For example, to create a project named my_project
, execute the following command:
yarn init my_project
Yarn will prompt you with a series of questions about your project. You can either accept the default values by pressing Enter or provide your own answers.
The interaction will resemble the following:
This process generates a basic package.json
file containing the information you provided. You can modify this file at any time to update your project’s metadata.
Alternatively, you can initiate a Yarn project in an existing directory. Simply navigate to the directory and run the yarn init
command without specifying a project name:
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 any packages it depends on. 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]
Upgrade a Package with Yarn
Yarn provides commands for upgrading packages:
$ yarn upgrade
$ yarn upgrade [package_name]
$ yarn upgrade [package_name]@[version_or_tag]
If no package name is provided, the yarn upgrade
command updates all project dependencies to their latest versions, based on the version ranges specified in the package.json
file. Otherwise, only the specified packages are updated.
Remove a Package with Yarn
To remove a package from your project’s dependencies, use the yarn remove
command:
yarn remove [package_name]
This command removes the specified package and updates the package.json
and yarn.lock
files accordingly.
Install All Dependencies
In an existing project, you can install all dependencies specified in the package.json
file using either of the following commands:
yarn
Or:
yarn install
Alternative Solutions for Installing Yarn on AlmaLinux 9
While the standard method outlined above is reliable, there are alternative approaches to installing Yarn on AlmaLinux 9. Let’s explore two such methods:
1. Using npm
(Node Package Manager)
Since Yarn is compatible with the npm registry, you can actually use npm
to install Yarn globally. This method can be useful if you already have npm
set up and prefer to manage all your packages through it.
First, ensure Node.js and npm
are installed (as shown in the original article). Then, run the following command:
sudo npm install -g yarn
The -g
flag ensures that Yarn is installed globally, making it accessible from any directory.
After the installation, verify the Yarn version to confirm the installation:
yarn --version
Explanation:
This method leverages the existing npm
infrastructure to install Yarn. It’s a straightforward approach, especially if you’re already familiar with npm
. However, it’s important to note that this method relies on npm
being correctly configured, and any issues with npm
might affect the Yarn installation.
2. Using corepack
corepack
is a tool that ships with Node.js and is designed to manage package managers like Yarn and pnpm. It is disabled by default but it allows you to use package managers without having them explicitly installed.
First, enable corepack
:
corepack enable
Once enabled, you can directly use yarn
:
yarn --version
If Yarn is not already in your path, corepack
will automatically download and cache the appropriate version based on your project’s package.json
or global settings.
Explanation:
corepack
offers a different paradigm. Instead of explicitly installing Yarn, it manages the Yarn versions on-demand, based on your project’s configuration. This approach can simplify dependency management and avoid potential conflicts between different Yarn versions. It’s particularly useful in environments where multiple projects with varying Yarn requirements coexist.
Conclusion
Yarn has taken inspiration from npm, addressing its shortcomings to create a package management solution that developers appreciate. This guide has provided a comprehensive walkthrough of How To Install Yarn on AlmaLinux 9. We covered the standard installation process, along with alternative methods using npm
and corepack
. By understanding these different approaches, you can choose the method that best suits your needs and workflow. Please follow us on Facebook, YouTube, and X.
You may also find these articles helpful:
Install and Use Yarn on Ubuntu 22.04
How To Enable TCP BBR on AlmaLinux 9
How To Install PowerDNS on AlmaLinux 9