RHEL BasedRocky Linux

How To Install Yarn on Rocky Linux 10

Install Yarn on Rocky Linux 10

Installing Yarn on Rocky Linux 10 opens the door to faster, more reliable JavaScript package management for your development projects. Yarn, developed by Meta (formerly Facebook), addresses many limitations found in traditional package managers by offering parallel downloads, offline installation capabilities, and deterministic dependency resolution. This comprehensive guide walks you through three proven installation methods, complete with troubleshooting tips and best practices to get Yarn running smoothly on your Rocky Linux 10 system.

Whether you’re managing a production server or setting up a development environment, you’ll find a method that fits your specific needs. Let’s dive in.

Understanding Yarn Package Manager

Yarn stands for “Yet Another Resource Negotiator.” It’s a fast, reliable, and secure JavaScript package manager that revolutionized dependency management when it launched in 2016. Built as an alternative to NPM, Yarn solves critical issues developers faced with inconsistent installations and slow download speeds.

The package manager works seamlessly with the NPM registry, meaning you have access to the entire NPM ecosystem while benefiting from Yarn’s enhanced performance. This compatibility makes migration from NPM straightforward and risk-free.

Key Advantages of Using Yarn

Yarn brings several compelling features to the table. Speed is its most celebrated attribute—parallel installation processes can reduce setup time by 30-50% compared to sequential installations. While NPM downloads packages one at a time, Yarn fetches multiple packages simultaneously, dramatically cutting installation times for large projects.

Security is baked into Yarn’s core functionality. Every package undergoes checksum verification before execution, protecting your projects from compromised or tampered dependencies. This verification happens automatically, giving you peace of mind without extra configuration.

The offline mode capability sets Yarn apart. Once a package is downloaded, it’s cached locally. Future installations pull from this cache even without internet connectivity. For developers working in restricted environments or with unreliable connections, this feature is invaluable.

Deterministic installations ensure consistency across development, staging, and production environments. The yarn.lock file locks exact package versions, eliminating the “it works on my machine” problem. When your team clones a project, everyone gets identical dependencies.

When to Choose Yarn Over NPM

Yarn excels in enterprise environments where reliability and speed matter most. Team projects benefit from Yarn’s consistent installations across different machines. CI/CD pipelines run faster with parallel downloads. Monorepo architectures leverage Yarn’s workspace features for managing multiple packages efficiently.

For individual developers, the performance gains become noticeable in projects with numerous dependencies. Modern web applications often include hundreds of packages—Yarn’s efficiency shines in these scenarios.

Prerequisites and System Requirements

Before installing Yarn, ensure your Rocky Linux 10 system meets the necessary requirements. Rocky Linux 10 runs smoothly with a minimum of 2GB RAM, 10GB storage, and a 1 GHz 64-bit processor. For development work, 4GB RAM and 20GB storage provide a more comfortable experience.

Rocky Linux 10 supports multiple architectures including x86_64-v3, aarch64, ppc64le, and s390x. Check your system’s compatibility before proceeding.

You’ll need root or sudo access to install system-wide packages. An active internet connection is required for the initial installation, though Yarn will function offline afterward.

Preparing Your System

Start by verifying your Rocky Linux version. Open your terminal and run:

cat /etc/os-release

You should see output confirming Rocky Linux 10. Next, update your system packages to ensure you’re working with the latest software:

sudo dnf update -y

This command refreshes your package repositories and installs available updates. The process takes a few minutes depending on your system’s current state.

Check if Node.js or NPM are already installed:

node -v && npm -v

If these commands return version numbers, Node.js is present. If not, don’t worry—we’ll install it in the next section.

Method 1: Installing Yarn via NPM

This method suits developers already familiar with NPM and those seeking the quickest path to a working Yarn installation. It’s ideal for development environments and single-user systems.

Step 1: Install Node.js and NPM

Yarn requires Node.js to function. We’ll add the official NodeSource repository to access the latest Node.js version. Run this command:

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

This script adds the NodeSource repository to your system and imports the necessary GPG keys. The setup script automatically detects your system architecture and configures the appropriate repository.

Now install Node.js:

sudo dnf install -y nodejs

The installation includes both Node.js runtime and NPM package manager. Verify the installation succeeded:

node -v && npm -v

You should see version numbers like Node v22.x.x and NPM v10.x.x. These confirm Node.js is ready.

Step 2: Install Yarn Globally Using NPM

With Node.js installed, adding Yarn is straightforward. Execute this command:

sudo npm install -g yarn

The -g flag installs Yarn globally, making it available system-wide. NPM downloads Yarn from the registry, places the binary in your system path, and creates necessary symbolic links. The process typically completes in 30 seconds to 2 minutes depending on your network speed.

