Facebook initiated the yarn project in collaboration with Google, Exponent and Tilde. Yarn is an open source NMP client that offers more advanced package management features and benefits than the standard NMP client. Its features include fast and reliable installations, license checks, compatibility with NPM, and multiple registrations. Yarn also enables the process of installing, configuring, updating and removing NodeJS packages and dependencies to be automated.
In this article we explain how to install Yarn on a Debian system through its official APT repository. We’ll also learn how to manage package dependencies through yarn.
We performed the procedure mentioned in this article on a Debian 10 system.
Step 1: Install Curl to Import Key for Yarn
To import GPG keys for the Yarn project, we will use the curl utility. Open the Debian command line to install curl. Go to the Activities tab in the top left corner of your desktop. Then enter the keyword in the search bar terminal. When the search result appears, click the Terminal icon to open it.
Now run the following command in Terminal to install Curl:
$ sudo apt install curl
Enter the password and press Enter. Remember that only an authorized user has the right to install, uninstall and configure software on a Linux system.
Now the system can ask you for confirmation by giving you a Yes No Opportunity. Blow and and then Enter to continue the installation. After that, the Curl utility will be installed on your system.
Step 2: import Yarn GPG key
Now use the Curl utility to import the Yarn GPG key. To do this, run the following command in the terminal:
$ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
Use the context menu to copy and paste this command instead of typing it.
Step 3: add Yarn APT repository
Now that the GPG key has been imported, let’s add the Yarn repository to our system. It helps with Yarn installation and also makes it possible to get updates and upgrades from Yarn internet repository in the future.
$ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
Step 4: Update the system’s repository index
In this step we now update the system repository index with the following command:
$ sudo apt-get update
This allows you to install the latest version of a program from the Internet.
Step 5: install yarn
Once all the requirements for the yarn are met, you can proceed to the installation of yarn. Run the following command in the terminal to install Yarn in your system:
$ sudo apt-get install yarn
The system may ask you for confirmation by giving you a Yes No Opportunity. Blow and and then Enter to continue the installation. Yarn will now be installed.
Step 6: check installation
To check that the installation is complete, run this command in the terminal:
$ yarn --version
$ yarn –v
The above output indicates that the latest version of Yarn 1.22.0 is installed on our system.
Create a new yarn project
Now to create a new yarn project, use the following syntax:
$ yarn init [project_name]
For example, to create a project named “first_yarn_project” the command would be:
$ yarn init first_yarn_project
When you run the above command, the system will show you an interactive form with various questions including project name, version, description, license and most importantly the dependencies required for the project. Enter answers to the questions asked. If you don’t want to reply, you can just hit Enter to choose the default settings or leave them blank.
All of your answers are saved in a file called package.json, which looks something like this:
All yarn packages contain this package.json file, which contains information about the project. It is located in the root directory of the system and can be edited at any time.
Manage NPM dependencies with Yarn
Let’s discuss how to create a yarn project and use it to manage dependencies. It contains:
- Add dependency
- Update a dependency
- Remove a dependency
- Add all dependencies from the package.json file
Adding a dependency
A package is usually a folder of code and a package.json file that contains the information. To use another package, you must first add it as a dependency in your yarn project.
The following syntax can be used to add a package to the dependencies in the package.json file:
$ yarn add [package_name]
For example, to add Lodash as a dependency, use the following command:
$ yarn add lodash
You can also include the version number along with the package name as follows:
$ yarn add [package_name]@[version_number]
For example, to add a specific version 4.0.0 of Gulp, the command would be:
$ yarn add [email protected]^4.0.0
If you don’t mention the version of a package, the latest version of the package is automatically added to the package.json file.
Now if you look at the package.json file, it looks similar to this one with the dependencies added.
Update a dependency
To update a previously added dependency, use the following command syntax:
$ yarn upgrade [package_name]
This will update the package to the latest available version or based on the version range specified in the .json package file.
For example, to update the gulp package that was added earlier, the command is:
$ yarn upgrade gulp
If you want to change the version number of an added dependency, you can do so using the following command:
$ yarn upgrade [package_name]@[version_number]
Update all dependencies
To update any dependencies added to your project use the following command in the terminal:
$ yarn upgrade
Each dependency in the package.json file is sequentially updated to the latest version.
Remove a dependency
Use the following syntax to remove a project dependency:
$ yarn remove <package_name>
For example, to remove the gulp from the project, use the following command:
$ yarn remove gulp
Once the dependency has been removed, the package.json and Garn.lock files are updated.
Install all dependencies
You probably know that your package.json file can be edited manually. For example, you can edit the package.json file to add a dependency to the list of dependencies.
Use one of the following commands to install the dependencies specified in the package.json file.
$ yarn install
To completely remove Yarn from your system, use the following command:
$ sudo apt purge yarn
This command not only removes the yarn, it also removes the constraints you added with it.
To remove the yarn repository from the sources.list.d directory, use the following command:
$ sudo rm /etc/apt/sources.list.d/yarn.list
You can even delete the GPG keys that you added during the installation. To do this, first find the key with the following command:
$ apt-key list
When you find the key, write down the last eight characters and use the following command to delete the key.
$ sudo apt-key del 86E50310
Alternatively, the GPG key can also be used via the Software & updates Utility. Start it from the application list in your system. Switch to Authentication Tab where you will see a list of keys from different software providers. Find and select the key for Yarn and click Removed Button to remove the key from your system.
That’s all! In this article we learned how to install yarn and use it to manage dependencies. For more information on Yarn, please visit the official Yarn page: https://yarnpkg.com/en/docs.