It was created to solve a set of problems with the npm such as speeding up the packages installation process by parallelizing operations and reducing errors related to network connectivity.
In this tutorial, we will discuss how to install Yarn on your Ubuntu 18.04 system via the Yarn APT package repository. The official Yarn repository is consistently maintained and provides the most up-to-date version. We will also go through the basic Yarn commands and options.
Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges.
Installing Yarn on Ubuntu
Follow the steps below to install Yarn on your Ubuntu 18.04 system:
The first step is to enable the Yarn repository. Start by importing the repository’s GPG key using the following curl command:
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
Add the Yarn APT repository to your system’s software repository list by typing:
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
Once the repository is added to the system, update the package list, and install Yarn, with:
sudo apt update
If you already don’t have Node.js installed on your system, the command above will install it. Those who are using nvm can skip the Node.js installation with:
sudo apt install --no-install-recommends yarn
To verify that Yarn installed successfully, run the following commands which will print the Yarn version number:
At the time of writing this article, the latest version of Yarn is version
Now that you have Yarn installed on your Ubuntu system, the next step is to explore some of the most common Yarn commands.
Creating a new project
To create a new project, use the
yarn init command as shown below:
yarn init my_yarn_project
The init script will ask you several questions. You can either answer or press
enter to use the default values.
yarn init v1.17.3 question name (vagrant): Linuxize question version (1.0.0): 0.0.1 question description: Testing Yarn question entry point (index.js): question repository url: question author: Linuxize question license (MIT): question private: success Saved package.json Done in 20.18s.
Once completed, the script will create a basic
package.json file containing the information you provided. You can later open and edit this file.
If you want to use another package in your project, you need to add it to the project dependencies. To do so, use the
yarn add command followed by the package name:
yarn add [package_name]
The command above will also update the
yarn.lock files, so anyone working on this project when running
yarn will get the same dependencies.
You can also specify the package version or package tag:
yarn add [package_name]@[version_or_tag]
To upgrade the packages, use one of the following commands:
If no package name is given, the command will update the project dependencies to their latest version according to the version range specified in the package.json file. Otherwise, only the specified packages are updated.
yarn remove command followed by the package name to remove a dependency:
yarn remove [package_name]
This command will also update the project’s
Installing all project dependencies
To install all project dependencies that are specified in the
package.json file run:
We have shown you how to install yarn on your Ubuntu 18.04 machine. For more information about yarn visit the Yarn documentation page.
If you have any questions or feedback, feel free to comment below.