How To Install and Use Yarn on CentOS 7 with Easy Steps

Posted on

How To Install and Use Yarn on CentOS 7 with Easy Steps

How To Install and Use Yarn on CentOS 7 with Easy Steps

In the realm of modern web development, efficient dependency management is paramount. This guide on the Orcacore website aims to teach you How To Install and Use Yarn on CentOS 7. Yarn stands out as a popular package manager, streamlining the process of packaging projects and sharing them with the global developer community.

Yarn distinguishes itself with its speed, security, and consistency, qualities developed to address shortcomings found in its competitor, the Node Package Manager (npm).

Before proceeding, ensure you have a non-root user with sudo privileges on your CentOS 7 server. Our guide on Initial Server Setup with CentOS 7 provides detailed instructions on how to achieve this. This setup is a prerequisite for a smooth installation of How To Install and Use Yarn on CentOS 7.

1. Installing Node.js on CentOS 7

Yarn depends on Node.js. Thus, the first step is to install Node.js on your CentOS 7 server. Begin by updating your package index:

sudo yum update -y

Next, enable the Nodesource repository using the following command:

curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash -

Finally, install Node.js:

sudo yum install nodejs -y

2. Add Yarn GPG Key and Repository

To guarantee you’re installing the latest and most stable version of Yarn, it’s recommended to add the official Yarn repository. Start by enabling the repository and importing its GPG key:

# curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
Add Yarn Repository
sudo rpm --import https://dl.yarnpkg.com/rpm/pubkey.gpg

Apply the changes by running a system update:

sudo yum update -y

3. Install Yarn Package Manager on CentOS 7

With the repository added and updated, installing Yarn is now straightforward:

sudo yum install yarn -y

Confirm the installation’s success by checking the Yarn version:

yarn --version
**Output**
1.22.19

4. How To Use Yarn Command on CentOS 7?

Now that Yarn is installed, let’s explore its basic usage, including creating projects and managing dependencies. This is a crucial part of understanding How To Install and Use Yarn on CentOS 7.

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

For instance, to create a project named my_project:

yarn init my_project

The command prompts for project details. You can accept the defaults by pressing Enter or provide custom information.

yarn init command

This creates a package.json file containing the provided details. You can modify this file at any time.

You can also initiate a Yarn project in an existing directory. Navigate to the directory and run:

yarn init

To add a package as a dependency:

yarn add [package_name]

This installs the package and its dependencies, updating the package.json and yarn.lock files. By default, Yarn installs the latest version.

To install a specific version or tag:

yarn add [package_name]@[version_or_tag]

To upgrade packages, use:

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

Without a package name, the command updates all dependencies to their latest versions as defined in package.json. Otherwise, only the specified packages are updated.

To remove a package:

yarn remove [package_name]

This updates the package.json and yarn.lock files accordingly.

In an existing project, install all dependencies listed in package.json with:

yarn

Or:

yarn install

Alternative Solutions

While the yum package manager method is the recommended way to install Yarn on CentOS 7, alternative approaches exist, each with its own benefits and drawbacks.

1. Installing Yarn via npm (Node Package Manager)

Since Yarn is a Node.js package, it can be installed globally using npm, which comes bundled with Node.js. This method bypasses the need for adding a separate Yarn repository.

Explanation:

This approach leverages npm, the default package manager for Node.js, to install Yarn globally on the system. It’s a quick and easy method, particularly useful if you already have npm configured. However, it relies on npm for the installation process, potentially inheriting any issues or limitations associated with npm. It can also lead to version conflicts if not managed correctly.

Code Example:

sudo npm install -g yarn

After the installation, verify the installation by checking the Yarn version:

yarn --version

2. Using a Docker Container

Another approach involves using a Docker container that already has Yarn installed. This isolates Yarn and its dependencies from the host system, preventing potential conflicts.

Explanation:

This method utilizes Docker, a containerization platform, to run Yarn in an isolated environment. It ensures consistency across different systems and avoids dependency conflicts. This approach requires Docker to be installed on the system. It is especially useful for development and deployment scenarios where reproducibility is important.

Code Example:

First, ensure Docker is installed on your CentOS 7 system. Then, pull a Docker image that includes Node.js and Yarn, such as the official Node.js image with Yarn pre-installed.

docker pull node:lts-alpine

Next, run a container from the image, mounting your project directory to the container:

docker run -it --rm -v "$PWD:/app" -w /app node:lts-alpine yarn --version

This command executes the yarn --version command within the Docker container, showing the installed Yarn version. You can replace --version with any other Yarn command to manage your project’s dependencies.

Conclusion

In conclusion, installing Yarn on CentOS 7 is a straightforward process, with options available to suit different needs and preferences. The primary method involves adding the Yarn repository and using yum for installation. However, alternative methods, such as installing via npm or using Docker containers, offer flexibility and isolation. Choosing the right method depends on your specific requirements and the existing infrastructure. Understanding How To Install and Use Yarn on CentOS 7 is essential for modern web development.

Hope you enjoy it. You may also like these articles:

How to Change SSH Port in CentOS 7

How to install LAMP stack on CentOS 7

Set up Apache virtual host on CentOS 7

How to install LEMP stack on CentOS 7

Leave a Reply

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