Website Vidya
Cloud & DevOpsStep-by-Step Guide

How to Install Yarn on Ubuntu 24.04

Learn how to install Yarn on Ubuntu 24.04 LTS. Step-by-step guide covers Corepack, official APT repository, npm setup, and project workflow.

RR
Rajesh RayLead Systems Educator
Published:
8 min read
PDF Cheat Sheet
How to Install Yarn on Ubuntu 24.04
AI Executive SummaryWebsiteVidya AI

8 min read

Learn how to install Yarn on Ubuntu 24.04 LTS. Step-by-step guide covers Corepack, official APT repository, npm setup, and project workflow.

Key Highlights
  • Prerequisites and Supported Releases
  • Step 1: Preparing Your Ubuntu 24.04 System
  • Step 2: Checking and Installing Node.js
Grounded in full engineering runbookWebsiteVidya AI Engine

In modern full-stack JavaScript and TypeScript development, managing application dependencies efficiently and deterministically is essential. Yarn is an open-source JavaScript package manager designed as a high-performance, resilient alternative to npm. Developed by engineers from Meta, Google, and Exponent, Yarn introduced features that have become standard in modern web development: parallel package downloads, strict offline caching, deterministic lockfile integrity through yarn.lock, and reproducible workspaces for monorepos.

If you are building front-end applications with React, Next.js, or Vue, or backend microservices with Node.js on Ubuntu Linux, learning how to install yarn on ubuntu 24.04 gives you access to both Yarn Classic (1.x) and Yarn Modern (Berry / 2.x–4.x). While Yarn Classic is still widely used in legacy systems, Yarn Modern introduces groundbreaking architectures such as Zero-Installs and Plug'n'Play (PnP), eliminating bulky node_modules directories in favor of optimized zip-based package references.

In this comprehensive tutorial, we walk you through the three established methods to install Yarn on Ubuntu 24.04 LTS (Noble Numbat): using Node.js Corepack (the officially recommended standard by the Yarn team), configuring the official Yarn APT repository with modern GPG keyrings, and installing globally via npm. We also demonstrate day-to-day package workflows, monorepo workspace configuration, and essential troubleshooting tips for permissions and path resolution.

Advertisement

Prerequisites and Supported Releases

This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat). The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.

Before proceeding, ensure you have the following requirements satisfied:

  • A server or desktop running Ubuntu 24.04 LTS with terminal access.
  • A non-root user account configured with sudo administrative privileges.
  • A supported version of Node.js (v18.x, v20.x, or v22.x LTS) installed on your system.
  • Basic familiarity with terminal commands, the bash shell, and JavaScript package workflows.

Step 1: Preparing Your Ubuntu 24.04 System

Before installing Yarn, update your system package catalog and verify that essential networking and cryptography utilities are present. The APT package manager requires secure transport tools to communicate with remote repositories.

Run the following command to update package lists and install curl, gnupg, and ca-certificates on your machine.

Advertisement

These utilities allow you to fetch public GPG signing keys and verify package integrity across secure HTTPS connections.

# Refresh package repository indices
sudo apt update && sudo apt upgrade -y

# Install prerequisite transport and cryptography tools
sudo apt install -y curl gnupg ca-certificates

Step 2: Checking and Installing Node.js

Yarn relies on the Node.js runtime environment to execute. Before proceeding with any Yarn installation method, confirm whether Node.js and npm are already available on your machine.

Check the installed versions of node and npm by executing the following commands in your terminal.

If Node.js is not yet installed, you can install the official Ubuntu 24.04 packaged version directly using APT, or alternatively use NodeSource or NVM for specific release tracks.

Advertisement
# Check existing Node.js and npm versions
node --version 2>/dev/null || echo 'Node.js is not installed'
npm --version 2>/dev/null || echo 'npm is not installed'

# Install default Node.js and npm from Ubuntu repositories if missing
sudo apt install -y nodejs npm

Beginning with Node.js 16.9.0 and continued across Node.js 20 and 22 in Ubuntu 24.04, Node.js bundles Corepack. Corepack is an official bridge between Node.js and package managers like Yarn and pnpm. Instead of managing Yarn versions globally through manual binary downloads, Corepack downloads and activates the exact Yarn release specified in each project's package.json file.

