How to Install Yarn JS (Node) Package Manager on Debian 11

Yarn is a package manager for Javascript. It is meant to replace npm (node package manager). Yarn uses a different way to install packages. Instead of installing from the registry, it installs packages from other nodes in your network that have already downloaded the package and its dependencies. This can speed up installations, especially in projects with lots of node modules.

Yarn works exactly the same as npm, but with some benefits. First of all, it tells you which version of a package that was installed is compatible with your project. This makes it easier if you need to roll back or update packages. Secondly, it makes your packages more secure. Every package’s checksum is validated before it’s run by Yarn. This means that if a developer installs an outdated or corrupted package, Yarn will be able to detect the error, show the error in an easy-to-read format, and allow them to correct it before executing the code.

It isn’t easy to say whether the yarn is better than npm or vice versa. It’s just different. If you want an easy-to-use package manager that makes your packages more secure, the yarn might be the answer.

If you are a developer, chances are you have heard of Yarn. Installing yarn on Debian 11 can be tricky if you’re unfamiliar with the process, but this tutorial will walk you through the process step-by-step so that after reading this post, installing Yarn should be as easy as 1-2-3!

Prerequisites

  • A server running Debian 11.
  • A non-root user with sudo privileges.

Updating the System

In order for Debian to work smoothly, it is recommended that you update your system. This can be done with a few simple commands. Before we start updating the system, it is a good idea to do a quick check of all your current packages. If there are any out-of-date packages, then they will need to be updated before the upgrade process begins. This command will get rid of everything that’s not needed:

sudo apt-get autoremove

When you run this command, the systemd will automatically remove any unused libraries, unused languages, and unused applications from your machine. After running it, make sure to look through the list of packages and delete anything else that isn’t needed.

Once this is completed, you’re ready to run the update process. There are a couple of different ways you can do this, but the easiest is by using the apt-get command:

sudo apt-get update && sudo apt-get upgrade -y

After both processes have been completed, restart your machine.

Installing Yarn using Install Script

This procedure is actually quite straightforward. This is the simplest method for installing yarn on your Debian 11 system. However, the version of yarn may not be the latest one available.

First, run the following command to install Nodejs to allow for an easier installation process. We will add the NodeSource repository to the system with the curl command and install Nodejs with the APT command.

curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install nodejs

Now, we will install Yarn using the following command:

curl -o- -L https://yarnpkg.com/install.sh | bash

During the installation, press Enter to accept the permissions and continue. It may seem like nothing is happening, but yarn will be installed in the background.

After completing the steps above, your machine should have yarn installed on it and ready to use. To check if Yarn is installed correctly, use the following command:

yarn -v

Yarn will display the version number of itself that has been installed on your machine. If you receive a message saying that it can’t be found, then try restarting your machine and run the command above again.

Yarn JS Package Manager

Install Yarn Using the APT

You can also install it using the APT, which is more stable and up to date than other methods.

Run the following commands to add the key and Yarn repository to your Debian 11 system.

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
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

Now, run the following command to update APT’s cache and install Yarn.

sudo apt-get update && sudo apt-get install yarn

After completing the steps above, your machine should have yarn installed on it and ready to use. To check if Yarn is installed correctly, use the following command.

yarn -v

Testing the Yarn Installation

Now that Yarn has been installed on your machine, it’s time to test if that everything went correctly. In this step, we will create a simple project and download a package from the internet. This will test to make sure that Yarn is installed correctly and working properly on your machine.

To start, create a new directory named testyarn and move into it with the following command.

mkdir testyarn && cd testyarn

Once you’re in the directory, execute the yarn init command to initiate the Yarn project.

yarn init

When prompted, press Enter to accept all default options or fill in all the details as per requirements.

As a result of this command, the package.json file will be created for your project with a lot of default information about your project. This information doesn’t do much other than register the project as a package with Yarn, but you can edit it if you want to assign different values.

Use Yarn package manager

Now that you have created a project, use the yarn add command to add a package from Yarn’s official registry to your project. The syntax for this command is:

yarn add [PackageName]

Where: [PackageName] is the actual name of the package you want to add. You can get all the available packages by visiting the official website and searching for them.

For example, to add the lodash package to your project, run the following command.

yarn add lodash

As a result of this command, the lodash package will be downloaded to your project and saved in the node_modules folder. It will also create a new file named yarn .lock, which should not be edited.

Use Yarn to install Node JS packages

By default, the yarn add command will add the latest version number for the package that you’re trying to download. If you want, you can add a specific version number by adding it after the package name.

yarn add [package_name]@[version_or_tag]

For example, to add version 4.0.0 of lodash to your project, run the following command.

yarn add [email protected]

Sample output:

Yarn PM in use

Conclusion

In this tutorial, you have learned how to install Yarn on Debian 11 GNU/Linux. You have also learned how to use Yarn to download packages from the official Yarn registry. You can now begin using Yarn to install and download packages for your applications.