The yarn was developed to solve consistency, security, and performance problems with large codebases.
Steps to install Yarn on Ubuntu 22.04 LTS Linux
We can execute the commands given in this tutorial on all Linux distros based on Ubuntu or Debian operating systems.
1. Update Ubuntu 22.04
It is very important to run the system update commands before installing any software on Linux; if you haven’t for a while. This not only installs the latest available updates but also rebuilds the APT package index cache.
sudo apt update && sudo apt upgade
2. Install NodeJs
Yarn is a package manager for Nodejs, hence, if you are planning to use Yarn then you should have Nodejs as well. Those who already have it can skip this step and move to the next one. Whereas users who don’t have the Ndoejs, can install its latest version using the command given below:
sudo apt install curl
curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
Install Nodejs and NPM:
sudo apt install nodejs
3. Enable Corepack
Corepack is a package that comes by default with Node.js versions greater than 16.10. The latest stable version while doing this article of NodeJs was 18.x. Hence, we have the Corepack package already on our system, just need to enable it.
Note: Users who are using Nodejs version older than 16.10 can use NPN itself to install Corepack, here is the command to get that.
npm i -g corepack
4. Install the latest Yarn version on Ubuntu 22.04
After enabling the Corepack we can easily install and activate the latest stable version of Yarn. Corepack is work as an intermediary between you and Yarn, therefore we can use it to manage different Yarn package manager versions across multiple projects.
corepack prepare yarn@stable --activate
The above command will activate the latest version of the Yarn for your project.
5. Check Version
Once the installation is completed, we can check which version of the package manager is on our system. For that use:
6. Install and Switch to the older Yarn version (optional)
According to our project need, we can use the Corepack to install and activate the older versions of the Yarn package manager. For example, if you want Yarn version 3.1.1 for some of your projects then the command will be:
corepack prepare firstname.lastname@example.org --activate
Set this version as default to use:
sudo yarn set version 3.1.1
7. Use yarn to install or remove packages
Now, let’s have a glimpse of how to use the Yarn command line to install or remove packages. Here is the command syntax:
yarn add package
yarn add react
Check out the official page to search for popular packages to add using Yarn.
We can also control the package’s installation by version and tags.
To add some particular versions of packages, we can use this syntax:
yarn add package@version
Whereas by Tag:
yarn add package@tag
For Upgrading a dependency:
yarn up package
By Version or Tag:
yarn up package@version
yarn up package@tag
Removing a dependency package
Do you want to remove any installed package? Then, here is the command syntax to follow:
yarn remove package
yarn remove react
8. How to upgrade Yarn
In case later you want to update the existing version of your package manager to the latest stable one then use:
yarn set version stable
9. Uninstall Yarn
If you think NPM is enough and don’t want the Yarn package manager on your Ubuntu 22.04 then to remove it we can use the Corepack:
sudo corepack disable yarn
Yarn is an alternative to the NPM package manager to full fill the dependencies required by the Nodejs projects. It was developed to solve consistency, security, and performance problems with large codebases. As compared to NPM it offers better performance and parallel installation of packages.
Yes, we can use NPM over Yarn to install required dependencies or vice versa.
NPM and Yarn both can be used together. NPM is a default package manager for Nodejs, hence it is installed automatically with Nodejs whereas those who want to use Yarn can either use NPM or Corepack to install it.
⇒ 3 ways to install Nodejs & NPM on Ubuntu 22.04 LTS Jammy
⇒ Install Foxit PDF Reader on Ubuntu 22.04 LTS Linux
⇒ Create a New User in Ubuntu 22.04 or 20.04 using GUI or Terminal
⇒ How to install Memcached on Ubuntu 22.04 LTS Server
⇒ Install PostgreSQL 13 on Ubuntu 22.04 LTS Linux