Installing and Using Yarn on Ubuntu
Yarn is a powerful JavaScript package manager that is compatible with npm and helps automate the process of installing, updating, configuring, and removing npm packages. Yarn provides speed and reliability by caching downloaded packages and parallelizing operations. In this tutorial, we will cover how to install both the latest version and classic version of Yarn on Ubuntu, along with an overview of basic Yarn commands and options.
Installing the Latest Yarn VersionTo install and manage the latest Yarn version, we recommend using Corepack, a binary included in newer Node.js releases that serves as a point of contact between the user and Yarn. Here are the steps to install Yarn using Corepack:
-
Ensure your Node.js version is up-to-date. Check the version with the command:
node -v
Corepack requires Node.js 16.10 or later. If the output shows an older version, update Node.js. -
Start Corepack by typing:
corepack enable
Note: If Corepack does not exist on your system, install it by typing:sudo npm install -g corepack
-
Install the latest version of Yarn with the command below:
corepack prepare yarn@stable --activate
-
Type the following command to test the installation and check the Yarn version:
yarn --version
To update the binary to the latest version, run:yarn set version stable
Although the classic versions of Yarn before 2.0 are in maintenance mode, you can still install Yarn 1.x using the official Yarn repositories and npm. Here's how:
Option 1: Install Yarn Classic Via Repository
-
Add the 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 authentic software. -
Add the 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
-
Update your local repository listings:
sudo apt update
-
Install Yarn:
sudo apt install yarn
This command installs Yarn and, if you don’t already have Node.js installed, your package manager will install it for you.
Option 2: Install Yarn Classic Using NPM
-
Check if npm is installed:
npm --version
If you don’t have npm, install it by runningsudo apt install npm
. -
To install Yarn with npm, enter:
sudo npm install -g yarn
To upgrade from classic Yarn to the latest version, follow these steps:
-
Run the
npm install
command to ensure that the classic Yarn is updated to the latest 1.x version:sudo npm install -g yarn
-
Switch to the modern Yarn version by typing:
yarn set version berry
Here are some basic Yarn commands you should know:
Creating a New Project
-
Create a directory for your application and navigate into it:
mkdir ~/my_project && cd ~/my_project
-
To create a new project, run
yarn init
.
Adding a Dependency
- Add an npm package to the 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]
Upgrading a Dependency
- To upgrade a package, use one of the following commands:
yarn upgrade
,yarn upgrade [package_name]
, oryarn upgrade [package_name]@[version_or_tag]
If no package name is given, the command will update all project dependencies to their latest version according to the version range specified in thepackage.json
file. Otherwise, only the specified packages are updated.
Removing a Dependency
- Use the
yarn remove
command followed by the package name to remove a dependency:yarn remove [package_name]
The command will remove the package and update thepackage.json
andyarn.lock
files.
Installing All Project Dependencies
- To install all project dependencies specified in the
package.json
file, run:yarn
oryarn install
You now have a comprehensive understanding of how to install and manage Yarn on your Ubuntu system. Whether you are using the latest version of Yarn or the classic version, you can benefit from Yarn's speed, reliability, and versatility. For more information about Yarn, visit the official Yarn documentation page.