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 installs, license checks, compatibility with NPM, and multiple registries. Yarn also allows it to automate the process of installation, configuration, upgrading, and removal of NodeJS packages and dependencies.
In this article, we will explain how to install Yarn on a Debian system via its official APT repository. We will also learn how to manage package dependencies through Yarn.
We have performed the procedure mentioned in this article on a Debian 10 system.
Step 1: Installing Curl for importing 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 in the search bar, type the keyword terminal. When the search result appears, click on the Terminal icon to open it.
Now in the Terminal, execute the following command to install Curl:
$ sudo apt install curl
Type the password and press Enter. Remember that only an authorized user has the right to install, uninstall, and configure any software on a Linux system.
Now the system might ask you for confirmation by providing you with a Y/n option. Hit y and then Enter to continue installation, after which the Curl utility will be installed on your system.
Step 2: Import Yarn GPG key
Now use the Curl utility in order to import the Yarn GPG key. Run the following command in Terminal to do so:
$ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
Use the right-click menu to copy and paste this command instead of typing it.
Step 3: Add Yarn APT repository
After the GPG key has imported, now we will add the Yarn repository to our system. It will help in Yarn installation and will also enable to receive update and upgrades from the 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: Updating the system’s Repository Index
Now in this step, we will update the system repository index using the following command:
$ sudo apt-get update
It will let you install the latest version of a program from the internet.
Step 5: Install Yarn
Once all the prerequisites for the Yarn have completed, you can proceed towards the installation of Yarn. Execute the following command in Terminal to install Yarn in your system:
$ sudo apt-get install yarn
The system might ask you for confirmation by providing you with a Y/n option. Hit y and then Enter to continue the installation. Yarn is installing now.
Step 6: Verify Installation
To verify if the installation is completed, run this command in Terminal:
$ yarn --version
$ yarn –v
The above output indicates the latest version of Yarn 1.22.0 has 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 instance, to create a project by the name of “first_yarn_project”, the command would be:
$ yarn init first_yarn_project
When you run the above command, the system will present you with an interactive form with various questions, including your project name, version, description, license, and most significantly, the dependencies required for the project. Enter answers for the asked questions. If you do not want to answer, you can simply hit Enter to choose the default ones or to leave them blank.
All your answers are saved in a file name package.json which looks similar to this:
All Yarn packages contain this package.json file which holds information about the project. It is located at the root directory of the system and can be edited at any time.
Managing NPM Dependencies with Yarn
Let’s discuss how to build a yarn project and manage dependencies with it. It involves:
- Adding a dependency
- Upgrading a dependency
- Removing a dependency
- Adding all dependencies from the package.json file
Adding a Dependency
A package is usually a folder with code and a package.json file containing the information. In order to use another package, you will need to 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 instance, 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 instance, to add specific version 4.0.0 of Gulp, the command would be:
$ yarn add [email protected]^4.0.0
If you do not mention the version of a package, it automatically adds the latest version of the package to the package.json file.
Now if you view the package.json file, it will look similar to this containing the added dependencies.
Upgrading a Dependency
In order to upgrade any dependency that you have added earlier, use the following command syntax:
$ yarn upgrade [package_name]
This will upgrade the package to the newest version available, or based on the version range specified in the.json package file.
For instance, to upgrade Gulp package that we have added earlier, the command would be:
$ yarn upgrade gulp
If you want to change the version number of an added dependency, it can be done via the following command:
$ yarn upgrade [package_name]@[version_number]
Upgrading all dependencies
In order to upgrade all dependencies added to your project, use the following command in Terminal:
$ yarn upgrade
Each dependency in the package.json file will be upgraded one by one to the latest version.
Removing a Dependency
To remove a project dependency, use the following syntax:
$ yarn remove
For instance, to remove the Gulp from the project, use the following command:
$ yarn remove gulp
Once the dependency is removed, the package.json and the yarn.lock files will be updated.
Installing all dependencies
You probably know that your package.json file can be edited manually. For instance, you can edit the package.json file for adding a dependency to the list of dependencies.
To install the dependencies specified inside the package.json file, use one of the following commands.
$ yarn install
In order to completely remove Yarn from your system, use the following command:
$ sudo apt purge yarn
This command will not only remove the Yarn but also the dependencies that you have added with it.
To remove 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 have added during the installation. To do so, first find the key using the following command:
$ apt-key list
Once you find the key, note down the last eight characters and use the command as follows to delete the key.
$ sudo apt-key del 86E50310
Alternatively, the GPG key can also be removed through the Software & Updates utility. Launch it through the Applications list in your system. Shift to the Authentication tab where you will see a list of keys from different software providers. Find and select the key for Yarn and click the Remove button to remove the key from your system.
That is all there is to it! In this article, we have learned how to install Yarn and manage dependencies through it. To find more detail about Yarn, visit Yarn official page: https://yarnpkg.com/en/docs.