Facebook initiated a 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 installation, license verification, NPM compatibility, and multiple registries. Yarn also allows you to automate the installation, configuration, upgrade, and removal of NodeJS packages and dependencies.
In this article, we will explain how to install Yarn on a Debian system through the official APT repository. We will also learn how to manage package dependencies through Yarn.
We followed the procedure mentioned in this article on a Debian 10 system.
Step 1: Install Curl to import a key for yarn
To import the GPG keys for the Yarn project, we will use the curl utility. Open the Debian command line to install curl. Click on the “Actions” tab in the upper left corner of the desktop. Then in the search bar enter the keyword terminal. When the search result appears, click on the terminal icon to open it.
Now in the Terminal run the following command to install Curl:
$ sudo apt install curl
Enter your password and click To come in, Remember that only an authorized user has the right to install, uninstall and configure any software on the Linux system.
The system can now ask you for confirmation by providing you Y / N option. Hit Y and then To come in continue installation, after which Curl utility will be installed on your system.
Step 2: Import the GPG Yarn Key
Now use the Curl utility to import the Yarn GPG key. To do this, execute 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
After the GPG key has been imported, we will now add the Yarn repository to our system. This will help in installing Yarn, as well as getting updates and updates from the Yarn online store in the future.
$ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
Step 4: Updating the System Repository Index
Now in this step we will update the system repository index using the following command:
$ sudo apt-get update
This will allow you to install the latest version of the program from the Internet.
Step 5: Install the yarn
Once all the necessary conditions for the yarn are met, you can begin to install the yarn. Run the following command in the terminal to install Yarn on your system:
$ sudo apt-get install yarn
The system may ask you for confirmation by providing you Y / N option. Hit Y and then To come in continue installation. Yarn is now installed.
Step 6: Verify Installation
To check if the installation is completed, run this command in 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 look like this:
$ yarn init first_yarn_project
When you run the above command, the system will provide you with an interactive form with various questions, including the name of your project, version, description, license and, most importantly, the dependencies necessary for the project. Enter the answers to your questions. If you do not want to answer, you can simply press Enter to select the default values or leave them blank.
All 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 project information. It is located in the root directory of the system and can be edited at any time.
Managing NPM Dependencies Using Yarn
Let’s discuss how to build a yarn project and manage dependencies with it. It includes:
- Add dependency
- Dependency Update
- Dependency Removal
- Adding all the dependencies from the package.json file
A package is usually a folder with code and a package.json file containing information. To use another package, you first need to add it as a dependency in your Yarn project.
The following syntax can be used to add a package depending on 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 specify the version number along with the package name as follows:
$ yarn add [package_name]@[version_number]
For example, to add a specific version of 4.0.0 Gulp, the command would be:
$ yarn add [email protected]^4.0.0
If you do not mention the version of the package, it will automatically add the latest version of the package to the package.json file.
Now, if you look at the package.json file, it will look something like the one containing the added dependencies.
To update any dependency added earlier, 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 we added earlier, the command would look like this:
$ yarn upgrade gulp
If you want to change the version number of the added dependency, you can do this with the following command:
$ yarn upgrade [package_name]@[version_number]
Updating All Dependencies
To update all dependencies added to your project, use the following command in Terminal:
$ yarn upgrade
Each dependency in the package.json file will be updated in turn to the latest version.
To remove a project dependency, use the following syntax:
$ yarn remove
For example, to remove Gulp from a project, use the following command:
$ yarn remove gulp
After the dependency is removed, the package.json and yarn.lock files will be 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 the dependency to the list of dependencies.
To install the dependencies specified in the package.json file, use one of the following commands.
$ yarn install
To completely remove Yarn from your system, use the following command:
$ sudo apt purge yarn
This command will remove not only the yarn, but also the dependencies that 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 installation. To do this, first find the key using the following command:
$ apt-key list
Once you find the key, write down the last eight characters and use the following command to delete the key.
$ sudo apt-key del 86E50310
In addition, the GPG key can also be deleted via Software updates utility. Run it through the list of applications on your system. Transition to Authentication tab where you will see a list of keys from different software providers. Find and select the key for yarn and press the button Delete Button to remove a key from your system.
That is all there is to it! In this article, we learned how to install Yarn and manage dependencies through it. To learn more about yarn, visit the official yarn page: https://yarnpkg.com/en/docs.
How to install Yarn NPM Client on Debian and manage dependencies through it