Yarn is the leading JavaScript package manager developed by Facebook in 2016 for the Node.js JavaScript runtime. An alternative to the npm package manager, Yarn was created in collaboration with Facebook, Exponent, Google, and Tilde (the company behind Ember.js) to solve problems around consistency, security, and performance with large code bases. Users can also a personal plugin for Yarn. In this guide we will learn how to install Yarn on Ubuntu 20.04.
In comparison to npm, Yarn includes the given functionality.
- Yarn can install packages from local cache.
- Yarn binds versions of the package strongly.
- Yarn uses checksum for ensuring data integrity, while npm uses SHA-512 to check data integrity of the packages downloaded.
- Yarn installs packages in parallel, while npm installs one package at a time.
Requirements
- 99RDP VPS/Dedicated Server(Running Ubuntu 20.04)
- Access To Terminal
- Node.js and npm installed
Steps To Install Yarn Required Packages On Ubuntu 20.04
The best way to manage Yarn is through Corepack, a new binary that comes with all Node.js versions from 16.10 onwards. It acts as an intermediary between you and Yarn and allows you to use different versions of the package manager on multiple projects without having to register the Yarn binary. Foollow the given steps to install Node.js and npm packages if it’s not installed on the system.
# sudo apt update
# curl -fsSL https://deb.nodesource.com/setup_19.x | bash – &&\ apt-get install -y nodejs
Once the installation is done we can verify it by running the version command.
# node -v
# npm -v
For us the current lates node version is 19.7.0 and for npm its 9.6.0. By default Corepack is included with all Node.js installs, but is currently opt-in. To enable it, run the following command:
# corepack enable
Now as all the package required for Yarn is installed we can now proceed with the installation.
Steps To Install The Latest Version Of Yarn On Ubuntu 20.04
To install the latest version of yarn we are going to use Corepack. Corepack is an experimental tool to help with managing versions of your package managers also acting as a point of contact between the user and Yarn.To install the latest version of Yarn run the given command in terminal.
# corepack prepare yarn@stable –activate
Once the package manger is activated we can check it version by running the given command.
# yarn –version
For us the current version is 3.4.1. This process installs the latest stable version of Yarn.
Steps To Create A Project With Yarn
In order to create a new Yarn project kindly browse to the project directory and run the given command.
# yarn init
Yarn initializes the project directory by creating two files. One is the package.json file which contains the project configuration and the other is the yarn.lock file which stores the dependency information.
Steps To Install Additional Package for Yarn
Yarn works through the package.json configuration file. This file records all the necessary packages for your project. When working on a project, you can add a package with additional dependencies. To do so you can run the given command in terminal.
# yarn install
root@ubuntu20:~# yarn install ➤ YN0000: ┌ Resolution step ➤ YN0000: └ Completed ➤ YN0000: ┌ Fetch step ➤ YN0000: └ Completed ➤ YN0000: ┌ Link step ➤ YN0000: └ Completed ➤ YN0000: Done in 0s 101ms
This will update and load all the dependencies for your project configuration.
Conclusion
In this guide we explained how to install node.js, npm, and yarn latest stable version for Javascript Project management and version control devlopment. Now you also know, how to setup your first project using yarn. To know more about Yarn follow up the official wiki page.