How to install Yarn on Ubuntu

Installing Yarn on Ubuntu 18.04 20.04 22.04 Debian linux Step-by-Step Guide

Yarn is a popular JavaScript package manager that allows you to easily install and manage JavaScript packages and dependencies for your projects. In this comprehensive guide, I will walk you through the complete process of installing Yarn on Ubuntu. I will cover installing Yarn through both the APT package manager and direct downloads, troubleshooting any issues, as well as best practices for keeping Yarn up-to-date.

Introduction to Yarn

Yarn was created by Facebook in 2016 as an alternative to the npm package manager for Node.js. It aims to provide increased performance and reliability over npm. Some of the key features and benefits of Yarn include:

  • Faster and more reliable installations – Yarn caches every package it downloads so it never needs to download the same package again. This significantly improves install times. It also utilizes parallelism to maximize resource utilization during installs.
  • Offline mode – If you’ve installed a package before, Yarn will use the cached version allowing you to continue working offline.
  • Consistent installs – Yarn uses lockfiles and a deterministic algorithm to ensure that an install results in the same file structure across all machines.
  • Secure – Yarn installs packages securely by default with HTTPS and cryptographic verification.
  • Compatible with npm – Yarn still utilizes the same package.json structure as npm and can work seamlessly with npm installed packages. Most npm commands will still work with Yarn.

Yarn provides a performant, reliable and secure package manager for JavaScript projects. It is a great choice for managing dependencies when building Node.js applications.

Prerequisites

Before installing Yarn, you will need:

  • Ubuntu 18.04 or higher. These instructions should work on any recent release.
  • Non-root user account with sudo privileges.
  • Node.js 10 or higher.
  • npm installed. This usually comes preinstalled with Node.js.

You can verify these prerequisites by running:

$ lsb_release -a

This will display your Ubuntu version.

$ node -v

To check your Node.js version.

$ npm -v

For your npm version.

If you need to install or upgrade Node.js and npm, the easiest way is to use the NodeSource package archives. You can install the latest Node.js LTS release with:

$ curl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
$ sudo apt install -y nodejs

With the prerequisites met, we can now install Yarn.

Installing Yarn via APT

The recommended way to install Yarn on Ubuntu is by using the APT package manager. Yarn is included in the default Ubuntu repositories starting in Ubuntu 18.04.

To install Yarn:

$ sudo apt update
$ sudo apt install yarn

This will install the latest stable release of Yarn from the package repositories.

Verify it installed correctly:

$ yarn --version

If it displays the version number, Yarn has been successfully installed.

Using Direct Downloads

Yarn can also be installed via direct downloads from their official website. This allows you to install specific version releases.

First, download the .deb package for your Ubuntu version:

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

Or for a specific version:

$ 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 update
$ sudo apt install yarn=1.22.19-1

This will allow you to install a particular Yarn release like 1.22.19 rather than just the latest.

Check it installed correctly:

$ yarn --version

The direct downloads give you more control over the version you install.

Global vs Local Installations

By default, Yarn is installed globally on the system. This allows you to run the yarn command anywhere to manage JavaScript packages.

Yarn can also be installed locally within specific project directories. This allows you to use different Yarn versions for different projects.

To install Yarn locally, run:

$ npm install --global yarn

Within a project directory. Now only this project will have access to the yarn command.

The global installation is more convenient and recommended for most scenarios. But a local install gives you more control for individual projects.

Setting the Yarn Cache Directory

By default, Yarn stores its package cache globally in ~/.cache/yarn. This can gradually consume disk space over time as more packages are installed.

If you want to change the cache location, you can use the yarn config command:

$ yarn config set cache-folder /new/cache/folder

This will move the cache to a new directory like /opt/yarn/cache or to another partition with more storage space.

You can verify the new cache folder by checking the config:

$ yarn config get cache-folder

Setting a custom cache directory can help manage disk space usage.

Basic Yarn Commands

Now that Yarn is installed, you can begin using it to manage JavaScript packages. Some common commands include:

  • yarn init – Initializes a new project including the package.json file.
  • yarn install – Installs all dependencies for a project based on package.json.
  • yarn add [package] – Adds a new package to use in the project.
  • yarn remove [package] – Removes an existing package from the project.
  • yarn upgrade [package] – Upgrades an installed package to the latest version.
  • yarn why [package] – Shows why a package is installed and which other package depends on it.
  • yarn run – Runs a defined script from package.json.

To begin using Yarn for a new project:

$ yarn init
$ yarn install

This will initialize the project and install the current dependencies. Then you can begin adding additional packages as needed.

Refer to the Yarn CLI documentation for more usage details.

Upgrading Yarn

As new versions of Yarn are released, you may want to upgrade to the latest for bug fixes and new features.

If you installed Yarn using APT, run:

$ sudo apt update
$ sudo apt install yarn

This will upgrade Yarn to the latest version in the repositories.

For direct downloads, reinstall the new version .deb package:

$ sudo apt install yarn=NEW_VERSION_HERE 

You can find the latest version on the Yarn website.

Upgrading Yarn will ensure you have the latest bug fixes and features. Check the release notes when upgrading.

Troubleshooting Issues

Here are some common problems and solutions when installing Yarn:

Command not found – If yarn command returns “command not found”, ensure Yarn is installed and ~/.npm-global/bin is in your PATH env variable.

Permissions errors – Use sudo when globally installing Yarn. Don’t run the yarn command with sudo.

Old Node.js version – Make sure you have Node.js 10+ installed. Lower versions may not be compatible.

Package installation fails – Check the network and any proxies. Try manually installing the package with npm instead.

Can’t upgrade Yarn – Your current Yarn version may be too old to upgrade directly. Try removing .yarnclean and ~/.cache/yarn then reinstalling.

For any other issues, refer to the Yarn troubleshooting guide. The Yarn community forums are also a good place to find solutions.

With the basics covered in this guide you should be able to successfully install, configure and use Yarn for your JavaScript projects on Ubuntu.

Conclusion

Yarn is a valuable tool for front-end developers to efficiently manage JavaScript dependencies and packages. Learning how to properly install, configure and upgrade Yarn helps ensure you leverage its full capabilities.

Key points covered in this guide:

  • Installing Yarn via APT and direct downloads on Ubuntu
  • Global vs local installations for managing versions
  • Setting a custom cache directory location
  • Basic Yarn commands for daily usage
  • Upgrading Yarn to the latest versions
  • Troubleshooting common problems
  • Following best practices for stability and performance

With Yarn properly set up, you can take advantage of:

  • Faster, more reliable installations
  • Offline mode
  • Improved security

Combined with knowledge of the Yarn CLI and best practices, you can optimize your JavaScript workflow.

Yarn simplifies dependency management for building Node.js applications on Ubuntu. Following this tutorial will get you up and running with this useful tool.

LEAVE A COMMENT