Newsletter Subscribe
Enter your email address below and subscribe to our newsletter
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
:
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.
Table of Contents
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
Now, let’s take a look at how to install Yarn in Ubuntu.
There are 3 simple ways you can install Yarn in Ubuntu:
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
You can check the installed version of Yarn by appending the -v
flag with the yarn
command as shown:
yarn -v
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
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
Finally, you can check the installed version of Yarn:
yarn --verison
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
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 -
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
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:
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"
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
In this section, I will walk you through some basic uses of Yarn which include the following:
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
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
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]
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]
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
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.
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
This will download and install all the dependencies listed in the package.json
file, and populate your node_modules
directory.
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.
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
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
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
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!
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.