The Yarn core team recommends Corepack as the primary installation vector for all new projects and Yarn Modern deployments. To enable Corepack on your Ubuntu system, execute the command below.

Once enabled, Corepack automatically creates symlinks for yarn and yarnpkg in your system path, pointing to the Corepack binary dispatcher located at /usr/bin/corepack or /usr/local/bin/corepack.

Next, initialize and activate the latest stable Yarn release globally across your operating system.

# Enable the Corepack binary dispatcher
sudo corepack enable

# Prepare and activate the latest stable Yarn release
corepack prepare yarn@stable --activate

# Verify the installed Yarn binary version
yarn --version

Step 4: Alternative Method — Installing Yarn via the Official APT Repository

If your production deployments or infrastructure automation playbooks rely on Debian package management, or if your projects specifically require Yarn Classic (1.22.x), installing via the official Yarn APT repository is a robust approach.

In accordance with Ubuntu 24.04 security standards, third-party GPG keys must be stored in de-armored format inside dedicated keyring directories rather than using deprecated tools like apt-key add. First, ensure the keyring directory exists at /etc/apt/keyrings.

Download the official Yarn GPG signing key and save it securely into the keyring directory with read-only permissions for unprivileged users.

Create the APT repository source entry in /etc/apt/sources.list.d/yarn.list, explicitly referencing the de-armored keyring file, and update your repository catalog.

# Create dedicated directory for trusted APT keyrings
sudo mkdir -p -m 0755 /etc/apt/keyrings

# Import the official Yarn GPG signing key
curl -fsSL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/yarn.gpg
sudo chmod 0644 /etc/apt/keyrings/yarn.gpg

# Add the official Yarn repository entry
echo 'deb [signed-by=/etc/apt/keyrings/yarn.gpg] https://dl.yarnpkg.com/debian/ stable main' | sudo tee /etc/apt/sources.list.d/yarn.list

# Update package lists and install Yarn without re-installing Node.js
sudo apt update
sudo apt install -y --no-install-recommends yarn

Step 5: Alternative Method — Installing Yarn Globally via npm

For developers who manage multiple Node.js environments using Version Managers such as NVM (Node Version Manager) or FNM (Fast Node Manager), installing Yarn via npm directly into your active Node environment is frequently the most convenient method.

If you are using the system-installed Node.js package on Ubuntu 24.04, install Yarn globally with elevated privileges using sudo npm install -g yarn.

If you manage Node.js via NVM or have configured a custom user-level npm prefix at ~/.npm-global, run the command without sudo to avoid altering system root permissions.

Verify that your active shell can locate the globally installed Yarn executable by checking its path and reported release number.

# Install Yarn globally using npm
sudo npm install -g yarn

# Verify executable location and version
which yarn
yarn --version

Step 6: Initializing a New Project and Managing Dependencies

Once Yarn is installed on Ubuntu 24.04, you can integrate it into your everyday JavaScript workflow. Here are the most essential commands for initializing projects, managing dependencies, and executing scripts.

To initialize a new project and generate a boilerplate package.json file, create an application directory and execute yarn init -y.

To add a production dependency to your project, use the yarn add command. Yarn fetches the package from the registry, extracts it, and records its exact version and integrity hash in yarn.lock.

To install development dependencies (such as testing frameworks, linters, or TypeScript compiler types), append the -D (or --dev) flag.

# Create a demo application directory
mkdir -p ~/my-yarn-app && cd ~/my-yarn-app

# Initialize package.json with defaults
yarn init -y

# Add production runtime dependencies
yarn add express dotenv

# Add development dependencies
yarn add -D typescript @types/node jest

Step 7: Upgrading to Yarn Modern (Berry) in Existing Projects

If your project was initialized with Yarn Classic (1.x) and you wish to take advantage of Yarn Berry's improved resolution engine, Plug'n'Play, and enhanced workspace protocols, you can set the modern release on a per-project basis.

Navigate to your project root and instruct Yarn to download and configure the modern release using yarn set version stable.

