Install and Use the Yarn Package Manager in Linux ⋆ ALexHost SRL

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills
01.11.2024

Install and Use the Yarn Package Manager in Linux

Install and Use the Yarn Package Manager in Linux

Yarn is a powerful package manager for JavaScript that aims to make managing dependencies easier and more efficient. Developed by Facebook, it has gained popularity due to its speed and reliability compared to other package managers, such as npm (Node Package Manager). This article will guide you through installing Yarn on a Linux system, explain how to use it, and discuss the differences between Yarn and npm, along with their respective pros and cons.

Installation of Yarn on Linux

Yarn can be installed on various Linux distributions. Below are the methods for some popular distributions.

Method 1: Install Yarn Using the APT Package Manager

This is the most common method for installing Yarn on Debian-based systems.

  1. Update Your Package List:Open your terminal and run the following command:
    sudo apt update
  2. Install Required Dependencies:If you don’t have curl installed, you can install it:
    sudo apt install curl
  3. Add the Yarn APT Repository:Now, you can add the Yarn package repository:

    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/yarn.gpg
    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

  4. Update Your Package List Again:After adding the Yarn repository, update your package list again:
    sudo apt update
  5. Install Yarn:Now you can install Yarn:
    sudo apt install yarn
  6. Verify Installation:Finally, check if Yarn was installed successfully:
    yarn --version

Method 2: Install Yarn Using npm

If you already have Node.js and npm installed, you can install Yarn using npm:

  1. Install Node.js and npm (if not installed):
    sudo apt install nodejs npm
  2. Install Yarn via npm:After installing npm, you can install Yarn globally with the following command:
    npm install --global yarn

Using Yarn Basic Commands

  • Initialize a New Project:To create a new project with Yarn, navigate to your desired directory and run:
    yarn init

    Follow the prompts to create a package.json file.

  • Add a Dependency:To add a package (dependency) to your project, use:
    yarn add package-name
  • Add a Development Dependency:For development-only dependencies, use:
    yarn add package-name --dev
  • Remove a Dependency:To remove a package, use:
    yarn remove package-name
  • Install All Dependencies:If you have a package.json file, you can install all dependencies with:
    yarn install
  • Upgrade Dependencies:To upgrade a specific package:
    yarn upgrade package-name

Running Scripts

Yarn also allows you to run scripts defined in your package.json file:

yarn run script-name

Yarn vs. npm: Differences, Pros, and Cons

Differences

  1. Lock Files:
    • Yarn: Uses a yarn.lock file to lock down the versions of package dependencies, ensuring consistent installs across different environments.
    • npm: Introduced a similar feature with package-lock.json in npm 5.
  2. Install Speed:
    • Yarn: Generally faster due to parallel installations and caching.
    • npm: Historically slower, but improvements have been made in recent versions.
  3. CLI Commands:
    • While many commands are similar, Yarn has unique commands like yarn upgrade-interactive for interactive upgrades.
  4. Workspaces:
    • Yarn: Supports workspaces natively for managing monorepos.
    • npm: Introduced workspace support in npm 7.

Pros and Cons Yarn

Pros:

  • Speed: Faster installation times due to caching and parallelism.
  • Deterministic Installs: Ensures consistent installations across environments with yarn.lock.
  • User-Friendly CLI: More intuitive commands and better output.
  • Workspaces: Built-in support for monorepo management.

Cons:

  • Dependency on Node.js: Requires Node.js to be installed before Yarn.
  • Learning Curve: New users might need time to adjust to the different command structure compared to npm.

Pros and Cons npm

Pros:

  • Widespread Adoption: Comes pre-installed with Node.js, making it widely used and well-supported.
  • Mature Ecosystem: Large community and extensive resources available for troubleshooting.
  • Simplicity: Familiar to most JavaScript developers.

Cons:

  • Speed: Historically slower than Yarn, though recent updates have improved this.
  • Less Deterministic: Prior to npm 5, installs could vary between environments without a lock file.

Conclusion

Yarn is a powerful package manager that offers a range of features aimed at improving the development workflow for JavaScript applications. With its speed, deterministic installs, and user-friendly commands, it has become a popular choice among developers. While npm remains widely used and continues to improve, choosing between Yarn and npm often comes down to personal or team preference. By understanding the strengths and weaknesses of each, you can make an informed decision on which package manager best suits your project needs.

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills