374 words
2 minutes

How to Install Yarn on Linux and Use It Effectively

Yarn is a fast, reliable, and secure dependency manager for JavaScript projects. It simplifies package management and ensures consistency across your development environment. In this guide, we will walk you through the steps to install Yarn on Linux and explore its basic usage.


Why Use Yarn?#

Yarn is popular among developers because of its speed, deterministic installs, and robust feature set. Some of the advantages of using Yarn include:

  1. Faster Dependency Management: Yarn caches packages, reducing download times significantly.
  2. Security: It verifies the integrity of every package installed.
  3. Offline Mode: Yarn allows you to work offline with cached packages.

How to Install Yarn on Linux#

Prerequisites#

Before installing Yarn, ensure you have the following:

  • A Linux distribution (e.g., Ubuntu, Fedora, Arch Linux)
  • Node.js installed (at least version 16)

You can install Node.js using a package manager like apt or directly from Node.js official website.


Installation Steps#

Method 1: Install via Package Manager (Ubuntu/Debian-based systems)#

  1. Add the Yarn Repository:

    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
  2. Update the System and Install Yarn:

    sudo apt update
    sudo apt install yarn
  3. Verify Installation:

    yarn --version

Method 2: Install via npm#

If you already have npm installed, you can install Yarn globally with:

npm install -g yarn

Method 3: Install via Script (For All Distributions)#

Run the official Yarn installation script:

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

Basic Yarn Commands#

Once Yarn is installed, you can start managing your project dependencies. Here are some basic commands:

  1. Initialize a Project:

    yarn init
  2. Add a Dependency:

    yarn add [package-name]
  3. Remove a Dependency:

    yarn remove [package-name]
  4. Install Dependencies:

    yarn install
  5. Upgrade Dependencies:

    yarn upgrade
  6. Run Scripts:

    yarn run [script-name]

FAQs#

  1. What is the main difference between Yarn and npm? Yarn is known for its speed, offline capability, and deterministic installs. While npm has improved significantly, Yarn offers features like workspaces and plug-and-play dependency management.

  2. Can I use Yarn with existing npm projects? Yes, Yarn is fully compatible with npm projects. You can migrate seamlessly and even use yarn.lock alongside package.json.

  3. How do I switch between Yarn versions? You can use a version manager like nvm to switch Node.js versions or install multiple Yarn versions using the yarn policies set-version command.


Conclusion#

Yarn is a powerful tool for managing JavaScript dependencies on Linux. By following this guide, you can easily install Yarn and use it effectively in your projects. Whether you’re working on a small website or a large-scale application, Yarn can significantly improve your development workflow.

How to Install Yarn on Linux and Use It Effectively
https://netalynx.web.id/posts/how-to-install-yarn-linux/
Author
Neta Lynx
Published at
2025-01-23