Installing and Using Yarn on Ubuntu

Installing and Using Yarn on Ubuntu

Yarn is a powerful JavaScript package manager that is compatible with npm and helps automate the process of installing, updating, configuring, and removing npm packages. Yarn provides speed and reliability by caching downloaded packages and parallelizing operations. In this tutorial, we will cover how to install both the latest version and classic version of Yarn on Ubuntu, along with an overview of basic Yarn commands and options.

Installing the Latest Yarn Version

To install and manage the latest Yarn version, we recommend using Corepack, a binary included in newer Node.js releases that serves as a point of contact between the user and Yarn. Here are the steps to install Yarn using Corepack:

  1. Ensure your Node.js version is up-to-date. Check the version with the command: node -v Corepack requires Node.js 16.10 or later. If the output shows an older version, update Node.js.

  2. Start Corepack by typing: corepack enable Note: If Corepack does not exist on your system, install it by typing: sudo npm install -g corepack

  3. Install the latest version of Yarn with the command below: corepack prepare yarn@stable --activate

  4. Type the following command to test the installation and check the Yarn version: yarn --version To update the binary to the latest version, run: yarn set version stable

Installing Classic Yarn Version

Although the classic versions of Yarn before 2.0 are in maintenance mode, you can still install Yarn 1.x using the official Yarn repositories and npm. Here's how:

Option 1: Install Yarn Classic Via Repository

  1. Add the GPG key: curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/yarn.gpg The GPG key ensures that you are installing authentic software.

  2. Add the Yarn repository: echo "deb [signed-by=/etc/apt/trusted.gpg.d/yarn.gpg] https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

  3. Update your local repository listings: sudo apt update

  4. Install Yarn: sudo apt install yarn This command installs Yarn and, if you don’t already have Node.js installed, your package manager will install it for you.

Option 2: Install Yarn Classic Using NPM

  1. Check if npm is installed: npm --version If you don’t have npm, install it by running sudo apt install npm.

  2. To install Yarn with npm, enter: sudo npm install -g yarn

Upgrading Yarn from Classic to Latest

To upgrade from classic Yarn to the latest version, follow these steps:

  1. Run the npm install command to ensure that the classic Yarn is updated to the latest 1.x version: sudo npm install -g yarn

  2. Switch to the modern Yarn version by typing: yarn set version berry

Basic Yarn Usage

Here are some basic Yarn commands you should know:

Creating a New Project

  1. Create a directory for your application and navigate into it: mkdir ~/my_project && cd ~/my_project

  2. To create a new project, run yarn init.

Adding a Dependency

  1. Add an npm package to the project dependencies: yarn add [package_name] By default, Yarn installs the latest version. To install a specific version or tag, use the following syntax: yarn add [package_name]@[version_or_tag]

Upgrading a Dependency

  1. To upgrade a package, use one of the following commands: yarn upgrade, yarn upgrade [package_name], or yarn upgrade [package_name]@[version_or_tag] If no package name is given, the command will update all project dependencies to their latest version according to the version range specified in the package.json file. Otherwise, only the specified packages are updated.

Removing a Dependency

  1. Use the yarn remove command followed by the package name to remove a dependency: yarn remove [package_name] The command will remove the package and update the package.json and yarn.lock files.

Installing All Project Dependencies

  1. To install all project dependencies specified in the package.json file, run: yarn or yarn install
Conclusion

You now have a comprehensive understanding of how to install and manage Yarn on your Ubuntu system. Whether you are using the latest version of Yarn or the classic version, you can benefit from Yarn's speed, reliability, and versatility. For more information about Yarn, visit the official Yarn documentation page.

George Whittaker is the editor of Linux Journal, and also a regular contributor. George has been writing about technology for two decades, and has been a Linux user for over 15 years. In his free time he enjoys programming, reading, and gaming.

Load Disqus comments