What is a Yarn?
Yarn’s speed is because of the proficient utilization of the cache that removed the need to download libraries more than once. In the meanwhile, checksum confirms the integrity of the libraries to guarantee Yarn is extremely secure and reliable.
It was made to take care of a lot of issues with the npm, for example, speeding up the packages installation process by parallelizing tasks and reducing mistakes identified with the network connectivity.
Let us look at how to install Yarn on Ubuntu 18.04
How to Install Yarn on Ubuntu 18.04
The most effective approach to install Yarn on Ubuntu 18.04 is through its repository. We will most likely be able to update the application along with the rest of the system effectively.
Access Your Server Through SSH
We must be able to access our server running on Ubuntu 18.04.
Add the GPG Key
Use the following command:
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add –
With the above command, we have added the important GPG key to ensure that the downloaded packages are authentic.
Add the Yarn Repository
Next we can add the yarn repository with the following command:
echo “deb https://dl.yarnpkg.com/debian/ stable main” | sudo tee /etc/apt/sources.list.d/yarn.list
1. Update System and Install Yarn on Ubuntu
Next we need the refresh the origins of the software and install Yarn on Ubuntu using APT.
sudo apt update
sudo apt install yarn nodejs
2. Check Yarn’s Version
To check if the installation was successful, we can output the version that has been installed:
yarn init v1.15.2
And as we can see, Yarn’s installation has been successful and we’re ready for work.
How to Use Yarn on Ubuntu
First of all, we have to start a new project with the following command:
yarn init project_name
Next, we will be asked some questions related to the project that will later create the .json file where the dependencies we set will be. If we want Yarn to use the default information for the file, we can press enter for each question.
To add a dependency to our project, we just use the following command:
yarn add [package]
It is possible to define a specific version of a package or library as a dependency of our project.
yarn add [package]@[version]
To update a package, we use the upgrade directive and the package name.
yarn upgrade [package]
We can also specify the version of the package to be updated:
yarn upgrade [package]@[version]
we can remove a package from the project with the command:
yarn upgrade [package]
Finally, to install all the defined dependencies, the following command is used:
Don’t forget that these dependencies are defined in the packages.json file.