Install Yarn on Debian 11: Best Node JS Package Manager

Posted on

Install Yarn on Debian 11: Best Node JS Package Manager

Install Yarn on Debian 11: Best Node JS Package Manager

In this tutorial, we will guide you through the process to Install Yarn on Debian 11. Yarn is a fast, reliable, and secure dependency management tool compatible with npm. It streamlines the process of setting up, updating, configuring, and removing Node.js packages. NPM (Node Package Manager) is the default package manager for Node.js and the world’s largest software registry, widely used by open-source DevOps engineers to share and distribute their code. Using Yarn on Debian 11 can significantly improve your development workflow.

Before proceeding, ensure you have a non-root user with sudo privileges on your Debian 11 server. If you haven’t already, you can follow our guide on Initial Server Setup with Debian 11.

1. Debian 11 Node.js Installation

To install Yarn, Node.js must be installed first. Update your package index using:

sudo apt update

Install the necessary packages for adding repositories and handling downloads:

sudo apt install software-properties-common apt-transport-https wget ca-certificates gnupg2 gcc make g++ curl -y

Import the NodeSource repository. Choose either the current release or the LTS version based on your needs:

# curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
# curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo bash -

2. Add Yarn GPG Key and Repository on Debian 11

Now that Node.js is set up, you can proceed with installing Yarn on your Debian 11 system.

First, import the Yarn GPG key:

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null

Then, add the Yarn repository to your system’s sources list:

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

Update the system package index to reflect the new repository:

sudo apt update

3. Install Yarn Package Manager on Debian 11

With the repository added, you can now install Yarn and Node.js with the following command:

sudo apt install yarn nodejs -y

Verify the Yarn installation by checking its version:

yarn -v
**<mark>Output</mark>**
1.22.19

Alternatively, you can use apt-cache policy to verify the installation:

apt-cache policy yarn
Install Yarn on Debian 11

4. Create a Sample Project with Yarn Command

Let’s demonstrate how to use the Yarn package manager by creating a new project and managing dependencies.

To create a new Yarn project, use the yarn init command. Here, we’ll create a project named my_project:

yarn init my_project

The command will prompt you for project details. You can press Enter to accept the defaults or provide your own information.

This will look like this:

Create a Sample Project with Yarn Package Manager

This will generate a basic package.json file containing the provided information. You can modify this file as needed.

You can also initialize a Yarn project in an existing directory. Navigate to the directory and run:

yarn init

To add a package as a dependency, use the yarn add command:

yarn add [package_name]

This installs the package and its dependencies, updating the package.json and yarn.lock files. If only the package name is specified, Yarn installs the latest version.

To install a specific version or tag, use:

yarn add [package_name]@[version_or_tag]

To upgrade packages, use:

yarn upgrade
yarn upgrade [package_name]
yarn upgrade [package_name]@[version_or_tag]

Without a package name, the command updates all dependencies to the latest version within the ranges specified in package.json. Otherwise, only the specified packages are updated.

To remove a package dependency, use the yarn remove command:

yarn remove [package_name]

This updates the package.json and yarn.lock files accordingly.

To install all dependencies specified in the package.json file in an existing project, use:

yarn

Or

yarn install

Conclusion

Yarn simplifies JavaScript package management by providing a faster and more reliable alternative to npm. It ensures consistency across projects and streamlines the process of installing, updating, and organizing code libraries. This tutorial has shown you how to Install Yarn on Debian 11 and how to use it for basic project management.

Hope you enjoy it, you may also like these articles:

Fix sort_buffer_size for Cacti

Install Apache Kafka on Debian 11

How to Install Webmin on Debian 12

Run Apache Maven on Debian 12

Install 7Zip on Debian 12

Installing ONLYOFFICE Desktop Editors on Debian 12

Install OpenLiteSpeed Debian 12

Install Tomcat on Debian 12

Alternative Installation Methods for Yarn on Debian 11

While the above method using the official Yarn repository is the recommended approach, there are alternative ways to Install Yarn on Debian 11. Here are two alternative methods:

1. Installing Yarn via npm (Not Recommended for Production)

This method involves using npm (Node Package Manager), which comes bundled with Node.js, to install Yarn globally. Although seemingly straightforward, this is generally not recommended for production environments. Installing Yarn through npm can sometimes lead to version conflicts or unexpected behavior, especially if you’re managing multiple Node.js projects with varying dependencies. The core reason is potential conflicts in how npm and Yarn manage their dependencies and resolve version constraints.

To install Yarn on Debian 11 via npm, run the following command:

sudo npm install -g yarn

This command installs Yarn globally, making it accessible from any directory in your terminal. After the installation, verify it by checking the Yarn version:

yarn -v

While this approach is simpler, it’s essential to be aware of the potential conflicts it can introduce. It’s often better suited for quick testing or development environments where strict dependency management isn’t critical.

Example illustrating potential conflicts:

Suppose you have Project A that depends on Yarn version 1.x and Project B that relies on Yarn version 2.x. If you install Yarn using npm globally, all projects will use the same global version. This might cause Project A to break if it is not compatible with Yarn 2.x. Using the Debian package manager ensures that the system dependencies are correctly managed, which reduces such conflicts.

2. Using a Snap Package

Snap is a package management system developed by Canonical, the company behind Ubuntu. It allows you to install applications in a sandboxed environment, which can help prevent conflicts with other software on your system. While not a traditional Debian package, it offers a way to install Yarn.

To install Yarn on Debian 11 using Snap, you first need to ensure that Snapd is installed. If it’s not already installed, you can install it using:

sudo apt update
sudo apt install snapd

After Snapd is installed, you can install Yarn using the following command:

sudo snap install yarn

Once installed, verify the installation by checking the version:

yarn -v

Explanation:

Snap packages are self-contained and include all the dependencies needed to run the application. This means that you don’t have to worry about conflicts with other software on your system. However, Snap packages can be larger than traditional Debian packages, as they include all the dependencies.

Important considerations when using Snap:

  • Snap packages are typically larger in size compared to traditional Debian packages.
  • Snap packages run in a sandboxed environment, which may limit access to certain system resources.
  • Updating Snap packages is handled automatically by the Snapd service.

Choosing the right installation method depends on your specific needs and environment. For most users, the official Yarn repository method is the most reliable and recommended approach. However, the alternative methods described above may be suitable in certain situations. Remember to weigh the pros and cons of each method before making a decision. Understanding how to install Yarn on Debian 11 through different ways is valuable.

Leave a Reply

Your email address will not be published. Required fields are marked *