Home > System Tutorial > LINUX > Installing and Using Yarn on Ubuntu

Installing and Using Yarn on Ubuntu

Jennifer Aniston
Release: 2025-03-17 10:09:15
Original
820 people have browsed it

Installing and Using Yarn on Ubuntu

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:

  1. Make sure your Node.js version is up to date. Use the command node -v to check the version. Corepack requires Node.js 16.10 or higher. If the output shows an older version, update Node.js.
  2. Enter corepack enable to start Corepack. (Note: If Corepack does not exist on your system, type sudo npm install -g corepack to install it.)
  3. Use the following command to install the latest version of Yarn: corepack prepare yarn@stable --activate
  4. Enter the following command to test the installation and check the Yarn version: 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

  1. Add GPG key: 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.
  2. Add a Yarn repository: 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
  3. Update the local repository list: sudo apt update
  4. Install Yarn: 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

  1. Check if npm is installed: npm --version If you don't have npm, run sudo apt install npm to install it.
  2. To install Yarn using npm, enter: sudo npm install -g yarn

Upgrade Yarn from Classic to Latest

To upgrade Yarn from the Classic to the latest version, follow these steps:

  1. Run the npm install command to ensure that the classic version of Yarn is updated to the latest 1.x version: sudo npm install -g yarn
  2. Switch to the modern version of Yarn by typing: yarn set version berry

Basic Yarn usage

Here are some basic Yarn commands you should know:

Create a new project

  1. Create a directory for your application and go to it: mkdir ~/my_project && cd ~/my_project
  2. To create a new project, run yarn init .

Add dependencies

  1. Add npm package to project 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

  1. To upgrade a package, use one of the following commands: 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

  1. Use the 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

  1. To install all project dependencies specified in the package.json file, run: 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template