Install and Use Yarn on Ubuntu 20.04

In this article, we want to teach you how to Install and Use Yarn on Ubuntu 20.04.

The Yarn was developed by Facebook in an attempt to resolve some of npm’s shortcomings. Yarn isn’t technically a replacement for npm since it relies on modules from the npm registry. Think of Yarn as a new installer that still relies upon the same npm structure. The registry itself hasn’t changed, but the installation method is different. Since Yarn gives you access to the same packages as npm, moving from npm to Yarn doesn’t require you to make any changes to your workflow.

How To Install and Use Yarn on Ubuntu 20.04

Before you start to install yarn, you need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article about the Initial Server Setup with Ubuntu 20.04.

Now follow the steps below to install Yarn on Ubuntu 20.04.

Installing Yarn on Ubuntu 20.04

First, you need to 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

when you have enabled the yarn repository, update your local package index with the following command:

sudo apt update

Then, install Yarn with the following command:

sudo apt install yarn

This command will also install Node.js.

Note: If you installed Node from NVM, skip the Node.js installation with:

sudo apt install --no-install-recommends yarn

Now verify your Yarn installation on Ubuntu 20.04 by checking its version:

yarn --version

In your output you will see:

Output
1.22.15

The version of Yarn may be different from here.

At this point, you have successfully installed Yarn on your Ubuntu 20.04. Now you can start using it.

Using Yarn

At this step, we will show you how to use Yarn by creating a new project and adding/removing dependencies.

To create a new Yarn project, you can use the yarn init command.

Here we create a project named my_project, you can choose another name for it:

yarn init my_project

This command will ask you some questions. Press enter to accept the defaults or fill them with your answers.

This will look like this:

Output
yarn init v1.22.15
question name (root): orca
question version (1.0.0): 0.0.1
question description: testing yarn
question entry point (index.js):
question repository url:
question author: olivia
question license (MIT):
question private:
success Saved package.json
Done in 107.06s.

This command will create a basic package.json file that contains the information you have provided.

You can modify this file any time you want.

Also, you can initiate a Yarn project in an existing directory on Ubuntu 20.04. To do this, switch to the directory and run the following command:

yarn init

To add a package as a dependency to your project you can use the following command:

yarn add [package_name]

With this command, you can install the package and any packages that it depends on. Also, it will update the project’s package.json and yarn.lock files.

If you use only the package name in the command, Yarn will install it in the latest version.

To install a specific version or tag, use the following command:

yarn add [package_name]@[version_or_tag]

Also, you can upgrade the packages with the following commands:

$yarn upgrade
$yarn upgrade [package_name]
$yarn upgrade [package_name]@[version_or_tag]

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.

To remove a package from your project dependency you can use the Yarn remove command on Ubuntu 20.04:

yarn remove [package_name]

This command will also update the project’s package.json and yarn.lock files.

In an existing project you can install all dependencies that are specified in the package.json file with the following command:

yarn

Or you can use:

yarn install

Conclusion

Yarn is becoming increasingly popular thanks to its superior performance, easy installation, and numerous convenient features.

Hope you enjoy this article about installing and using Yarn on Ubuntu 20.04.

May this article about Install and Use Yarn on AlmaLinux 8 be useful for you.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!