Choose a different version or distribution
Introduction
Before, we begin talking about how to install Yarn on CentOS 8. First, let's understand – What is Yarn?
Yarn is a JavaScript package manager compatible with npm. It helps the user to automate the process of installing and updating, configuring, and removing the npm packages.
It speeds up the package installation process as it parallelizes the processes and reduces network connectivity errors.
In this tutorial, you will install Yarn on a CentOS 8 system. It will be from the Yarn RPM package repository. We will also address a few FAQs related to the Yarn installation.
Advantages of Yarn
- Dependency Resolution: Yarn uses a lock file to manage dependencies, ensuring that each developer has the same package versions installed. This helps prevent conflicts and ensures that the code runs consistently across different environments.
- Speed: Yarn is known for its speed, particularly when installing packages. Yarn caches packages on the local machine, which means that subsequent installs can be significantly faster.
- Workspaces: Yarn has a feature called workspaces that make it easy to manage multiple packages in a single repository. Workspaces allow developers to share code and dependencies between packages, which can be particularly helpful for monorepos.
- Scripts: Yarn allows developers to define custom scripts that can be run using the
yarn run
command. This is useful for automating common tasks, such as running tests, building the application, or deploying the code. - Offline Support: Yarn has a built-in offline cache that allows developers to work on projects without an internet connection. Yarn can use this cache to install packages even when offline, which can be particularly useful for developers working in remote or low-bandwidth environments.
Prerequisites to Install Yarn on CentOS 8
1) Make sure to log in as a user with sudo privileges.
Install Yarn on CentOS 8
1) If you don't have Node.js installed on your system, you need to enable the Nodesource repository with the following curl
command:
curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash -
2) After that, install the Node.js package using the following command:
sudo yum install nodejs
3) There is consistent maintenance of the official Yarn repository. It further, provides the most up-to-date version. To enable the Yarn repository and import the repository’s GPG key, run the below commands:
curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
sudo rpm --import https://dl.yarnpkg.com/rpm/pubkey.gpg
4) After the addition of the repository, install Yarn by running the following command:
sudo yum install yarn
5) Now, run the below command to confirm if yarn
is installed successfully:
yarn --version
Here, the latest version of Yarn is 1.17.3
:
Output
1.17.3
Install Yarn with NVM
You can install Yarn with NVM (Node Version Manager) on CentOS 7 by following these steps:
- Install NVM by running the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
This will download and run the NVM installation script, which will add the NVM executable to your shell's PATH.
2. Close and reopen your terminal, or run the following command to apply the changes to your current shell:
source ~/.bashrc
3. Install a version of Node.js using NVM. For example, you can install the latest LTS version by running:
nvm install --lts
4. Install Yarn using npm (which is installed with Node.js):
npm install -g yarn
5. Verify that Yarn is installed by running:
yarn --version
This should print the version number of Yarn.
That's it! You now have Yarn installed with NVM on CentOS 7.
Using Yarn
Now that Yarn is installed, let's explore some common Yarn commands.
Creating a New Project
To create a new project, use yarn init
command along with the project name.
yarn init my_project
The script will ask you few questions, either answer or press enter
to use default values:
Output
yarn init v1.17.3
question name (vagrant): Vegastack
question version (1.0.0): 0.0.1
question description: Testing Yarn
question entry point (index.js):
question repository url:
question author: Vegastack
question license (MIT):
question private:
success Saved package.json
Done in 20.18s.
Once completed, package.json
file is created by the script, and it contains the information you provided above. You can open the file and edit it at any time.
Adding Dependency
You can use any one of the following commands to upgrade the packages:
yarn install
yarn add [package_name]
yarn add [package_name]@[version_or_tag]
If no package name is given, the command will update the project dependencies to their latest version as per the version range given in the package.json
file. Otherwise, only the mentioned packages will be updated.
Upgrade the Dependencies
Use one of the following commands to upgrade the dependencies:
yarn upgrade [package_name]
yarn upgrade [package_name]@[version_or_tag]
Removing the Dependencies
You can remove a dependency using the following command:
yarn remove [package_name]
The above command will also update both package.json
and yarn.lock
files.
Install all Project Dependencies
You can install all the dependencies of an existing project mentioned in the package.json
file using:
yarn
or
yarn install
FAQs to Install Yarn on CentOS 8
Why should I install Yarn on CentOS 8?
Installing Yarn on CentOS 8 allows you to efficiently manage JavaScript packages and streamline your development workflow.
What are the prerequisites for installing Yarn on CentOS 8?
To install Yarn on CentOS 8, you should have CentOS 8 installed on your system. Additionally, you need to have Node.js and npm (Node Package Manager) already installed, as Yarn depends on them.
What is the recommended method to install Yarn on CentOS 8?
The recommended method to install Yarn on CentOS 8 is by using the Node.js Package Manager (npm).
Can I use Yarn without Node.js?
No, Yarn relies on Node.js and npm as prerequisites. Node.js acts as the runtime environment for executing JavaScript, while npm is used to download Yarn and manage its dependencies.
How do I verify if Yarn is installed correctly on CentOS 8?
To verify if Yarn is installed correctly on CentOS 8, you can run the command yarn --version
. This command should display the installed version of Yarn if it has been successfully installed.
Can I update Yarn to the latest version on CentOS 8?
Yes, you can update Yarn to the latest version on CentOS 8. You can use the command npm install -g yarn
to update to the latest available version of Yarn.
Are there alternative installation methods for Yarn on CentOS 8?
Yes, besides using npm, you can also install Yarn using other methods. For example, you can install Yarn using the Yarn installer script or by using the package manager dnf
to install it directly from the CentOS repositories.
Conclusion
We hope this detailed tutorial helped you to install Yarn on CentOS 8.
If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.