How to install Yarn on Ubuntu 20.04

s2 0

Yarn is a npm compatible JavaScript package manager that helps you automate the process of installing, updating, configuring and removing npm packages. It caches every download package and speeds up the installation process by parallelizing operations.

In this tutorial we will explain how to install it yarn
on Ubuntu 04/20. We’ll also go through the basic yarn commands and options.

Installing Yarn on Ubuntu

Installing Yarn on Ubuntu is pretty straightforward. We activate the official yarn repository, import the GPG key of the repository and install the package. The repository is consistently maintained and provides the latest version.

Import the repository’s GPG key and add the Yarn APT repository to your system by running the following commands:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

Once the repository is activated, update the package list and install yarn.

sudo apt updatesudo apt install yarn

The above command will also install Node.js. If you installed Node over nvm, skip the Node.js installation with:

sudo apt install --no-install-recommends yarn

When done, verify the installation by printing out the Yarn version:

yarn --version

The output looks something like this:

1.22.4

The version installed on your system may differ from the one shown above.

That’s it! You have successfully installed Yarn on your Ubuntu computer and are ready to use it.

Use of yarn

Now that Yarn has been installed on your Ubuntu system, let’s explore some of the most common Yarn commands.

Create a new project

First, create a directory for your application and navigate into it:

mkdir ~/my_project && cd ~/my_project

To create a new project, run yarn init:

yarn init my_project

The command asks you several questions. Enter the information when prompted or accept the defaults:

yarn init v1.22.4
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.

When finished, the script creates a basic package.json File with the information provided. You can open and edit this file at any time.

Add dependency

To add an npm package to the project dependencies, use the yarn add Command followed by the package name:

yarn add [package_name]

The above command updates the package.json and yarn.lock Files.

If only the package name is given, Yarn will install the latest version by default. Use the following syntax to install a specific version or tag:

yarn add [package_name]@[version_or_tag]

Upgrade dependency

To update the packages, use one of the following commands:

yarn upgradeyarn upgrade [package_name]yarn upgrade [package_name]@[version_or_tag]

If a package name is not specified, the command updates the project dependencies to their latest version based on the version range specified in the package.json file. Otherwise, only the specified packages will be updated.

Remove dependency

Use the yarn remove Command followed by the package name to remove a dependency:

yarn remove [package_name]

The command removes the package and updates the project files package.json and yarn.lock Files.

Install all project dependencies

To install all project dependencies that are included in the package.json File, run:

yarn

or

yarn install

diploma

We showed you how to install Yarn on your Ubuntu machine. For more information on yarn, see theirs documentation
Page.

If you have any questions or feedback, feel free to comment below.

Tags: