Install and Use Yarn on Ubuntu 20.04 with Efficient Steps

Posted on

Install and Use Yarn on Ubuntu 20.04 with Efficient Steps

Install and Use Yarn on Ubuntu 20.04 with Efficient Steps

In this comprehensive guide, brought to you by Orcacore, we will walk you through the process of how to install and use Yarn on Ubuntu 20.04. Yarn, developed by Facebook, emerged as a solution to address certain limitations encountered with npm (Node Package Manager). While not strictly a replacement for npm, Yarn leverages the npm registry for its modules.

Think of Yarn as an alternative package installer that utilizes the same npm infrastructure. The fundamental registry remains unchanged; however, the installation methodology differs. Because Yarn grants access to the same extensive package ecosystem as npm, transitioning from npm to Yarn involves minimal disruption to your established workflow. This makes learning how to install and use Yarn on Ubuntu 20.04 a valuable skill.

Before diving into the installation, ensure you are logged in to your Ubuntu 20.04 server as a non-root user with sudo privileges. If needed, you can refer to our article on Initial Server Setup with Ubuntu 20.04. Now, let’s proceed with the steps to install and use Yarn on Ubuntu 20.04.

1. Installing Yarn on Ubuntu 20.04

First, import the repository’s GPG key and add the Yarn APT repository to your system using the following commands:

$ 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

After enabling the Yarn repository, update your local package index with the following command:

sudo apt update

Next, install Yarn using the following command:

sudo apt install yarn -y

This command will also install Node.js, which is a prerequisite for Yarn to function correctly.

  • Note: If you have already installed Node.js using NVM (Node Version Manager), you can skip the Node.js installation by using the following command:
sudo apt install --no-install-recommends yarn

Now, verify the Yarn installation on your Ubuntu 20.04 system by checking its version:

yarn --version

The output should display the installed Yarn version, similar to this:

**Output**
1.22.22

The version number may differ depending on the latest available release. If you see a version number, you have successfully installed Yarn on your Ubuntu 20.04 system. Now you can explore how to install and use Yarn on Ubuntu 20.04 by using it.

2. How To Use Yarn on Ubuntu 20.04?

This section will demonstrate how to use Yarn by creating a new project and adding/removing dependencies.

To create a new Yarn project, use the yarn init command.

Let’s create a project named "my_project". You can, of course, choose a different name:

yarn init my_project

This command will prompt you with a series of questions. Press Enter to accept the default values or provide your own answers.

The interaction will resemble this:

question name (my_project):
question version (1.0.0):
question description:
question entry point (index.js):
question repository url:
question author:
question license (MIT):
success Saved package.json
Done in 0.23s.

This command creates a basic package.json file containing the information you entered. You can modify this file at any time to adjust the project’s metadata.

Alternatively, you can initiate a Yarn project in an existing directory on Ubuntu 20.04. To do this, navigate to the directory and execute the following command:

yarn init

To add a package as a dependency to your project, use the yarn add command:

yarn add [package_name]

This command installs the specified package and any packages it depends on. It also updates the project’s package.json and yarn.lock files. The yarn.lock file ensures that all team members or deployment environments use the exact same versions of dependencies, preventing unexpected issues caused by version mismatches.

If you only specify the package name, Yarn will install the latest version.

To install a specific version or tag, use the following syntax:

yarn add [package_name]@[version_or_tag]

You can upgrade packages using the following commands:

$ yarn upgrade
$ yarn upgrade [package_name]
$ yarn upgrade [package_name]@[version_or_tag]

If no package name is provided, the yarn upgrade command updates all project dependencies to their latest versions, respecting the version ranges specified in the package.json file. If a package name is specified, only that package is updated.

To remove a package from your project dependencies, use the yarn remove command on Ubuntu 20.04:

yarn remove [package_name]

This command removes the specified package and updates the project’s package.json and yarn.lock files.

In an existing project, you can install all dependencies specified in the package.json file using the following command:

yarn

Or, equivalently:

yarn install

This command reads the package.json file and downloads and installs all the required dependencies, ensuring that your project has all the necessary components to run correctly.

Alternative Installation Methods

While the APT package manager method is the most straightforward way to install Yarn on Ubuntu 20.04, here are two alternative approaches:

1. Installing Yarn via npm (Node Package Manager):

Since Yarn is built on top of Node.js, you can leverage npm to install Yarn globally. This method is particularly useful if you already have npm installed and configured.

First, ensure you have Node.js and npm installed. Then, execute the following command:

npm install -g yarn

This command installs Yarn globally, making it accessible from any directory in your terminal. After the installation, verify the installation by checking the Yarn version:

yarn --version

This method is convenient if you prefer using npm for package management and want to avoid adding a new repository to your system.

2. Installing Yarn by Downloading the Tarball:

Another approach involves downloading the Yarn tarball directly from the Yarn website and manually configuring it. This method provides more control over the installation process and allows you to install Yarn in a specific location.

  • Visit the official Yarn website and download the latest stable tarball.
  • Extract the tarball to a directory of your choice (e.g., /opt/yarn).
  • Add the Yarn bin directory to your system’s PATH environment variable. This allows you to execute Yarn commands from any directory. You can achieve this by adding the following line to your ~/.bashrc or ~/.zshrc file:
export PATH="$PATH:/opt/yarn/bin"

Replace /opt/yarn with the actual path where you extracted the tarball.

  • Source your shell configuration file to apply the changes:
source ~/.bashrc  # or source ~/.zshrc

Finally, verify the installation by checking the Yarn version:

yarn --version

This method offers flexibility in terms of installation location and avoids relying on package managers. However, it requires more manual configuration.

Conclusion

Yarn is rapidly gaining popularity due to its superior performance, ease of installation, and a wealth of convenient features. This article aimed to provide a clear and concise guide on how to install and use Yarn on Ubuntu 20.04. By following the steps outlined above, you can seamlessly integrate Yarn into your development workflow and benefit from its advantages.

Please subscribe to us on YouTube and X.

You may also like to read the following articles:

  • Set up MariaDB 10.8 on Ubuntu 20.04
  • Install AnyDesk on Ubuntu 20.04
  • Set up Gulp on Ubuntu 20.04
  • Install and Use chkrootkit on Ubuntu 20.04

Leave a Reply

Your email address will not be published. Required fields are marked *