How to install Yarn on Linux (AlmaLinux)
This article will walk you through the Yarn installation process on AlmaLinux in simple steps. AlmaLinux provides a solid base for a wide range of development projects, and installing Yarn on this platform can help with project dependency management. We will also explore how Liquid Web products can help this installation by offering a reliable hosting environment and competent support.
To read up on how to install Yarn on Windows and Ubuntu, refer to the following articles:
Key points
The main points to take away after reading this article include:
- Installing Yarn using the Yarn RPM package.
- Installing Yarn using Node Package Manager (NPM).
- Installing packages using Yarn.
- Creating a sample project in Yarn.
- Updating Yarn.
- Uninstalling Yarn.
What is Yarn?
Yarn is a fast, reliable, secure package manager for JavaScript developed by Facebook. It is crucial in modern software development for efficiently managing dependencies, ensuring consistent installations across different environments, and avoiding version conflicts. Yarn's use of a lock file (yarn.lock) and a global cache for downloaded packages streamline the development process, making it an essential tool for developers on platforms like AlmaLinux, where it simplifies the management of JavaScript dependencies in projects
Prerequisites
- Operating System and Version: AlmaLinux OS 8.
- Ensure you have root or sudo access to install the Yarn.
How to Install Yarn on Linux (AlmaLinux)
To install Yarn on Linux (AlmaLinux), please follow the below steps.
Step #1. Access your Liquid Web server
Gain access to your Liquid Web server using SSH or another remote access method.
Step #2. Update your AlmaLinux system
The AlmaLinux system must be updated to the latest version before installing Yarn. Use the following command to update the AlmaLinux system.
sudo dnf update
Step #3. Install Yarn on Linux (AlmaLinux)
To install Yarn on Linux (AlmaLinux), use the Yarn RPM package or NPM.
Option 3.1. Using the Yarn RPM package
To install Yarn on Linux (AlmaLinux) using the Yarn RPM package, follow these step.
3.1.1. Enable the Yarn repository
To add the Yarn repository, use the following command:
echo -e "[yarn]
name=Yarn Repository
baseurl=https://dl.yarnpkg.com/rpm/
gpgcheck=1
gpgkey=https://dl.yarnpkg.com/rpm/pubkey.gpg
enabled=1" | sudo tee /etc/yum.repos.d/yarn.repo
3.1.2. Install Yarn
To install Yarn on Linux (AlmaLinux), use the following command:
sudo dnf install yarn
Here is the output:
[root@yarn-alma ~]# sudo dnf install yarn
Yarn Repository 1.7 MB/s | 316 kB 00:00
Dependencies resolved.
====================================================================================================================================================
Package Architecture Version Repository Size
====================================================================================================================================================
Installing:
yarn noarch 1.22.19-1 yarn 1.2 M
Transaction Summary
====================================================================================================================================================
Install 1 Package
Total download size: 1.2 M
Installed size: 5.1 M
Is this ok [y/N]: y
Downloading Packages:
yarn-1.22.19-1.noarch.rpm 8.0 MB/s | 1.2 MB 00:00
----------------------------------------------------------------------------------------------------------------------------------------------------
Total 7.9 MB/s | 1.2 MB 00:00
Yarn Repository 18 kB/s | 1.6 kB 00:00
Importing GPG key 0x6963F07F:
Userid : "Yarn RPM Packaging <yarn@dan.cx>"
Fingerprint: 9A6F 73F3 4BEB 7473 4D8C 6914 9CBB B558 6963 F07F
From : https://dl.yarnpkg.com/rpm/pubkey.gpg
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : yarn-1.22.19-1.noarch 1/1
Verifying : yarn-1.22.19-1.noarch 1/1
Installed:
yarn-1.22.19-1.noarch
Complete!
3.1.3. Verify the installation on Linux (AlmaLinux)
To verify that the Yarn is installed, use the following command:
yarn -v
Alternatively, you can use this syntax for the command:
yarn --version
Here is the output:
[root@yarn-alma ~]# yarn -v
1.22.19
[root@yarn-alma ~]# yarn --version
1.22.19
Option 3.2. Using Node Package Manager (NPM)
To install Yarn on Linux (AlmaLinux) using the NPM, follow these steps:
3.2.1. Install Node.js and NPM
Before installing Node.js and NPM, check available Node.js streams using the following command:
sudo dnf module list nodejs
Here is the output:
[root@yarn-alma ~]# sudo dnf module list nodejs
Last metadata expiration check: 1:49:55 ago on Sun 25 Feb 2024 08:02:49 AM UTC.
AlmaLinux 8 - AppStream
Name Stream Profiles Summary
nodejs 10 [d] common [d], development, minimal, s2i Javascript runtime
nodejs 12 common [d], development, minimal, s2i Javascript runtime
nodejs 14 common [d], development, minimal, s2i Javascript runtime
nodejs 16 common [d], development, minimal, s2i Javascript runtime
nodejs 18 common [d], development, minimal, s2i Javascript runtime
nodejs 20 common [d], development, minimal, s2i Javascript runtime
Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
Then, install Node.js using the following command:
sudo dnf module install nodejs:16
Here is the output:
[root@yarn-alma ~]# sudo dnf module install nodejs:16
Last metadata expiration check: 1:50:13 ago on Sun 25 Feb 2024 08:02:49 AM UTC.
Dependencies resolved.
====================================================================================================================================================
Package Architecture Version Repository Size
====================================================================================================================================================
Installing group/module packages:
nodejs x86_64 1:16.20.2-3.module_el8.8.0+3653+c77a731f appstream 12 M
npm x86_64 1:8.19.4-1.16.20.2.3.module_el8.8.0+3653+c77a731f appstream 1.9 M
Installing weak dependencies:
nodejs-docs noarch 1:16.20.2-3.module_el8.8.0+3653+c77a731f appstream 9.4 M
nodejs-full-i18n x86_64 1:16.20.2-3.module_el8.8.0+3653+c77a731f appstream 8.0 M
Installing module profiles:
nodejs/common
Enabling module streams:
nodejs 16
Transaction Summary
====================================================================================================================================================
Install 4 Packages
—-
—-
Installed:
nodejs-1:16.20.2-3.module_el8.8.0+3653+c77a731f.x86_64 nodejs-docs-1:16.20.2-3.module_el8.8.0+3653+c77a731f.noarch
nodejs-full-i18n-1:16.20.2-3.module_el8.8.0+3653+c77a731f.x86_64 npm-1:8.19.4-1.16.20.2.3.module_el8.8.0+3653+c77a731f.x86_64
Complete!
3.2.2. Verify the version of Node.js installed
Once successfully installed, verify the version of Node.js installed using the following commands:
node -v
Or:
node --version
Here is the output:
[root@yarn-alma ~]# node -v
V16.20.2
[root@yarn-alma ~]# node --version
v16.20.2
3.2.3. Checked he version of NPM installed
To check the version of NPM, use the following commands:
npm -v
Or
npm --version
Here is the output:
[root@yarn-alma ~]# npm -v
8.19.4
[root@yarn-alma ~]# npm --version
8.19.4
3.2.4. Install Yarn using NPM
Once Node.js and NPM are installed, use NPM to install Yarn globally.
sudo npm install -g yarn
Here is the output:
[root@yarn-alma ~]# sudo npm install -g yarn
added 1 package, and audited 2 packages in 858ms
found 0 vulnerabilities
3.2.5. Verify the installation on Linux (AlmaLinux)
To verify that the Yarn is installed, use the following commands:
yarn -v
Or
yarn --version
Here is the output:
[root@yarn-alma ~]# yarn -v
1.22.19
[root@yarn-alma ~]# yarn --version
1.22.19
Install packages using Yarn
To install a package using Yarn, use the following command:
yarn add [package_name]
Replace the [package_name] placeholder as per your requirements.
For example, to install the Express.js framework for Node.js applications, use the following command:
yarn add express
Here is the output:
[root@yarn-alma sample-yarn-project]# yarn add express
yarn add v1.22.19
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 38 new dependencies.
info Direct dependencies
└─ express@4.18.2
info All dependencies
├─ accepts@1.3.8
├─ array-flatten@1.1.1
├─ body-parser@1.20.1
├─ call-bind@1.0.7
├─ content-disposition@0.5.4
├─ cookie-signature@1.0.6
├─ cookie@0.5.0
├─ define-data-property@1.1.4
├─ ee-first@1.1.1
├─ express@4.18.2
├─ finalhandler@1.2.0
├─ forwarded@0.2.0
├─ get-intrinsic@1.2.4
├─ has-property-descriptors@1.0.2
├─ has-proto@1.0.3
├─ has-symbols@1.0.3
├─ hasown@2.0.1
├─ inherits@2.0.4
├─ ipaddr.js@1.9.1
├─ media-typer@0.3.0
├─ merge-descriptors@1.0.1
├─ methods@1.1.2
├─ mime-db@1.52.0
├─ mime-types@2.1.35
├─ mime@1.6.0
├─ ms@2.0.0
├─ negotiator@0.6.3
├─ object-inspect@1.13.1
├─ path-to-regexp@0.1.7
├─ proxy-addr@2.0.7
├─ raw-body@2.5.1
├─ safer-buffer@2.1.2
├─ serve-static@1.15.0
├─ set-function-length@1.2.1
├─ side-channel@1.0.5
├─ toidentifier@1.0.1
├─ utils-merge@1.0.1
└─ vary@1.1.2
Done in 2.38s.
Create a sample project in Yarn
To create a sample project using Yarn, follow these steps:
Step #1. Create a directory for the sample project
To create a directory for the sample project, use the following command:
mkdir sample-yarn-project
Then, move to the directory using the command:
cd sample-yarn-project
Step #2. Initialize a sample project with Yarn
To Initialize the sample project with Yarn, use the following command:
yarn init
Enter details about the sample project, such as name, version, description, and license as prompted. Press the Enter key to accept the default values for most prompts.
Here is the output:
[root@yarn-alma sample-yarn-project]# yarn init
yarn init v1.22.19
question name (sample-yarn-project): sample-yarn-project
question version (1.0.0): 1.0.0
question description: This is the sample yarn project
question entry point (index.js):
question repository url:
question author:
question license (MIT): MIT
question private:
success Saved package.json
Done in 200.03s.
Step #3. Install a sample package
For example, to install the lodash package, which provides utility functions for JavaScript, use the following command:
yarn add lodash
Here is the output:
[root@yarn-alma sample-yarn-project]# yarn add lodash
yarn add v1.22.19
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ lodash@4.17.21
info All dependencies
└─ lodash@4.17.21
Done in 2.24s.
Step #4. Create a JavaScript file (index.js) in the project directory
Open the JavaScript file index.js and use the following command:
nano index.js
Then, add the following details:
// index.js
const _ = require('lodash');
console.log(_.capitalize('This is the sample yarn project!'));
Step #5. Run the JavaScript file
To run the JavaScript file, use the following command:
node index.js
Here is the output:
[root@yarn-alma sample-yarn-project]# node index.js
This is the sample yarn project!
Step #6. Add scripts to the package.json (optional)
To run commonly used commands, add scripts to the package.json file. For example, add a script to run the index.js file:
"scripts": {
"start": "node index.js"
}
Here is the output:
Then, run the script using the following command:
yarn start
Here is the output:
[root@yarn-alma sample-yarn-project]# yarn start
yarn run v1.22.19
$ node index.js
This is the sample yarn project!
Done in 0.15s.
How to update Yarn
If Yarn was installed using Yarn's RPM package, use the following command:
sudo dnf update yarn
If Yarn was installed using NPM, use the following command:
npm install -g yarn
How to uninstall Yarn
If Yarn was installed using Yarn's RPM package, use the following command to uninstall yarn:
sudo dnf remove yarn
If Yarn was installed using NPM, use the following command to uninstall yarn:
npm uninstall -g yarn
Empower your projects — installing Yarn on AlmaLinux
The installation steps outlined in the article give a simple but detailed tutorial for installing Yarn on on Linux — AlmaLinux — with the added benefit of using Liquid Web products for a seamless experience. These steps allow you to install Yarn quickly and easily on your AlmaLinux servers.
By choosing Liquid Web, you can access robust hosting solutions and benefit from your server management and optimization expertise. This combination ensures a smooth and efficient installation process, allowing you to focus more on your projects and less on setup and configuration. With Yarn and Liquid Web, you're equipped to tackle complex development tasks easily, all while knowing that your infrastructure is in good hands.
Related Articles:
About the Author: Mohammed Noufal
Mohammed Noufal is a B.Tech graduate with a decade of experience in server administration and web hosting. He is a father to two daughters and finds fulfillment in their growth. In his free time, he enjoys blogging, sharing experiences, and listening to music. With a strong technical background, family commitment, and creative outlets, he represents a well-rounded life journey.
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.
Latest Articles
How to install PyTorch on Linux (AlmaLinux)
Read ArticleInnovative armor — exploring ThreatDown’s impact on cyber defense
Read ArticleControlling PHP settings with a custom php.ini file
Read ArticleLinux dos2unix command syntax — removing hidden Windows characters from files
Read ArticleChange cPanel password from WebHost Manager (WHM)
Read Article