How to Install Yarn on Ubuntu

How to Install Yarn on Ubuntu

Understanding how to install Yarn on their systems is essential for developers aiming to streamline their project workflows in the Node.JS platform. Yarn offers a powerful alternative to default package managers (npm or Node Package Manager), ensuring speed, reliability, and consistency. It allows you to install, uninstall, and update packages. Suppose you’re running Ubuntu and want to leverage the benefits of Yarn. In that case, this guide will provide a walkthrough to help you seamlessly install Yarn and integrate it into your development routine.

What is Yarn?

Yarn is a package manager for JavaScript, introduced to address some of the challenges associated with npm (Node Package Manager), which was the default package manager for Node.js. Yarn was developed by Meta in collaboration with Exponent, Google, and Tilde.

Here are some key features and benefits of Yarn:

  1. Deterministic Dependency Resolution. Yarn uses a yarn.lock file to lock down the versions of package dependencies, ensuring that every install results in the exact same file structure in node_modules across all systems.
  2. Speed. Yarn caches every package it downloads, so it doesn’t need to repeatedly download them. This, in combination with parallelized operations, makes Yarn faster in many scenarios compared to npm.
  3. Security. Yarn uses checksums to verify the integrity of every package before its code is executed, ensuring that the package content is consistent and hasn’t been tampered with.
  4. Workspaces. It offers a feature called workspaces, which allows users to manage multiple projects with shared dependencies more effectively, making it a popular choice for monorepos.
  5. Compatibility with npm.  It is compatible with the npm registry, meaning that it can install any package that is available on npm.

Installing Yarn on Ubuntu

Here’s a step-by-step guide to installing Yarn on Ubuntu:

#1 Update package lists and install prerequisites

Before installing any new package on Ubuntu, it’s a good habit to update the package lists for the latest version of the repository.

sudo apt update

#2 Install the required dependencies

This step ensures that curl, a tool to fetch content from servers, and gnupg, a tool for secure communications, are installed.

sudo apt install curl gnupg -y

#3 Add Yarn APT repository to your system

This allows you to install Yarn and receive updates from Yarn’s own repository.

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

#4 Update package lists again

Since you’ve added a new repository in the previous step, you’ll need to update the package lists once more.

sudo apt update

#5 Install Yarn

Now, you can install Yarn, using this command:

sudo apt install yarn

#6 Verify Installation

After the installation, it’s good to check the installed version of Yarn to ensure it was installed correctly.

yarn --version

And that’s it! Yarn is now successfully installed on your Ubuntu system, and you’re ready to use it to manage your JavaScript project dependencies.

Conclusion

Want to run Ubuntu in a virtual environment? With Gcore Cloud, you can choose from Basic VM, Virtual Instances, or VPS/VDS suitable for Ubuntu:

Choose an instance

Subscribe to our newsletter

Stay informed about the latest updates, news, and insights.