How to Install Yarn on Ubuntu 22.04 or 20.04

This guide will demonstrate how to install Yarn, a popular package manager, on Ubuntu 22.04 or 20.04. Yarn is an essential tool for developers, particularly those working with JavaScript projects. It streamlines package management, ensuring efficient and reliable handling of dependencies. The installation process is straightforward, making Yarn accessible for both novice and experienced developers.

Features of Yarn:

  • Fast Performance: Yarn caches every package it downloads, so it never needs to download the same package again. This enhances the installation process, making it faster and more efficient.
  • Reliable: Yarn uses checksums to verify the integrity of every installed package before its code is executed, ensuring security and consistency in your projects.
  • Secure: With its detailed but concise lockfile format and deterministic algorithm for installations, Yarn ensures that operations on different systems produce identical results, enhancing security.
  • Compatibility: Yarn works seamlessly with the npm registry, providing access to the same packages while offering additional features and improved performance.

To install Yarn on Ubuntu, the process involves adding the Yarn repository to your system, importing the repository’s GPG key for secure package management, and then executing the installation command. Once installed, Yarn is ready to manage your project dependencies with its robust and user-friendly features. The following sections will guide you through each step in detail, ensuring a smooth and successful installation of Yarn on your Ubuntu system.

Install Yarn on Ubuntu 22.04 or 20.04 via NodeSource

This section will guide you through installing Yarn using the NodeSource and Yarn’s repository for the latest version. This method is ideal for users who prefer the most up-to-date version of Yarn and Node.js LTS or the current stable release for their projects directly from the maintainers. Installing Yarn through the NodeSource repository ensures you’ll receive updates and bug fixes more promptly as they become available.

Step 1: Add NodeSource APT Repository on Ubuntu

First, you need to add the NodeSource repository to your Ubuntu system. This repository contains the latest versions of both Node.js and Yarn. Adding the repository ensures that your package manager recognizes the available packages and can fetch them during the installation process.

To add the NodeSource repository, run the following commands:

Current Release:

curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -

LTS Release:

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -

These commands download and execute a script, which adds the NodeSource repository to your system.

Additionally, you will also need to import the Yarn repository with the following command:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo gpg --dearmor -o /usr/share/keyrings/yarn-archive-keyring.gpg

Next, create a new file in the /etc/apt/sources.list.d/ directory to store the Yarn repository information:

echo "deb [signed-by=/usr/share/keyrings/yarn-archive-keyring.gpg] https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

Step 2: Install Yarn via NodeSource via APT Command

With the NodeSource and Yarn repositories added, you can now install Yarn using the apt package manager. The following command will fetch and install the latest Yarn version from the NodeSource repository, along with any required dependencies:

sudo apt update && sudo apt install yarn nodejs

After the installation is complete, you can verify the successful installation by checking the Yarn version:

yarn --version

The output should display the installed version number, confirming that Yarn is now installed on your Ubuntu system using the NodeSource repository.

Install Yarn on Ubuntu 22.04 or 20.04 via Node Version Manager (NVM)

In this section, we will explore the process of installing Yarn using the Node Version Manager (NVM). This method is particularly useful for developers who need to manage multiple Node.js versions on their system, as NVM allows you to switch between different Node.js environments effortlessly. Installing Yarn through NVM ensures that it is associated with a specific Node.js version, allowing you to maintain separate Yarn installations for different projects or environments.

Step 1: Install NVM on Ubuntu

First, you’ll need to install NVM on your Ubuntu system. NVM is a powerful tool that simplifies the process of installing and managing multiple Node.js versions.

To install NVM, use the official installation script by running one of the following commands:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash

or

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash

These commands download and execute the NVM installation script to set up NVM on your system.

After installing NVM, close and reopen your terminal, or run the following command to ensure NVM is sourced and available for use:

source ~/.bashrc

Step 2: Install Node.js and Yarn via APT Command on Ubuntu

With NVM installed, you can now install the desired version of Node.js. NVM allows you to install multiple versions and switch between them as needed.

To install the latest LTS (Long Term Support) version of Node.js, run:

nvm install --lts

Alternatively, to install the latest current version, run:

nvm install stable

Once you have installed the desired Node.js version, you can install Yarn globally for the selected version. This ensures that the Yarn installation is associated with the chosen Node.js environment.

To install Yarn globally, execute the following command:

npm install -g yarn

After the installation is complete, you can verify the successful installation by checking the Yarn version:

yarn --version

The output should display the installed version number, confirming that Yarn is now installed on your Ubuntu system using NVM.

Getting Started with Yarn on Ubuntu 22.04 or 20.04

