Introduction
Yarn is a fast, secure, and reliable dependency manager designed to resolve some of the shortcomings exhibited by npm. The application relies on npm registry modules and is not a replacement for the npm. It caches all the packages it downloads and parallelizes operations to boost resource utilization to expedite install times. This tutorial will show you how to install and use Yarn on Ubuntu 22.04 VPS or Dedicated server through the APT package manager. This repository is updated consistently and avails the most recent Yarn’s version.
Before You Begin
For this tutorial to run flawlessly requires a fully configured Ubuntu 22.04 at a good VPS hosting or dedicated server hosting service.
Method 01: Installing Yarn from Ubuntu apt repository
The first thing when installing Yarn on Ubuntu 22.04 is adding a Yarn repository together with the signing key the repository. Issue the commands below:
$ curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
$ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
Next, run the following commands to update your system and deploy Yarn on your system:
$ sudo apt-get update
$ sudo apt install yarn
The command above will automatically install Yarn if you have not installed Node.js on your server. However, if you are using the Node.js Version Manager, run the command below to skip the installation of Node.js:
$ sudo apt install--no-install-recommends yarn
Next, execute the command below to check the Yarn’s version:
$ sudo yarn --version
This will give you an output similar to the one below:
Method 2: Install latest version of Yarn on Ubuntu 22.04
To install the latest version of yarn, download the ‘Yarn’ tarball by writing out the following command in the Ubuntu 22.04 terminal:
For installing npm package:
$ sudo apt install npm
Now, download and install yarn:
As we can see in the above screenshot, yarn latest version 1.22.19 has been installed on this system.
Step 2 – Using Yarn
By now you have deployed Yarn on your system, next you need to explore the most commonly used commands for Yarn:
Creating Projects
To generate a Yarn project execute the command below:
$ sudo yarn init my_yarn_project
The above init command will prompt you to answer several questions. Answer the questions accordingly or hit ENTER to skip them.
Once you answer all the question, Yarn will generate a new package.json file featuring the details you provided. The file is readily available and can be opened and edited at any time.
Adding Dependencies
The yarn add is a command used to add a new package to dependencies of a project. The command used to add the package takes the form:
$ sudo yarn add [package_name]
For example, here we are installing ‘react’ package using yarn:
$ sudo yarn add react
This command will update the yarn.lock and package.json files. Updating these files enables everyone working on the project to get similar dependencies when using yarn. Besides, you can use the yarn add command and specify the version or tag for the package you want to add:
$ sudo yarn add [package_name]@[version_or_tag]
Upgrading The Dependencies
If you want to use the latest version of a specific package, you can execute the command below:
$ sudo yarn upgrade [package_name]
Alternatively, if you want to upgrade the dependency to a specific version or tag, issue the command below:
$ sudo yarn upgrade [package_name]@[version_or_tag]
Removing A Dependency
If you want to expunge a certain dependency, execute the command below:
$ sudo yarn remove [package_name]

Installing All Dependencies
If you want to install the project dependencies specified in a package.json you can execute one of the commands below:
$ sudo yarn
Or
$ sudo yarn install

Conclusion
That is It! You have installed Yarn on Ubuntu 22.04 VPS or Dedicated server and learned how to implement some of the most commonly used Yar commands. For in-depth details regarding Yarn usage, go to the official Yarn documentation website page.