Enter your email address below and subscribe to our newsletter

How to Install Yarn in Ubuntu

Learn how to install the latest version of Yarn in Ubuntu along with how to use Yarn and how to remove it (if you want to).

Share your love

Yarn is a fast, reliable, and secure JavaScript package manager. It is also a popular alternative to npm for managing project dependencies.

But if you try to install Yarn using apt through sudo apt install yarn, it installs cmdtest instead of Yarn:

You can not install Yarn directly using apt in Ubuntu
Apt installs cmdtest instead of Yarn

But that does not mean you can not install Yarn in Ubuntu through apt. So in this tutorial, I will walk you through multiple ways you can install Yarn in Ubuntu. But before that, let’s take care of the prerequisites to install Yarn.

Prerequisites for installing Yarn in Ubuntu

To install Yarn in Ubuntu, you need NPM and Node.js installed in your system. While there are multiple ways to install Node.js and NPM on Ubuntu, for the sake of this tutorial, I will walk you through the fastest way.

The easiest way to install Node.js and NPM on Ubuntu is to use apt as shown here:

sudo apt update && sudo apt install nodejs npm

Once done, you can verify the installation by checking the installed version of Node.js and NPM:

node --version
npm --version
Install Prerequisites for installing Yarn in Ubuntu

Now, let’s take a look at how to install Yarn in Ubuntu.

3 ways to install Yarn in Ubuntu

There are 3 simple ways you can install Yarn in Ubuntu:

  1. Using NPM (officially recommended way)
  2. Using apt package manager
  3. Using Corepack (gives you the latest version of Yarn)

Let’s start with the first one.

This method is recommended by the official documentation of Yarn as it takes a single command for installation and gives you a seamless experience.

To install Yarn using NPM, all you have to do is execute the following command:

sudo npm install -g yarn
Install Yarn using NPM in ubuntu

You can check the installed version of Yarn by appending the -v flag with the yarn command as shown:

yarn -v
Check installed version of Yarn which was installed using NPM in Ubuntu

2. Install the latest version of Yarn using Corepack

Using Corepack is the easiest way you can get the latest version of Yarn in Ubuntu. That too with few steps.

If you don’t have Corepack installed on your system, then you can use the following command to install Corepack on your Ubuntu system:

sudo npm install -g corepack
Install Corepack to get the latest verison of Yarn in Ubuntu

Now, enable Corepack using the following command:

corepack enable

Finally, you can install the latest version of Yarn on Ubuntu through Corepack using the following:

corepack prepare yarn@stable --activate
Install the latest version of Yarn in Ubuntu using Corepack

Finally, you can check the installed version of Yarn:

yarn --verison
How to install the latest version of Yarn in Ubuntu

As you can see, it gave me the latest stable version of Yarn. Now, use the following command to set the Yarn binaries to the latest stable version:

yarn set version stable
Set Yarn binaries to the latest version

3. Using apt package manager

With a few extra steps, you can install Yarn using the apt package manager which is great news for someone like me who prefers using apt over other package managers.

To install Yarn using apt, you need to set up a Yarn repository on Ubuntu. For that purpose, first, add the GPG key using the following command:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
Add Yarn GPG key in Ubuntu

Next, add the Yarn repository using the following command:

echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
Add Yarn repository in Ubuntu

Finally, you can install Yarn using apt by executing the given command:

sudo apt update && sudo apt install yarn

Now, you have to add Yarn to the $PATH variable to save yourself from the -bash: /usr/local/bin/yarn: No such file or directory:

To add Yarn to the $PATH variable, you need to replace [version] with the installed Yarn version:

export PATH="$PATH:/opt/yarn-[version]/bin"

You can refer to the output of the sudo apt update && sudo apt install yarn command to know your Yarn version. When I checked my installation output, it gave me Yarn version 1.22.22-1:

Find Yarn verison

So my command to add Yarn 1.22.22-1 to the $PATH variable would look like this:

export PATH="$PATH:/opt/yarn-1.22.22-1/bin"
-bash: /usr/local/bin/yarn: No such file or directory error

Now, you check if the Yarn command is being recognised or by checking the installed version of Yarn itself (pretty generic but works):