In this section, we will guide you through the initial setup process with Yarn on your Ubuntu Linux system. By following these steps, you can start using Yarn to manage your projects effectively and efficiently.

Step 1: Configure Yarn on Ubuntu

First, let’s configure Yarn to ensure a smooth user experience. Yarn allows you to customize its behavior by modifying the settings in the .yarnrc file. If the file doesn’t exist, you can create one in your home directory.

To create a .yarnrc file, open your terminal and run the following command:

touch ~/.yarnrc

Now, open the .yarnrc file using your preferred text editor and add any configuration settings that you may require. For example, you can specify a custom cache directory or set the default package manager to Yarn:

cache-folder "/path/to/custom/cache/directory"
default-package-manager "yarn"

Save and close the file when you’re done.

Step 2: Initialize a New Yarn Project

To create a new Yarn project, navigate to your desired project directory using the terminal and run the following command:

yarn init

This command will prompt you to answer several questions about your project, such as the project name, version, description, entry point, and more. Answer these questions as per your project requirements, or press Enter to accept the default values.

Upon completion, Yarn will generate a package.json file containing the project’s metadata and dependencies.

Step 3: Add and Manage Dependencies

Yarn makes it easy to add and manage dependencies for your project. To add a new dependency, use the yarn add command followed by the package name:

yarn add package-name

This command will install the specified package and update the package.json file accordingly.

To remove a dependency, use the yarn remove command followed by the package name:

yarn remove package-name

This command will uninstall the specified package and update the package.json file.

Step 4: Run Scripts and Manage Tasks

Yarn allows you to define and run scripts to automate tasks in your project. To add a script, open the package.json file and include the script definition in the “scripts” section:

"scripts": {
  "start": "node index.js"
}

Save and close the file when you’re done.

To run a script, use the yarn run command followed by the script name:

yarn run start

This command will execute the specified script, making it easier to manage and automate tasks in your project.

Basic Yarn Commands on Ubuntu 22.04 or 20.04

In this section, we’ll cover some of the most common and useful Yarn commands developers frequently use while working with Yarn. Familiarizing yourself with these commands will help you manage your projects more effectively and streamline your workflow.

Initializing a New Project

To start a new project using Yarn, navigate to the desired directory, and run the following command:

yarn init

This command will create a package.json file in your project directory, which will store information about your project, such as dependencies, scripts, and metadata.

Adding Dependencies

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

yarn add <package-name>

This command will download the specified package and its dependencies and add them to your package.json file and yarn.lock file.

Removing Dependencies

To remove an existing dependency from your project, use the yarn remove command followed by the package name:

yarn remove <package-name>

This command will remove the specified package from your package.json file and yarn.lock file and delete it from the node_modules directory.

Updating Dependencies

To update your project’s dependencies, run the following command:

yarn upgrade

This command will update all dependencies listed in your package.json file to their latest available versions. If you want to update a specific dependency, use the yarn upgrade command followed by the package name:

yarn upgrade <package-name>

Running Scripts

To run a script defined in your package.json file, use the yarn run command followed by the script name:

yarn run <script-name>

For example, if your package.json file has a start script, you can run it with:

yarn run start

Adding a Development Dependency

Sometimes, you may need to add a dependency that is only required during the development process and not in the production environment. In such cases, you can use the --dev flag when adding a package. For example, to add the eslint package as a development dependency, run:

yarn add eslint --dev

This command will add the eslint package as a development dependency in your package.json file.

Installing All Dependencies

If you have just cloned a project from a Git repository or need to install all the dependencies listed in the package.json file, you can use the yarn install command:

yarn install

This command will read the package.json file and install all the required dependencies into the node_modules directory.

Checking for Outdated Dependencies

To identify outdated dependencies in your project, use the yarn outdated command:

yarn outdated

This command will compare the currently installed package versions with the latest available versions, and display a list of outdated packages along with their current and latest versions.

Listing Installed Dependencies

To view a list of all installed dependencies and their versions, run the yarn list command:

yarn list

This command will display a tree-like structure of your project’s dependencies, which can be helpful when troubleshooting dependency issues or understanding the relationships between packages.

Closing Thoughts on Installing Yarn on Ubuntu Linux

In conclusion, installing Yarn on Ubuntu Linux is a straightforward process that provides you with a powerful and efficient package manager for your Node.js projects. We have covered multiple installation methods, including the use of APT, NVM, and manual installation. Additionally, we have discussed the first-time setup, common Yarn commands, and provided more examples to help you better understand the use of Yarn in various scenarios. By following these steps, you can easily set up Yarn on your Ubuntu system and take advantage of its features to manage your Node.js projects.