This command creates a .yarn folder containing the standalone Yarn engine binary and generates a modern configuration file named .yarnrc.yml. You can inspect the configuration file to customize node linkers, registry credentials, and network timeouts.

If your project or certain third-party libraries require a traditional node_modules directory instead of Plug'n'Play, specify the node-modules linker in .yarnrc.yml by setting nodeLinker: node-modules.

# Switch project to Yarn Modern (Berry)
cd ~/my-yarn-app
yarn set version stable

# Configure traditional node_modules directory structure if required
echo 'nodeLinker: node-modules' >> .yarnrc.yml

# Reinstall dependencies using modern resolution engine
yarn install

Step 8: Verifying Lockfile Integrity and Running Scripts

Deterministic builds are a key benefit of Yarn. When cloning an existing repository containing a package.json and yarn.lock file, running yarn install --immutable (or yarn install --frozen-lockfile in Yarn Classic) guarantees that no packages are silently upgraded.

Yarn also simplifies running custom scripts defined in the scripts section of your package.json file. Unlike traditional npm, you can omit the run keyword for custom scripts.

Execute your build, test, and startup tasks directly through the Yarn command line interface.

# Enforce immutable lockfile verification during CI/CD builds
yarn install --immutable

# Execute build and test scripts defined in package.json
yarn build
yarn test

Troubleshooting and Common Errors

When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:

Command 'yarn' not found or conflicts with 'cmdtest'

Resolution: On older Ubuntu installations or systems with the legacy cmdtest package installed, running yarn may trigger a python test tool rather than the package manager. Remove the conflicting package by running sudo apt remove -y cmdtest and verify that /usr/bin is in your $PATH.

Corepack signature verification or network download failure

Resolution: If Corepack reports cryptographic errors when fetching Yarn binaries behind enterprise firewalls or proxies, reinstall Corepack dispatcher symlinks using sudo corepack enable --install-directory /usr/local/bin and ensure your corporate proxy is configured in HTTPS_PROXY.

EACCES permission errors when installing global packages

Resolution: Avoid using sudo when installing global packages. Configure an unprivileged global directory in your home folder by running yarn config set prefix ~/.yarn and appending export PATH="$HOME/.yarn/bin:$PATH" to your ~/.bashrc file.

Security Best Practices and Hardening

Always commit your yarn.lock file to version control. The lockfile contains cryptographic checksums for every downloaded package, preventing dependency substitution and man-in-the-middle attacks.

Regularly audit your project dependencies for known security vulnerabilities by executing yarn audit in your terminal.

Store custom private registry tokens inside environment variables or ~/.npmrc rather than committing plaintext authentication credentials into .yarnrc.yml or source code repositories.

When using Yarn via the official APT repository, always verify that the repository line in /etc/apt/sources.list.d/yarn.list references the verified GPG keyring located at /etc/apt/keyrings/yarn.gpg.

Deepen your Linux system administration skills with our related production-tested guides:

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

3 FAQs
Yarn Classic (v1.x) relies on traditional node_modules directory hierarchies and is in maintenance mode. Yarn Modern (Berry, v2+) features a completely redesigned modular architecture, supporting Plug'n'Play (PnP), zero-installs, and native workspace enhancements.
Corepack is officially bundled with Node.js and eliminates global version collisions. It allows different projects on the same Ubuntu system to run different Yarn releases seamlessly without manual reinstallation.
If installed via APT, run sudo apt purge yarn && sudo rm /etc/apt/sources.list.d/yarn.list /etc/apt/keyrings/yarn.gpg. If installed via npm, run sudo npm uninstall -g yarn. If enabled via Corepack, disable it with sudo corepack disable.
RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Cloud & DevOps•

How to Close File Descriptors Above 9 in Dash Shell

Attempting to close a file descriptor above 9 in Dash using exec 10<&- results in a 10: not found error because Dash's POSIX lexer only recognizes single-digit file descriptors (0–9) in redirection operators. This guide explains the exact parser limitation, why FDs 10+ are reserved for internal shell state, and how to safely close inherited file descriptors using close_range, Python, or Perl wrappers.

RR
Rajesh Rai
Read More
Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout