Skip to main content

How to install Yarn in Ubuntu 22/24

4 minutes


Yarn is a package manager for Node.js JavaScript runtime environment and developed in 2016 by Sebastian McKenzie of Meta. It is more efficient and reliable alternative to npm (Node Package Manager), which is the default package manager for Node.js.

Yarn offers several advantages over npm and are -

  • Faster and more consistent package installations 
  • Improved security features
  • Improved deterministic dependency resolution. 
  • Uses a lockfile (yarn.lock) to ensure that the same versions of dependencies are installed across different environments.

This post will walk you through the Yarn installation process, from adding the repository to running the final npm install yarn command along with usages of Yarn.

Prerequisites

To install Yarn on Ubuntu, ensure you meet the following requirements.

  • A running local or cloud instance of Ubuntu 22.04/24.04
  • SSH access to the server with sudo privilege. 
  • npm and Node.js is already installed on the system.

 

 

 

Method #1: Install the Latest Yarn Version

It is highly recommended to utilize Corepack for the installation of the latest version of Yarn. This script acts as a liaison between the user and Yarn, and is integrated into the most recent releases of Node.js. 

Now follow the steps given in the next section to use Corepack to install Yarn latest version.

Check the node version

$ node -v
v20.11.1

It is imperative that Node.js version 20.11 or higher is required in order to utilize Corepack. Make sure to upgrade your current Node.js version if it is outdated.

The Corepack is not available in some Linux distributions and if you find it missing in your system, You can install Corepack by utilizing following command.

$ sudo npm install -g corepack
npm install corepack
 

Launch Corepack

$ sudo corepack enable

Finally, install the latest version of yarn with the following command.

$ corepack prepare yarn@stable --activate
Activate corepack
 

Verify the installation process of yarn by printing the Yarn version in the terminal.

$ yarn --version
4.3.1

You may choose to upgrade the binary to the latest stable version by executing the following command.

$ yarn set version stable
➤ YN0000: Done in 0s 6ms

Method #2: Install the Classic Yarn Version

Previous versions of Yarn, prior to 2.0, are no longer supported and are now in maintenance mode. In certain circumstances, it may be necessary to utilize a previous iteration of Yarn (typically for the purpose of evaluating backward compatibility) within your project.

One may utilize the repository maintained by the Yarn developers in order to install previous versions, but please be advised that cURL must be present on your system to access the repository.

Add Yarn repository

To start with, you need to add Yarn repository key in your system. Use the following command to add the repository key.

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

 Now add the Yarn repository to your package sources using the following command and subsequently update the package index:

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

Install Yarn

Finally, Run the following command to install Yarn:

$ sudo apt install yarn

At this point, Yarn should be available on your Ubuntu system. Verify the installation by checking the Yarn version:

$ yarn --version

The above command should display the version of Yarn installed on your system.

Usages of YARN

Once you have successfully installed YARN, use it to create a test project, add dependencies and run a few common Yarn commands. 

Init a yarn project

Create a new YARN project using yarn init by specifying name of the project.

$ mkdir -p yarn_project && cd yarn_project
$ yarn init 

The above command will create few configuration files and initialize an empty git repository in your project folder. List the project folder to view all these configuration files.

$ ls -al yarn_project

List packages

To list all the installed packages in the Yarn project, use the list option with the yarn command.

$ yarn list

Add dependencies

To add dependencies in a yarn project, use the yarn add command followed by the package name. 

$ yarn add @package_name
$ yarn add @package_name@version  // with specific version

Remove dependencies

To remove a dependency from the yarn project, use the remove option along with Yarn command. 

$ yarn remove package_name
$ yarn remove package_name@version

Install all dependencies

Install all dependencies specified in package.json file by using either yarn or yarn install. This is convenient  in situation when you have manually added the dependencies in the file package.json and subsequently want to install them.

$ yarn install
      OR
$ yarn

Upgrade package

To upgrade a package to the latest version, use the upgrade option along with yarn command.

$ yarn upgrade package_name
$ yarn upgrade // Upgrades all packages listed in the package.json file to the latest

Audit

The audit command checks the known vulnerabilities in the installed packages.

$ yarn audit

Clean cache

To clean the Yarn's global cache of packages, use the clean cache option.

$ yarn cache clean

 

Conclusion

You've completed the steps to install Yarn on Ubuntu with npm and apt. The setup equips you with a robust package manager that enhances your JavaScript and Node.js projects.  The Yarn community and documentation serve as valuable assets for those seeking guidance on a seamless and efficient development process.

fivestar_rating
No votes yet
Comments