×

How to Install Yarn on Ubuntu 24.04 – LinuxWays

How to Install Yarn on Ubuntu 24.04 – LinuxWays


For building any web server or any other application that will require internet access to function mostly javascript is used as a scripting language along with Node.js runtime environment. Further to add any functionalities to such applications or web servers a package manager is required. There are two package managers named node package manager (npm) and yarn (yet another resource navigator) used for this purpose.

Yarn is usually preferred by most of users for several reasons, it is supported on different operating systems including the newly released Ubuntu 24.04. This guide is about the installation of the yarn javascript package manager on Ubuntu 24.04.

Outline:

How to Install Yarn on Ubuntu 24.04

Yarn is an open-source package manager specifically used for managing javascript project dependencies. Just like the Ubuntu package manager, it serves the purpose of streamlining the process of installing, updating, configuring, and removing package dependencies.

Yarn offers various features which include workspaces, offline caching, and parallel installs which result in improvements in speed, reliability, and security. These features make it preferable when compared with the node package manager, to Install yarn on Ubuntu 24.04 there are four ways:

1: Through Yarn Repository

Installing an application on Ubuntu 24.04 via the application repository is beneficial in two cases one if the application package is not present in apt or the version of the package in apt is old. For yarn, the second case is true so to install a relatively newer version use its official repository. Yarn comes with three types of versions which are:

  • Stable: This version comes with the least bugs and issues, it comes with reliable dependency management, checksum for installed packages, and fast installation due to cached packages.
  • Release Candidate: This is a pre-release version that will precede the stable version release, it is used for testing new features, and errors or bugs can be expected.
  • Nightly: This version is built from the latest yarn source code or in other words its the developmental version.

To install any of the three using the add repository method you have to add the yarn repository for the respective yarn version. Here. for illustration, I have added the repository for the developmental version which is the nightly version. First, I have added the GPG key for yarn and the command for all of the versions will be the same:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add

Now fetch the yarn repository and save it in sources.list directory:

echo “deb https://nightly.yarnpkg.com/debian/ nightly main” | sudo tee /etc/apt/sources.list.d/yarn.list

To apply the changes or to successfully add the yarn repository in Ubuntu 24.04 update the packages list and then use apt to install yarn:

Once the installation is complete you can verify it by checking its version. Further to install the stable version of yarn add the following repository:

echo “deb https://dl.yarnpkg.com/debian/ stable main” | sudo tee /etc/apt/sources.list.d/yarn.list

Similarly, install the pre-release version of Yarn on Ubuntu 24.04 by executing:

echo “deb https://dl.yarnpkg.com/debian/ rc main” | sudo tee /etc/apt/sources.list.d/yarn.list

Note: Yarn on Ubuntu 24.04 can be installed via the default package installer without adding the repository. However, the version in apt is quite old and you might get an error or encounter issues using it due to cmdtest utility.

2: Through Node Package Manager

Since Yarn is the package manager for javascript projects that use Node.js as their runtime environment it can be installed on Ubuntu 24.04 by using the node package manager. However, first, the npm should be installed using Ubuntu’s default package installer:

Now install Yarn via npm, here g flag is used to install a global version of Yarn which provides the privilege of accessing it from any directory of the system:

3: Through Corepack

Correpack is a Node.js script that serves as a bridge between the Node.js projects and the package managers used during the server or app development. In other words, Corepack allows to use yarn package manager without installing it separately.

If the Node.js or npm is already installed on Ubuntu 24.04 then Corepack may also be pre-installed you can check by using the version command. To install Corepack use the node package manager:

sudo npm install -g corepack

After the installation enable this script:

Now prepare and activate the stable version of the Yarn package manager:

corepack prepare yarn@stable –activate

Now execute the yarn command and it asks to add the tar file for version 1.22.19 so prompt it:

Now as you can see an older version is installed so if you want to update it to the latest version then use the up command along with Corepack:

4: Through Installation Script

If you are looking for an easy way to install Yarn on Ubuntu 24.04 then try this method as this method bash script file for yarn is downloaded from its official website. The downloaded bash file is then executed using the bash command. So in other words the file Yarn package manager is installed directly from the internet:

curl -o- -L https://yarnpkg.com/install.sh | bash

On the completion of the command execution, there will be a completion message showing the version installed:

To make Yarn work properly you need to reboot the system. In the above image, I have installed the stable version of Yarn so to install the pre-release version execute:

curl -o- -L https://yarnpkg.com/install.sh | bash -s –rc

On the other hand to install the development version execute:

curl -o- -L https://yarnpkg.com/install.sh | bash -s –nightly

How to Change the yarn Version on Ubuntu 24.04

Based on the usage and compatibility you might need to upgrade or downgrade the version of Yarn and for that you can use the set version command along with the version name:

sudo yarn set version stable

Or you can use the version number that you want to use:

sudo yarn set version <version-number>

 

How to Remove Yarn from Ubuntu 24.04

The process of removing Yarn on Ubuntu 24.04 varies since there are several different ways to install it. To remove Yarn from Ubuntu 24.04 if installed via Yarn repository then execute:

sudo apt remove –autoremove yarn

Next, remove the repository from the sources.list directory:

sudo rm /etc/apt/sources.list.d/yarn.list

Also, remove the GPG key for yarn and for that first find its key number by listing all the keys:

Now use the last four key combinations along with the del command ti remove the Yarn GPG key from Ubuntu 24.04:

sudo apt-key del B01B 86E5 0310

To uninstall Yarn if installed via the node version manager execute:

sudo npm uninstall -g yarn

To remove the Yarn package manager through Corepack first disable it and then remove it using the node package manager:

sudo corepack disable

sudo npm uninstall -g corepack

In case Yarn is installed through a bash file then first locate the directory in which it is installed and for that execute:

Now remove the directory which will result in Yarn uninstallation:

Conclusion

Yarn is a javascript project package manager used to add functionalities or features in the application or server under development. To install Yarn in Ubuntu 24.04, use the Yarn repository, node package manager, Corepack, or installation script from the official source. You can also select three versions for Yarn: stable, pre-released, and development version. Further, you can also switch between the Yarn version by using the set version command.



Source link