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

Use code at checkout:

Skills
01.11.2024

Installing and Using the Yarn Package Manager on Linux

Yarn is a powerful JavaScript package manager that aims to make dependency management 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 help you install Yarn on a Linux system, explain how to use it, and discuss the differences between Yarn and npm as well as their respective advantages and disadvantages.

Installing Yarn on Linux

Yarn can be installed on a variety of Linux distributions. Below are the methods for some popular distributions.

Method 1: Installing Yarn using the APT package manager

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

  1. Update the package list:
    sudo apt update
  2. Install the necessary 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, verify that Yarn has installed successfully:
    yarn --version

Method 2: Install Yarn via npm

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

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

Using the basic Yarn commands

  • Initiate a new project:To create a new project with Yarn, navigate to the desired directory and start:
    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 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 execute scripts defined in your package.json file:

yarn run script-name

Yarn vs npm: Differences, advantages and disadvantages

Differences

  1. Lock files:
    • Yarn: Uses a yarn.lock file to lock package dependency versions, ensuring consistent installations across environments.
    • npm: Introduced a similar feature with package-lock.json in npm 5.
  2. Installation speed:
    • Yarn: Generally faster due to parallel installations and caching.
    • npm: Has been slower in the past, but improvements have been made in recent versions.
  3. CLI commands:
    • Yarn has unique commands like yarn upgrade-interactive for interactive upgrades.
  4. Workspaces:
    • Yarn: Supports workspaces for managing monorepositions.
    • npm.

Pros and cons of Yarn

Pros:

  • Speed: Faster installation thanks to caching and parallelism.
  • Deterministic installations.
  • More intuitive commands and better results.
  • Workspaces: Built-in support for monorepo management.

Disadvantages:

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

Pros and cons of 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.

Disadvantages:

  • Speed: Has been slower than Yarn in the past, although recent updates have improved it.
  • Less deterministic: Before npm 5, installations could vary across environments without a lock file.

Conclusion

Yarn is a powerful package manager that offers a number of features aimed at improving the workflow of JavaScript application development. With its speed, deterministic installations and easy-to-use commands, it has become a popular choice among developers. Although npm continues to be widely used and refined, the choice 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 about which package manager best suits your project’s needs.

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

Use code at checkout:

Skills