How to Install Yarn on Ubuntu 24.04


Yarn is one of the open-source package managers for JavaScript. It is increasingly getting popular due to its rapidity and other security features. Yarn is also compatible with the NPM, so it helps in installing, setting up, and uninstalling the NPM packages. By caching each downloaded package, Yarn improves and enhances the installation process by parallelizing the operations.

This article covers Yarn installation on the latest Ubuntu 24.04.

Table of Contents:

1. How to Install Yarn on Ubuntu 24.04

2. How to Install the Classic Version of Yarn on Ubuntu 24.04

3. How to Upgrade Classic Yarn Version to Latest

4. Performing Basic Yarn Operations on Ubuntu 24.04

5. How to Uninstall Yarn from Ubuntu 24.04

Conclusion

1. How to Install Yarn on Ubuntu 24.04

To install Yarn on Ubuntu, you can use Corepack. This will give you the latest version of Yarn, but if you want to install Yarn’s classic versions, you can install them using the repositories and NPM.

Now we will cover all three yarn installation methods for Ubuntu 24.04:

  • Install Yarn Using Corepack
  • Install Classic Yarn Version Using Repository
  • Install Classic Yarn Version Using NPM

1.1. Install the Latest Yarn Version Using Corepack

For Yarn installation on Ubuntu, the recommended way is to use the Corepack. It is a tool that makes installing and keeping Yarn up-to-date easier. Corepack acts like a bridge between you and Yarn.

Before you start the Yarn installation using Corepack, first check your Node.js version and make sure that it is up-to-date:

node -v

Corepack only works with Node.js versions 16.10 and up. If your Node.js is older, you’ll need to update it first.

Note: If Node.js is missing, run the sudo apt install nodejs command.

Now enable the Corepack using this command:

corepack enable

When you run Corepack enable, it activates Corepack’s features, allowing it to intercept package manager commands transparently.

If Corepack is not already installed on your Ubuntu 24.04, you can get it using this command:

sudo npm install -g corepack

Note: If NPM is unavailable on your system, run the sudo apt install npm command to get it.

Finally, install Yarn using this command:

corepack prepare yarn@stable --activate

The above command will prepare Corepack and set the Yarn to the stable version. Corepack will ensure that the Yarn is installed and ready to use.

To confirm Yarn installation, check its version:

yarn --version

Once, the latest Yarn is installed now run the below command to also update the binary:

yarn set version stable

2. How to Install the Classic Version of Yarn on Ubuntu 24.04

Older Yarn versions (below 2.0) are no longer being actively developed and will soon reach end-of-life. You can still install Yarn 1.x using official Yarn package repositories or NPM, but be aware of its limited support.

2.1. Install Classic Yarn Version via Repository

To install the classic Yarn version, you can use the official Yarn repository. The developer maintains this repository and gives you the latest version of the classic Yarn. It also simplifies the process for classic Yarn installation.

First start by importing the GPG key for the Yarn repository.

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/yarn.gpg

This GPG key will make sure the package you installed is from trusted sources.

Now add the Yarn repository to the Ubuntu 24.04 system:

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

This command sets up Yarn as a source for your package manager, allowing it to install and manage Yarn itself.

Now update your local system repository:

sudo apt update

Finally, run the apt install command to get Yarn from its official repository on your Ubuntu 24.04 system:

sudo apt install yarn

To confirm Yarn installation, run the version command:

yarn ––version

2.2. Install Classic Yarn Version Using NPM

NPM is a package manager that helps you to develop and share the JavaScript code. You can also install Yarn using the NPM package manager.

First, make sure the NPM is available on your system and make sure it is the latest version:

npm --version

Note: If NPM is unavailable on your system, run the sudo apt install npm command to get it.

Now to install Yarn using NPM, run this command:

sudo npm install -g yarn

3. How to Upgrade Classic Yarn Version to Latest

To upgrade the classic Yarn to the latest version, run the npm install command:

sudo npm install -g yarn

Now switch to the latest stable version of Yarn (berry). In your terminal, type:

yarn set version berry

You’ve successfully updated Classic Yarn to its latest version.

Note: You have to enable the Corepack to upgrade the Classic Yarn to the latest version.

4. Performing Basic Yarn Operations on Ubuntu 24.04

Now that the Yarn installation is completed, let’s check its basic operations.

Creating New Yarn Project

To initialize a new Yarn project, use the following command:

yarn init

When you start a new project with Yarn, it sets things up by creating two key files:

  • package.json: This file stores the essential information of a project like its name, dependencies, and scripts.
  • yarn.lock: This file keeps a record of the version of your project’s dependencies to avoid any conflicts.

Adding Dependency to Project

In Yarn, packages may depend on other packages for work, called dependencies. To add dependencies, you can follow the below-given commands.

To add the latest version of a package:

yarn add [package-name]

To add a specific version of a package:

yarn add [package-name]@version

To add a package with a specific tag (e.g., latest or next):

yarn add [package-name]@tag

This initialization process isn’t just about creating files. It also keeps your project’s configuration (in package.json) up-to-date and generates a yarn.lock file. This yarn.lock file ensures everyone working on the project uses the same software versions.

Upgrade a Yarn Dependency

If you have already installed a Yarn dependency, you can also upgrade it using the following command.

The syntax for adding and upgrading is almost similar. For example:

To upgrade a package, you can follow the below command:

yarn upgrade [package-name]

Similarly, to upgrade a specific conversion of the package, run this command:

yarn upgrade [package-name]@version

To upgrade a package to a specific tag, run:

yarn upgrade [package-name]@tag

Removing a Yarn Dependency

To remove a Yarn package dependency, run this command:

yarn remove [package-name]

Installing Additional Yarn Dependencies

If you have a package.json file with listed dependencies, you can install them all at once using:

yarn install

Note: Yarn allows different developers to add dependencies. To ensure everyone has the same project setup, run yarn install after changes to the package.json file. This keeps everyone’s development environment in sync.

5. How to Uninstall Yarn from Ubuntu 24.04

The way to uninstall Yarn from Ubuntu depends on the installation method you have used. There are two main possibilities:

Installed with apt package manager:

If you used apt or apt-get to install Yarn, you can uninstall it using the same package manager. Open a terminal and run:

sudo apt remove yarn

You might also want to remove any leftover configuration files with:

sudo apt autoremove

Installed with npm:

If you used npm to install Yarn globally, you can uninstall it with:

sudo npm uninstall -g yarn

If you have used Corepack for Yarn installation and used the Corepack enable command, then to remove the Yarn from the system, run:

corepack disable yarn

Conclusion

Yarn is one of the growing open-source package management tools for JavaScript packages. It is also compatible with the NPM, which increases its use case one step further. Yarn is available in two different versions, one is the latest version and the other one is the Classic. To get the latest Yarn version on your Ubuntu 24.04 you can use the Corepack repository. If you need a classic Yarn version for some older applications, you can get it either through the NPM or using the Yarn repository. Follow the article for details of all three Yarn installation methods.

Print Friendly, PDF & Email
Categories