yarn --version
Check installed version of Yarn installed using Apt in ubuntu

How to use Yarn in Ubuntu

In this section, I will walk you through some basic uses of Yarn which include the following:

  • Adding a dependency to the project
  • Upgrading a Yarn dependency
  • Removing a Yarn dependency
  • Installing additional dependencies

To demonstrate these basic uses, first, you need to create a directory and initiate a project. So let’s start with creating a directory.

I’m creating my-dir directory but you can change the name at your convenience:

mkdir my-dir

Now, switch to my-dir using the cd command as shown here:

cd my-dir

Next, set the Yarn version (stable) for the recently created Yarn directory:

yarn set version stable

Finally, execute the yarn command to set the current directory to be used for the Yarn project:

yarn
Create a Yarn project

1. Adding dependency to the Yarn project

To add a new package as a dependency to your project, use the yarn add command followed by the package name:

yarn add [package-name]

For example, to add the lodash utility library, I’d use the following command:

yarn add npm
Add dependency to the Yarn project

While adding a dependency, you can also specify an exact version or tag:

yarn add [package]@[version]
yarn add [package]@[tag]

You can also use the --dev or -D flag to install a package as a development dependency:

yarn add --dev [package]

2. Upgrade dependency

To upgrade a package to the latest version allowed by your package.json, you can use the upgrade flag as shown here:

yarn upgrade [package]

You can also upgrade the Yarn dependency to a specific tag or version using the following:

yarn upgrade [package-name]@version
yarn upgrade [package-name]@tag

You can also use the --latest flag to ignore the version range specified in package.json and jump to the newest version:

yarn upgrade --latest [package]

3. Remove a Yarn dependency

Removing a Yarn dependency is quite easy where all you have to do is use the remove flag as shown here:

yarn remove [package-name]

For example, if I want to remove npm, then I’ll use the following command:

yarn remove npm
Remove Yarn dependency

This will update your package.json and yarn.lock files. Other developers on the project can run yarn install to sync their own node_modules with the updated dependencies.

4. Install all dependencies

When you first clone a project or another developer adds new dependencies, you’ll need to install all the packages. To install all the dependencies at once, use the following command:

yarn install
Install additional Yarn dependencies

This will download and install all the dependencies listed in the package.json file, and populate your node_modules directory.

How to uninstall Yarn from Ubuntu

In this section I will explain how to uninstall Yarn based on the three installation methods previously covered. It will take fewer steps compared to the installation.

So let’s start with the removal of the Yarn installed through NPM.

1. Uninstall Yarn NPM package

If you installed Yarn as a NPM package, it can be remove using a single command.

Here’s what you need to execute to remove the Yarn NPM package:

sudo npm uninstall -g yarn
Uninstall Yarn from NPM in ubuntu

2. Uninstall Yarn from Corepack

If you used the Corepack to install the latest version of Yarn, then first you need to set the Yarn to the classic version using the following command:

yarn set version classic
Set Yarn to the classic version

Next, disable Corepak by executing the given command:

sudo corepack disable

Now, remove the Yarn directories by executing given commands one by one:

rm -rf ~/.yarn 
rm ~/.yarnrc
Uninstall Yarn which was installed through corepack

3. Uninstall Yarn using Apt

If you installed Yarn using external repository through apt, then you can remove Yarn using the apt remove command as shown here:

sudo apt remove yarn

If you want to remove binaries and configuration files, then you can use apt purge instead of apt remove command as shown:

sudo apt purge yarn

If you are not planning to install Yarn using the external repository, then you can remove the external repository using the given command:

sudo rm /etc/apt/sources.list.d/yarn.list

That’s it!

Conclusion

In this tutorial, I went through how you can install Yarn on Ubuntu covering methods such as using NPM to give you convinience, using Corepack to install the latest version of Yarn and apt to make sure everything can be manager through single package manager.

I’ve also mentioned some basic use of Yarn on Ubuntu and at the end, explained how you can uninstall Yarn for previously mentioned installation methods.

I hope you will find this guide helpful and if you have any queries, leave us a comment.

Share your love
Kabir
Kabir

A tech journalist whose life revolves around networks.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!