Yarn is a fast, reliable, and secure dependency management tool for JavaScript projects, offering a superior caching mechanism, network performance, and more intuitive user interface than its counterparts. As an essential tool for modern web development, installing Yarn on your system ensures you can manage project dependencies efficiently.
This article will guide you through 4 effective methods to install Yarn on Ubuntu 22.04, covering various needs and preferences.
- Installing Yarn via NPM
- Installing Yarn via the Official APT Repository
- Installing Yarn via the Ubuntu Software Package
- Installing Yarn via Official Shell Script
Method 1: Installing Yarn via NPM
Prerequisites: Node.js installed on your system. If you don’t have Node.js installed, you can download it from nodejs.org or install it using Ubuntu’s package manager.
- Update your package list to ensure you can download the most recent versions of packages and their dependencies.
sudo apt update
- Install Node.js. If you haven’t installed Node.js yet, you can do so by running:
sudo apt install nodejs npm
- Install Yarn using npm (Node Package Manager) by running:
npm install --global yarn
- Verify the installation by checking the version of Yarn installed:
yarn --version
This method is ideal for users who prefer using npm and want to manage Yarn as a global npm package.
Method 2: Installing Yarn via the Official APT Repository
This method ensures you receive updates directly from the Yarn maintainers.
- Configure the Yarn repository by adding the Yarn package source to your system.
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
- Update your package list to recognize the newly added Yarn repository.
sudo apt update
- Install Yarn. Note that if you do not need Node.js, you can avoid installing it by appending –no-install-recommends when installing Yarn.
sudo apt install yarn --no-install-recommends
- Verify the installation by running:
yarn --version
This method is recommended for those who want the most straightforward installation process, directly from the official source.
Method 3: Installing Yarn via the Ubuntu Software Package
This method involves using Ubuntu’s built-in package manager without adding an external repository.
- Update your package list to ensure your system is up-to-date.
sudo apt update
- Install Yarn by running:
sudo apt install yarn
- Verify the installation by checking Yarn’s version:
yarn --version
While convenient, this method might not always provide the latest version of Yarn available compared to the official Yarn repository or npm.
Method 4: Installing Yarn via Official Shell Script
For those who prefer a more automated approach or need to install Yarn across multiple systems efficiently, using Yarn’s official shell script for installation can be a convenient option. This method ensures that you get the latest version directly from Yarn’s official source without manually adding repositories. Here’s how to do it:
- Open a terminal on your Ubuntu 22.04 system.
- Download and execute the Yarn installation script. The Yarn team provides a script that automates the installation process. You can download and execute it by running:
curl -o- -L https://yarnpkg.com/install.sh | bash
This command downloads the script and pipes it directly to bash for execution. It handles the configuration of your environment to use Yarn globally.
- Reload your profile. After installation, the script advises you to either open a new terminal session or reload your profile. Reloading your profile can be done with the following command:
source ~/.bashrc
If you’re using a different shell (like Zsh or Fish), make sure to reload the configuration file corresponding to your shell (e.g., ~/.zshrc for Zsh).
- Verify the installation by checking the installed version of Yarn. Once the script completes its execution, you can verify that Yarn is successfully installed by checking its version:
yarn --version
The shell script method is particularly useful for developers who need a quick and reliable installation method that fetches the latest version of Yarn directly from the official source. This approach is also helpful for scripting and automating the setup of development environments.
Conclusion
Choosing the right installation method depends on your specific needs, whether it’s the ease of management, staying up-to-date with the latest versions, or simply using existing tools like npm. Each of these methods provides a reliable way to install Yarn on Ubuntu 22.04, ensuring you’re equipped with one of the most powerful tools for modern web development. Remember to verify your installation by checking the Yarn version, ensuring that Yarn is correctly installed and ready to manage your project’s dependencies.