Installing and Using the Yarn Package Manager on Linux
Yarn is a powerful package manager for JavaScript designed to make dependency management simpler and more efficient. Originally developed by Facebook, it became popular due to its speed and reliability compared to other package managers such as npm (Node Package Manager). In this article, youβll learn how to install Yarn on Linux, how to use it, and what the main differences are between Yarn and npm (including pros and cons).
Installing Yarn on Linux
Yarn can be installed on various Linux distributions. Below are common installation methods.
Method 1: Install Yarn using APT
This is the most common way to install Yarn on Debian-based systems.
- Update the package list:
sudo apt update - Install required dependencies:If curl is not installed, install it:
sudo apt install curl - Add the Yarn APT repository:
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/yarn.gpg echo "deb [signed-by=/etc/apt/keyrings/yarn.gpg] https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list - Update the package list again:
sudo apt update - Install Yarn:
sudo apt install yarn - Verify the installation:
yarn --version
Method 2: Install Yarn using npm
If Node.js and npm are already installed, you can install Yarn via npm:
- Install Node.js and npm (if not installed):
sudo apt install nodejs npm - Install Yarn globally using npm:
npm install --global yarn
Using common Yarn commands
- Initialize a new project:Go to your project directory and run:
yarn initFollow the prompts to create a package.json file.
- Add a dependency:
yarn add package-name - Add a development dependency:
yarn add package-name --dev - Remove a dependency:
yarn remove package-name - Install all dependencies:If you have a package.json, install dependencies with:
yarn install - Upgrade a dependency:
yarn upgrade package-name
Running scripts
Yarn can also run scripts defined in package.json:
yarn run script-name
Yarn vs npm: differences, pros, and cons
Key differences
- Lockfiles:
- Yarn: Uses yarn.lock to lock dependency versions, helping ensure consistent installs across environments.
- npm: Introduced a similar mechanism in npm 5 with package-lock.json.
- Install speed:
- Yarn: Often faster due to parallel installs and caching.
- npm: Historically slower, but improved significantly in newer versions.
- CLI commands:
- Many commands are similar, but Yarn includes unique commands such as yarn upgrade-interactive for interactive upgrades.
- Workspaces:
- Yarn: Supports workspaces for managing monorepos.
- npm: Added workspace support in npm 7.
Yarn: pros and cons
Pros:
- Speed: Faster installs via caching and parallelism.
- Deterministic installs: Consistent installs across environments using yarn.lock.
- User-friendly CLI: Often considered more intuitive with better output.
- Workspaces: Built-in monorepo management support.
Cons:
- Dependency on Node.js: Node.js must be installed before using Yarn.
- Learning curve: New users may need time to get used to differences compared to npm.
npm: pros and cons
Pros:
- Widespread adoption: Bundled with Node.js, making it commonly used and well supported.
- Mature ecosystem: Large community and extensive troubleshooting resources.
- Simplicity: Familiar to most JavaScript developers.
Cons:
- Speed: Historically slower than Yarn, although recent updates improved performance.
- Less deterministic (historically): Prior to npm 5, installs could vary across environments without a lockfile.
Conclusion
Yarn is a powerful package manager that offers features aimed at improving the JavaScript development workflow. Thanks to its speed, deterministic installs, and convenient commands, it has become a popular choice among developers. While npm remains widely used and continues to evolve, the choice between Yarn and npm often comes down to personal or team preference. By understanding the strengths and weaknesses of both, you can make a more informed decision about which package manager best fits your projectβs needs.
