For developers, the use of dependency managers is very common. With this, they avoid having to install dependencies of their projects manually and thus lose time. That’s why in this tutorial, we’ll show you how to install Yarn on Ubuntu to improve your VPS development workflow!
What is Yarn?
Yarn’s speed is thanks to the efficient use of the cache that removed the need to download libraries repeatedly. Meanwhile, checksum verifies the integrity of the libraries to ensure Yarn is very secure, leading to reliability.
Now, we will show how to install Yarn on Ubuntu 18.04.
How to Install Yarn on Ubuntu 18.04
The most efficient way to install Yarn on Ubuntu 18.04 is through its repository. We will be able to update the application along with the rest of the system easily:
1. Access Your Server Through SSH
To do this, we must access our server running Ubuntu 18.04. If you’re having trouble, check out our PuTTY tutorial.
2. Add the GPG Key
Run the following command:
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
With the above command, we have added the necessary GPG key to make sure that the downloaded packages are authentic.
3. Add the Yarn Repository
Then, 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
4. Update System and Install Yarn on Ubuntu
Now, all we need to do is refresh the origins of the software and install Yarn on Ubuntu using APT.
sudo apt update
sudo apt install yarn nodejs
5. Check Yarn’s Version
Then, 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
The basic use of Yarn is quite simple. First of all, we have to start a new project with the following command:
yarn init project_name
Then, 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]
However, it is also 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. For example:
yarn upgrade [package]
We can also specify the version of the package to be updated:
yarn upgrade [package]@[version]
Also, 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:
Remember that these dependencies are defined in the packages.json file.
On the other hand, it is recommended that you visit Yarn’s website and consult the official documentation. This way you will have more information, and be able to use this very popular tool to its fullest potential.