Step 3: Verify Yarn Installation

Confirm Yarn installed correctly by checking its version:

yarn --version

You’ll see output like 1.22.22, indicating Yarn Classic is installed. Check where Yarn’s binary lives:

which yarn

The command returns /usr/local/bin/yarn or /usr/bin/yarn, confirming the binary is in your system PATH.

Test Yarn by running:

yarn help

A list of available commands appears, proving Yarn is functioning properly.

This method’s simplicity makes it perfect for developers who already use NPM extensively. Updates are handled through NPM, and version management integrates with your existing workflow.

Method 2: Installing Yarn from Official Repository

The repository method provides the most robust solution for production environments and multi-user systems. It integrates Yarn with your system’s package manager for cleaner updates and better system integration.

Step 1: Add the Yarn Repository

First, download and add Yarn’s official repository configuration:

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

This command fetches the repository definition file and saves it to your system’s repository directory. The file tells DNF where to find official Yarn packages.

Alternatively, use DNF’s config manager:

sudo dnf config-manager --add-repo=https://dl.yarnpkg.com/rpm/yarn.repo

Both approaches achieve the same result. Verify the repository was added successfully:

sudo dnf repolist

Look for “yarn” in the output list.

Step 2: Import the GPG Key

Security matters. Import Yarn’s GPG key to verify package authenticity:

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

This key ensures packages you download come from Yarn’s official maintainers and haven’t been tampered with. GPG signatures protect against malicious package substitution.

Step 3: Install the Yarn Package

Search for available Yarn versions:

sudo dnf search yarn

Now install Yarn:

sudo dnf install yarn -y

DNF downloads the package, verifies its signature, and installs it to your system. You may see a prompt asking to accept the GPG key during first installation—press ‘y’ to continue. The installation typically uses less than 10MB of disk space and completes within a minute.

Step 4: Verify the Installation

Check Yarn’s version:

yarn -v

The version number confirms successful installation. List installed files to see where Yarn placed its components:

rpm -ql yarn

You’ll see binaries, documentation, and configuration files in their respective directories.

This method offers significant advantages for production environments. Updates are straightforward:

sudo dnf update yarn

Your system’s package manager handles version tracking, making maintenance cleaner than manual methods. Multiple users on the same system all access the same Yarn installation, reducing duplication and ensuring consistency.

Method 3: Installing Yarn with Installation Script

The script method provides the fastest installation path and works well when you lack root privileges or need a user-specific installation. It’s popular in containerized environments and personal development setups.

Step 1: Download and Execute the Script

Run this single command:

curl -o- -L https://yarnpkg.com/install.sh | bash

The script downloads the latest Yarn version, extracts it to ~/.yarn/ in your home directory, and automatically configures your PATH. You’ll see output like “Successfully installed Yarn 1.22.22!” when it completes.

This installation lives in your user directory, meaning it doesn’t require sudo privileges for the installation itself. Each user on a multi-user system can maintain their own Yarn version.

Step 2: Configure Your Environment

Activate the PATH changes:

source ~/.bashrc

If you use zsh instead of bash, run:

source ~/.zshrc

The script adds ~/.yarn/bin to your PATH automatically. If you encounter issues, manually add this line to your ~/.bashrc file:

export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"

Save the file and source it again.

Step 3: Verify Installation

Check Yarn is accessible:

yarn -v

Verify the PATH includes Yarn’s directory:

echo $PATH | grep yarn

Confirm which Yarn binary you’re using:

which yarn

The output should show ~/.yarn/bin/yarn.

This method’s primary advantage is speed—one command gets you running. Updates are equally simple: re-run the installation script to upgrade to the latest version. The user-level installation means you can use different Yarn versions for different projects without system-wide conflicts.

However, remember this installation only affects your user account unless you configure system-wide PATH settings. Other users won’t have Yarn access automatically.

Post-Installation Configuration and Basic Usage

With Yarn installed, let’s verify everything works and explore basic commands to get you productive immediately.

Complete Verification Checklist

Run these commands to ensure a successful setup:

yarn -v
yarn help
yarn global bin

The first shows your version. The second displays available commands. The third reveals where Yarn stores global binaries.

Essential Yarn Commands

Initialize a new project in an empty directory:

yarn init

Yarn asks interactive questions about your project—name, version, description, entry point, repository, author, and license. Answer each prompt or press Enter to accept defaults. This creates a package.json file containing your project metadata.

For quick initialization without prompts:

yarn init -y

Add packages to your project:

yarn add express

This installs Express.js and updates both package.json and yarn.lock. For development dependencies:

yarn add --dev jest

Install all dependencies from an existing project:

yarn install

This command reads package.json and installs every listed dependency. Remove unwanted packages:

yarn remove express

List installed packages and their versions:

