How to Install Yarn on Ubuntu 22.04 | 20.04

As we dive into modern web development, one tool stands out as an essential ally for JavaScript developers: the Yarn package manager. Yarn, an open-source project, was born in 2016 as a collaboration between Facebook, Google, Exponent, and Tilde. It was designed to focus on speed, reliability, and security, making it a popular choice for managing dependencies in JavaScript projects.

Yarn aims to optimize dependency management, ensuring that packages are installed consistently across different systems while maintaining the project’s integrity. With its wide adoption and ongoing support from the developer community, Yarn has emerged as a reliable and efficient solution for package management in the ever-evolving world of web development.

Yarn: Key Features and Benefits

Yarn boasts several compelling features that make it a go-to choice for developers working with JavaScript projects:

  • Speed: Yarn’s impressive performance speeds up installation times, reducing the wait for developers and enhancing productivity.
  • Security: By incorporating checksums to verify the integrity of every installed package, Yarn ensures that your project remains secure and stable.
  • Offline Mode: Yarn’s ability to cache every package allows developers to work seamlessly, even without an internet connection.

With a solid understanding of Yarn’s role in package management and its benefits to developers, it’s time to get it up and running on your system. The following guide will demonstrate how to install Yarn on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa using one of two methods: NodeSource repository or NVM for the latest version.

Section 1: Installing Yarn from NodeSource Repository on Ubuntu

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 Repository

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:

For the Current Release:

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

For the 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

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.

Section 2: Installing Yarn with Node Version Manager (NVM) on Ubuntu

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

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

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.

Section 3: Getting Started with Yarn on Ubuntu Linux

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

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.

Section 4: Essential Yarn Commands for Everyday Use

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.

Step 1: 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.

Step 2: 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.

Step 3: 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.

Step 4: 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>

Step 5: 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

Step 6: 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.

Step 7: 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.

Step 8: 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.

Step 9: 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.

Additional Resources and Links

As you continue to explore and work with Yarn, you may find the following official resources and documentation helpful:

  1. Yarn Official Website: This is the official homepage for Yarn, where you can find the latest news, updates, and releases for the package manager.
  2. Yarn Documentation: The official Yarn documentation is an invaluable resource that covers all aspects of using Yarn, from installation to advanced features. It provides comprehensive guides, tips, and best practices for managing your Node.js projects.
  3. Yarn GitHub Repository: The official Yarn GitHub repository is where you can find the source code, report issues, and contribute to the project’s development. It’s an excellent resource for staying up-to-date with the latest development and understanding the inner workings of Yarn.

Share to...