How to Install Yarn in Ubuntu?

In this article, we’ll learn how to install Yarn on Ubuntu. Yarn which stands for Yet Another Resource Negotiator is a package manager cum project manager for all your personal and professional needs. It was built by Facebook as an alternative to npm, fixing some of the security and speed issues npm had at the time. Since then, it has been made open-source and is supported by huge companies like Google. By default, Yarn supports Node.js just like npm but through plugins, its support can be extended to other languages.

Also read: Ubuntu Package Manager – All about dpkg and apt

Install Yarn on Ubuntu Using APT Package Manager

Usually, the default Ubuntu repository does not comes with Yarn. So you can simply add the official Yarn repository to the list of sources and then install Yarn.

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
sudo apt-get update && sudo apt-get install yarn

This will install Yarn on your Ubuntu device.

Install Yarn on Ubuntu Using NPM

If you are already an npm user and want to switch to Yarn, you can simply execute :

npm install -g yarn

This will install Yarn on your device globally.

Verify the Installation

To verify if the installation was successful, you can simply run

yarn --version

This will print out the version number indicating that Yarn has been installed.

Upgrading from 1.x to 2.x

Two versions of Yarn have been released till now and it is recommended by developers to migrate to the latest versions.

npm by default installs the older version of Yarn. A simple way of confirming this is running yarn --version and if the output is in the format, 1.x.x (like 1.22.10) then you are on the older version.

To migrate a specific project to the new version, go to the specific project directory and execute

yarn set version berry
Note : Berry" is the codename for the Yarn 2 release line. 

To permanently upgrade to the newer version, run

yarn set version latest

Conclusion

Now you know how to install Yarn which you can use to manage your code snappily with ease. To learn more about Yarn and how to use it, you can read the official Yarn documentation.