Yarn is a package manager for JavaScript environments. It is an alternative to the NPM package manager and was developed by Facebook in collaboration with Google, Tilde, and Expo Dev. Yarn speed up the installation process to install packages from the local cache. It is designed to solve security and performance-related issues with large codebases.

In this tutorial, I will show you how to install the Yarn package manager on Ubuntu 22.04.

Prerequisites

  • A server running Ubuntu 22.04.
  • A root password is configured on the server.

Install Node.js

Before installing Yarn, the Node.js package must be installed on your server. First, install the curl tool with the following command:

apt install curl -y

Next, add the Node.js repository using the following command:

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

Next, install the Node.js package using the following command:

apt install nodejs -y

Enable Corepack

Corepack is a package manager that allows you to install Yarn on your server. By default, the Corepack is installed with Node.js packages. But it is disabled by default. So you will need to enable it to use.

You can enable the Corepack with the following command:

corepack enable

Install Yarn on Ubuntu 22.04

Using Corepack, you can easily install and activate the Yarn package on your server. Corepack allows you to manage different Yarn package versions across multiple projects.

Run the following command to install and activate the latest Yarn version:

corepack prepare [email protected] --activate

You can now check the Yarn version using the following command:

yarn --version

You will get the following output:

3.2.3

You can also use the Corepack to install the older version of Yarn.

For example, run the following command to install Yarn 3.1.1:

corepack prepare [email protected] --activate

Next, set the default version to Yarn 3.1.1, and run the following command:

yarn set version 3.1.1

You will get the following output:

? YN0000: Retrieving https://repo.yarnpkg.com/3.1.1/packages/yarnpkg-cli/bin/yarn.js
? YN0000: Saving the new release in .yarn/releases/yarn-3.1.1.cjs
? YN0000: Done in 0s 790ms

If you want to set the default Yarn version to the stable version, run the following command:

yarn set version stable

How to Use Yarn

Using Yarn, you can install and remove any packages easily.

First, initialize the Yarn project with the following command:

yarn init

You will get the following output:

{
  name: 'root',
  packageManager: '[email protected]'
}

Next, install the React package using the following command:

yarn add react

You will get the following output:

? YN0000: ? Resolution step
? YN0000: ? Completed in 0s 332ms
? YN0000: ? Fetch step
? YN0013: ? [email protected]:4.0.0 can't be found in the cache and will be fetched from the remote registry
? YN0013: ? [email protected]:1.4.0 can't be found in the cache and will be fetched from the remote registry
? YN0013: ? [email protected]:18.2.0 can't be found in the cache and will be fetched from the remote registry
? YN0000: ? Completed in 0s 418ms
? YN0000: ? Link step
? YN0000: ? Completed
? YN0000: Done in 0s 808ms

To install the specific version of the package, run the following command:

yarn add [email protected]

To upgrade the dependency, run the following command:

yarn up package

To remove any package, run the following command:

yarn remove package

How to Upgrade Yarn

If you want to upgrade the Yarn to the latest version, run the following command:

yarn set version stable

yarn install

Remove Yarn Package

If you don’t want to use the Yarn, then you can uninstall it easily using the following command:

corepack disable yarn

Conclusion

In this post, we explained how to install Yarn on Ubuntu 22.04. We also explained to install and manage multiple Yarn versions. I hope this guide will help you to manage your JavaScript project.

<img alt="Hitesh Jethva" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/10/echo/hitesh-80.jpg6351445c87203.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="76" src="data:image/svg xml,” width=”80″>

About Hitesh Jethva

Over 8 years of experience as a Linux system administrator. My skills include a depth knowledge of Redhat/Centos, Ubuntu Nginx and Apache, Mysql, Subversion, Linux, Ubuntu, web hosting, web server, Squid proxy, NFS, FTP, DNS, Samba, LDAP, OpenVPN, Haproxy, Amazon web services, WHMCS, OpenStack Cloud, Postfix Mail Server, Security etc.