yarn list

Understanding Key Files

Three files are central to Yarn’s operation. package.json declares your project’s metadata and dependency requirements. It’s human-readable and typically edited directly. yarn.lock contains exact resolved versions of every dependency and transitive dependency. Never edit this file manually—Yarn manages it automatically. The lock file ensures reproducible installations across all environments.

node_modules/ stores the actual package code. This directory can grow large in complex projects. It’s generated from package.json and yarn.lock, so it’s excluded from version control.

Creating a Test Project

Let’s build confidence with a quick test:

mkdir yarn-test && cd yarn-test
yarn init -y
yarn add express

These commands create a directory, initialize a Yarn project, and install Express.js. Check the directory contents:

ls -la

You’ll see package.json, yarn.lock, and node_modules/. Your test installation succeeded.

Troubleshooting Common Issues

Even smooth installations occasionally hit snags. Here’s how to resolve typical problems.

Command Not Found Errors

If yarn returns “command not found,” your PATH configuration needs attention. First, try reloading your shell configuration:

source ~/.bashrc

Still not working? Manually add Yarn to your PATH by editing ~/.bashrc:

export PATH="$HOME/.yarn/bin:/usr/local/bin:$PATH"

Save, then source the file again. For system-wide installations via repository method, verify the binary exists:

ls -l /usr/bin/yarn

If missing, reinstall using your chosen method.

Permission Denied Errors

NPM installations without sudo often trigger permission errors. Either use sudo with the install command or configure NPM’s prefix for user-level installations:

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH=~/.npm-global/bin:$PATH

Add the export line to ~/.bashrc for persistence. Alternatively, switch to the script installation method, which installs to your home directory by default.

GPG Key Import Failures

Network restrictions or proxy servers sometimes block GPG key imports. Download the key manually:

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

Check your firewall settings if downloads fail completely. Corporate networks may require proxy configuration.

Version Conflicts

Yarn requires compatible Node.js versions. If you encounter errors about unsupported Node.js, update Node.js to a current LTS version. Remove the old version first:

sudo dnf remove nodejs

Then reinstall using the NodeSource repository setup from Method 1.

Repository Connection Problems

Cannot reach dl.yarnpkg.com? Verify internet connectivity first:

ping -c 4 dl.yarnpkg.com

DNS issues may require updating /etc/resolv.conf with reliable nameservers. Proxy environments need Yarn configured with proxy settings in ~/.yarnrc:

proxy "http://proxy.company.com:8080"
https-proxy "http://proxy.company.com:8080"

Best Practices and Recommendations

Professional Yarn usage follows certain patterns that prevent problems and optimize performance.

Production Environment Guidelines

Use the repository installation method for production servers. System package management simplifies updates and rollbacks. Lock Yarn versions in CI/CD pipelines to ensure consistent builds. Add this to your pipeline configuration:

yarn --frozen-lockfile

This flag fails the build if yarn.lock needs updates, catching dependency drift early.

Run security audits regularly:

yarn audit

This command checks for known vulnerabilities in your dependency tree. Always commit yarn.lock to version control. The lock file is the key to reproducible builds—without it, different environments may resolve different package versions.

Performance Optimization

Enable offline mirroring for faster installations in restricted environments. Configure a shared cache location when multiple users work on the same server:

yarn config set cache-folder /shared/yarn-cache

Project-specific configurations live in .yarnrc files in your project root. This keeps team settings consistent without system modifications.

Security Considerations

Update Yarn regularly to patch security vulnerabilities. Repository method users run:

sudo dnf update yarn

NPM method users execute:

sudo npm update -g yarn

Yarn verifies package checksums automatically, but stay vigilant about dependency vulnerabilities. Tools like yarn audit and dependabot help monitor security issues.

Updating and Uninstalling Yarn

Maintenance is part of the package manager lifecycle. Here’s how to update or remove Yarn cleanly.

Updating Yarn

For NPM installations:

sudo npm update -g yarn

Repository installations use DNF:

sudo dnf update yarn

Script installations simply re-run the installation command:

curl -o- -L https://yarnpkg.com/install.sh | bash

The script detects existing installations and upgrades them. Verify the update:

yarn -v

Uninstalling Yarn

Remove NPM-installed Yarn:

sudo npm uninstall -g yarn

Repository installations:

sudo dnf remove yarn

Script installations require manual cleanup:

rm -rf ~/.yarn

Edit ~/.bashrc to remove the PATH additions. Remove the lines containing .yarn/bin. Configuration files in ~/.yarnrc can also be deleted.

Congratulations! You have successfully installed Yarn. Thanks for using this tutorial for installing Yarn Package Manager on your Rocky Linux 10 system. For additional help or useful information, we recommend you check the official Yarn website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button