Install Yarn on Debian 11: Best Node JS Package Manager
In this tutorial, we’ll explore how to Install Yarn on Debian 11. Yarn is an npm-compatible JavaScript package manager that streamlines the process of setting up, updating, configuring, and removing npm packages. NPM, short for Node Package Manager, is the default package manager for Node.js. It’s recognized as the world’s largest software registry, widely used by open-source DevOps teams to publish and share their code. Mastering package management is crucial for efficient Node.js development.
To follow this guide on installing and using the Yarn package manager, you need to be logged into your Debian 11 server as a non-root user with sudo privileges. If you haven’t already, refer to our guide on Initial Server Setup with Debian 11 for instructions. Let’s delve into how to Install Yarn on Debian 11.
1. Debian 11 Node.js Installation
Before you can install Yarn, you need to have Node.js installed on your Debian 11 server. Begin by updating your local package index:
sudo apt update
Next, install the necessary packages by executing the following command:
sudo apt install software-properties-common apt-transport-https wget ca-certificates gnupg2 gcc make g++ curl -y
Now, import the NodeSource repository that corresponds to your needs, choosing between the current release or the LTS (Long Term Support) version:
# 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
With the Node.js repository configured, you’re ready to Install Yarn on Debian 11.
First, import the Yarn package manager 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 package sources:
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’s package list to recognize the new repository:
sudo apt update
3. Install Yarn Package Manager on Debian 11
With the repository in place, you can now install Yarn and Node.js with a single command:
sudo apt install yarn nodejs -y
Verify the successful installation of Yarn by checking its version:
yarn -v
**Output**
1.22.19
Alternatively, you can use the apt-cache policy
command to confirm the installed version and origin of the Yarn package:
apt-cache policy yarn

4. Create a Sample Project with Yarn Command
This section demonstrates how to use the Yarn package manager by creating a new project and adding/removing dependencies.
To create a new Yarn project, use the yarn init
command. Here, we’ll create a project named my_project
. Feel free to use any name you prefer:
yarn init my_project
The command will prompt you for information about the project. Press Enter to accept the defaults or provide your own values.
The process will look similar to this:
This command generates a basic package.json
file containing the provided information. You can modify this file at any time.
You can also initialize a Yarn project within an existing directory. Simply navigate to the directory and run the yarn init
command:
yarn init
To add a package as a dependency to your project, use the following command:
yarn add [package_name]
This command installs the specified package and any packages it depends on. It also updates the project’s package.json
and yarn.lock
files.
If you only provide the package name, Yarn installs the latest version.
To install a specific version or tag, use the following syntax:
yarn add [package_name]@[version_or_tag]
To upgrade packages, use the yarn upgrade
command:
$yarn upgrade
$yarn upgrade [package_name]
$yarn upgrade [package_name]@[version_or_tag]
Without a package name, the command updates all project dependencies to their latest versions, according to the version ranges specified in the package.json
file. Otherwise, only the specified packages are updated.
To remove a package from your project’s dependencies, use the yarn remove
command:
yarn remove [package_name]
This command updates the package.json
and yarn.lock
files accordingly.
In an existing project, you can install all dependencies specified in the package.json
file using either of these commands:
yarn
Or:
yarn install
Conclusion
In essence, Yarn is a JavaScript package management tool that simplifies the installation, updating, and organization of code libraries (packages) for your projects.
Yarn offers speed and reliability improvements over npm (Node Package Manager) by efficiently downloading packages and ensuring consistency across projects. At this point, you should know how to Install Yarn on Debian 11.
Hope you found this guide helpful! You might also be interested in 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 Solutions for Installing Yarn on Debian 11
While the above method using apt
and the Yarn repository is the recommended and most straightforward approach, there are alternative ways to Install Yarn on Debian 11, although they might be less convenient or suitable for all situations. Here are two such alternatives:
1. Installing Yarn via npm (Node Package Manager)
Since Yarn is designed to be a replacement for npm, it might seem counterintuitive to use npm to install Yarn. However, this is a viable option, especially if you already have Node.js and npm set up and prefer to avoid adding a new repository.
Explanation:
This method leverages npm’s global installation feature. When you install a package globally with npm, it becomes available system-wide. This makes Yarn accessible from any directory on your system. It’s important to note that the version of Yarn installed this way might not be the latest, and updates will need to be managed through npm.
Code Example:
sudo npm install -g yarn
After installation, you can verify the installation by checking the version:
yarn -v
This method is simple but relies on the npm version already installed on your system. Updating Yarn then becomes a matter of updating it through npm:
sudo npm update -g yarn
2. Installing Yarn by Downloading the Tarball
Another alternative is to download the Yarn tarball directly from the Yarn website and manually install it. This provides more control over the installation location and can be useful in specific scenarios, such as offline installations.
Explanation:
This method involves downloading the pre-built Yarn package, extracting it, and then manually placing the executable in a directory within your system’s PATH. This allows you to run Yarn from the command line. This approach avoids the package manager altogether, giving you direct control over the files.
Code Example:
First, download the latest Yarn tarball (replace [version]
with the actual version number):
wget https://yarnpkg.com/downloads/yarn-[version].tar.gz
Extract the tarball:
tar xzf yarn-[version].tar.gz
Move the extracted directory to /opt/yarn
(or any other desired location):
sudo mv yarn-[version] /opt/yarn
Create a symbolic link to the Yarn executable in /usr/local/bin
(or another directory in your PATH):
sudo ln -s /opt/yarn/bin/yarn /usr/local/bin/yarn
sudo ln -s /opt/yarn/bin/yarnpkg /usr/local/bin/yarnpkg
Verify the installation:
yarn -v
Remember to replace [version]
with the actual version number of the downloaded tarball. You can find the latest version on the official Yarn website.
This approach gives you full control, but also requires more manual steps for installation and updates. You’ll need to manually download and extract the new tarball and update the symbolic links when a new version of Yarn is released.
While these alternative methods are available, using the official Yarn repository through apt
as described in the main tutorial is generally recommended for its ease of use, automatic updates, and integration with the Debian package management system. However, understanding these alternatives provides flexibility and options for specific use cases.