Yarn is a package manager for Node.js that allows installation of packages from the npm registry. It can be used as an alternative to npm and supports the same package.json format. This tutorial explains how to install Yarn on Ubuntu 26.04.
Prepare environment
Ensure that Node.js and Corepack are installed. Refer to the post for instructions on how to install them.
Install Yarn
Enable Yarn support through Corepack by running:
sudo corepack enable yarn
This step creates a small executable wrapper command (shim) that points Yarn calls to the correct Corepack-managed version of Yarn.
Corepack is the preferred method for managing Yarn because it allows each project to specify its own Yarn version through the packageManager field in the package.json file. When this field is not set, the system falls back to the globally installed Yarn version. If a project declares the packageManager field, Corepack automatically fetches and activates the specified Yarn version for that project, and that version is used instead of the global one.
Install Yarn globally, which acts as a fallback when the packageManager field is not defined in the package.json:
corepack install -g yarn
Verify the installation by checking the Yarn version:
yarn --version
Testing Yarn
Create a new project directory and switch into it:
mkdir myproject && cd myproject
Initialize a basic package definition:
echo '{"name":"myproject","version":"1.0.0"}' > package.json
Add a sample dependency to confirm Yarn functionality:
yarn add express
Uninstall Yarn
Disable Yarn integration in Corepack:
sudo corepack disable yarn
Remove Corepack Yarn cache, which stores multiple Yarn versions downloaded and managed by Corepack:
rm -rf ~/.cache/node/corepack/v1/yarn
Remove Yarn configuration directory:
rm -rf ~/.yarn
Leave a Comment
Cancel reply