Yarn, a powerful JavaScript package manager, compatible with npm, automates the installation, update, configuration and removal of npm packages. Yarn improves speed and reliability by caching downloaded packages and parallel operations. This tutorial will explain how to install the latest and classic version of Yarn on Ubuntu and will outline the basic Yarn commands and options.
Install the latest version of Yarn
To install and manage the latest version of Yarn, it is recommended to use Corepack, a binary file included in the newer Node.js versions, acting as a bridge between the user and Yarn. Here are the steps to install Yarn using Corepack:
node -v
to check the version. Corepack requires Node.js 16.10 or higher. If the output shows an older version, update Node.js.corepack enable
to start Corepack. (Note: If Corepack does not exist on your system, type sudo npm install -g corepack
to install it.)corepack prepare yarn@stable --activate
yarn --version
To update the binary file to the latest version, run: yarn set version stable
Install the classic version of Yarn
Although the classic version of Yarn before 2.0 is in maintenance mode, you can still install Yarn 1.x using the official Yarn repository and npm. The method is as follows:
Method 1: Install the classic version of Yarn through the warehouse
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/yarn.gpg
The GPG key ensures that you are installing genuine software.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
sudo apt update
sudo apt install yarn
This command will install Yarn, and if your system has not yet installed Node.js, your package manager will install it for you.Method 2: Use NPM to install the classic version of Yarn
npm --version
If you don't have npm, run sudo apt install npm
to install it.sudo npm install -g yarn
Upgrade Yarn from Classic to Latest
To upgrade Yarn from the Classic to the latest version, follow these steps:
sudo npm install -g yarn
yarn set version berry
Basic Yarn usage
Here are some basic Yarn commands you should know:
Create a new project
mkdir ~/my_project && cd ~/my_project
yarn init
.Add dependencies
yarn add [package_name]
By default, Yarn installs the latest version. To install a specific version or tag, use the following syntax: yarn add [package_name]@[version_or_tag]
Upgrade dependencies
yarn upgrade
, yarn upgrade [package_name]
or yarn upgrade [package_name]@[version_or_tag]
If no package name is given, the command updates all project dependencies to the latest version based on the version range specified in the package.json file. Otherwise, only the specified package is updated.Remove dependencies
yarn remove
command followed by the package name to remove dependencies: yarn remove [package_name]
This command will remove the package and update the package.json and yarn.lock files.Install all project dependencies
yarn
or yarn install
in conclusion
You have now fully understood how to install and manage Yarn on Ubuntu systems. Whether you are using the latest version of Yarn or the classic version of Yarn, you can benefit from the speed, reliability and versatility of Yarn. For more information about Yarn, please visit the official Yarn documentation page.
The above is the detailed content of Installing and Using Yarn on Ubuntu. For more information, please follow other related articles on the PHP Chinese website!