How to Install Yarn on Debian 12, 11, or 10 Linux

Yarn is a fast, reliable, and secure dependency manager for JavaScript projects. It is an alternative to npm and offers features such as parallel installation, offline caching, and deterministic dependency resolution. Yarn is widely used by developers for managing project dependencies efficiently.

To install Yarn on Debian 12, 11, or 10, you have two primary methods. You can use the NodeSource and Yarn official APT repositories to install the latest stable builds of Node.js and Yarn, or you can use the nvm (Node Version Manager) APT repository to install the latest stable build of nvm on Debian, followed by installing Node.js and Yarn with npm.

Method 1: Install Yarn with NodeSource

Add NodeSource Repository

First, add the NodeSource repository to your Debian system. This repository contains the latest Node.js and Yarn versions; adding it lets your package manager know where to get them during installation.

Run these commands to add the NodeSource repository of the version of Node.js you wish to work with:

Node.js 22.x:

curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh
sudo -E bash nodesource_setup.sh

Node.js 20.x:

curl -fsSL https://deb.nodesource.com/setup_20.x -o nodesource_setup.sh
sudo -E bash nodesource_setup.sh

Node.js 18.x:

curl -fsSL https://deb.nodesource.com/setup_18.x -o nodesource_setup.sh
sudo -E bash nodesource_setup.sh

Next, import the Yarn repository with this command:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo gpg --dearmor -o /usr/share/keyrings/yarn-archive-keyring.gpg

Then, create a new file to store the Yarn repository information:

echo "deb [signed-by=/usr/share/keyrings/yarn-archive-keyring.gpg] https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

Install Node.js and Yarn

After adding the repositories, you can install Yarn using the apt package manager:

sudo apt update && sudo apt install yarn nodejs

To verify the installation, check the Yarn version:

yarn --version

The output should show the version number, indicating that Yarn is installed on your Debian system via NodeSource.

Method 2: Install Yarn via NVM

In this section, you’ll explore another method to install Yarn: using the Node Version Manager (NVM). This approach is handy for developers who work with multiple Node.js versions. NVM enables easy switching between different Node.js environments. When you use NVM to install Yarn, it links to a specific Node.js version. This way, you can have distinct Yarn setups for different projects or environments.

Install NVM on Debian for Yarn

First, make sure you have NVM installed on your Debian system.

Run one of these commands to install NVM:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash

or

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash

After installing NVM, close and reopen your terminal. Or, you can use this command to make NVM available immediately:

source ~/.bashrc

Node.js and Yarn Installation

With NVM ready, choose the Node.js version you want. NVM allows you to install several Node.js versions and switch between them easily.

For the latest LTS version of Node.js, use:

nvm install --lts

If you want the latest version, run:

nvm install stable

After installing Node.js, install Yarn globally for that version:

npm install -g yarn

To check if Yarn installed correctly, see its version:

yarn --version

If this command shows the Yarn version, you’ve successfully installed Yarn on your Debian system using NVM.

Basic Commands For Yarn

This section covers the most frequently used Yarn commands and relevant configuration examples. The aim is to give you the proficiency to navigate and utilize Yarn for your project dependencies.

Initialize a New Project

The first command to start a new project in Yarn is yarn init. This command will prompt you to answer a few basic questions about your project. It then generates a package.json file, which outlines the basic structure of your project.

yarn init

Adding Dependencies

To add a dependency to your project, you can use the yarn add command followed by the package name. This command downloads the package and updates your package.json and yarn.lock files.

yarn add [package_name]

Adding Dev Dependencies

Development dependencies are added similarly to regular dependencies but with the -D flag. These are usually packages required during development but not in production.

yarn add -D [package_name]

Upgrading Dependencies

To upgrade a dependency, you can use the yarn upgrade command. This command will update the package to its latest version.

yarn upgrade [package_name]

Removing Dependencies

You can use the yarn remove command to remove a dependency from your project.

yarn remove [package_name]

Installing All Dependencies

The yarn install command installs all the dependencies for a project. It is usually the first command you run when cloning a Yarn project.

yarn install

Checking for Outdated Packages

Yarn offers a handy command to check for outdated packages. The yarn outdated command provides a list of packages that need updating.

yarn outdated

Run Scripts

The yarn run command runs scripts defined in the package.json file.

yarn run [script_name]

Creating a Yarn Alias

Using the yarn config set command, you can create an alias for a Yarn command. This can be particularly useful for long commands that you use frequently.

yarn config set [alias_name] [command]

Listing Installed Packages

The yarn list command provides a tree view of the dependencies installed for your project.

yarn list

Displaying Package Info

To display a package’s information, use the yarn info command followed by the package name.

yarn info [package_name]

Checking Why a Package is Installed

Yarn provides the yarn why command, which checks why a specific package is installed in your project.

yarn why [package_name]

Setting Registry

The yarn config set registry command lets you change the default package registry.

yarn config set registry [registry_url]

Adding a Scoped Registry

If you need to add a scoped registry, use the yarn config set command, as shown below.

yarn config set '@scope:registry' [registry_url]

Log in to a Registry

The yarn login command enables you to log in to a registry that requires authentication.

yarn login --registry [registry_url]

Publishing a Package

To publish a package to a registry, you can use the yarn publish command.

yarn publish

Cleaning up

To clean up unnecessary files and optimize the project, Yarn provides the yarn clean command.

yarn clean

Interactive Upgrade

Yarn provides an interactive way to upgrade packages using the yarn upgrade-interactive command.

yarn upgrade-interactive

Global Adding/Removing

Lastly, to add or remove packages globally, you can use yarn global add or yarn global remove commands.

yarn global add [package_name]
yarn global remove [package_name]

Conclusion

By installing Yarn on your Debian system using either the NodeSource and Yarn repositories or the nvm method, you can efficiently manage your JavaScript project dependencies. Regularly check for updates to ensure you have the latest stable builds of Node.js and Yarn. These methods provide flexibility and ensure you have a robust setup for your development environment. Enjoy the speed, reliability, and security that Yarn brings to your project management.

Leave a Comment