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

Install and Use Yarn on Your AlexHost VPS

Why use Yarn on AlexHost? Yarn, a fast and reliable JavaScript package manager, streamlines dependency management for Node.js projects. AlexHost’s VPS with NVMe storage, root access, and DDoS protection ensures swift Yarn operations for your web apps. This guide covers installing Yarn on Linux, basic commands, and compares Yarn vs. npm for AlexHost users.

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: Power Your JavaScript Projects with Yarn on AlexHost

Yarn’s speed and reliability make it a top choice for dependency management, and AlexHost’s NVMe-powered VPS ensures lightning-fast installs. Set up Yarn via APT or npm, use intuitive commands, and leverage AlexHost’s security for robust Node.js apps. Whether you choose Yarn or npm, AlexHost’s root access and support keep your projects running smoothly—start building now!

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

Use code at checkout:

Skills