How to install Yarn on an Ubuntu 22.04 VPS or Dedicated Server

How to install Yarn on an Ubuntu 22.04 VPS or Dedicated Server

Introduction

Yarn is a fast, secure, and reliable dependency manager designed to resolve some of the shortcomings exhibited by npm. The application relies on npm registry modules and is not a replacement for the npm. It caches all the packages it downloads and parallelizes operations to boost resource utilization to expedite install times. This tutorial will show you how to install and use Yarn on Ubuntu 22.04 VPS or Dedicated server through the APT package manager. This repository is updated consistently and avails the most recent Yarn’s version.

Before You Begin

For this tutorial to run flawlessly requires a fully configured Ubuntu 22.04 at a good VPS hosting or dedicated server hosting service.

Method 01: Installing Yarn from Ubuntu apt repository

The first thing when installing Yarn on Ubuntu 22.04 is adding a Yarn repository together with the signing key the repository. Issue the commands below:

$ curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -

screenshot of Install Yarn on an Ubuntu

$ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

screenshot of Install Yarn on an Ubuntu

Next, run the following commands to update your system and deploy Yarn on your system:

$ sudo apt-get update

screenshot of Install Yarn on an Ubuntu

$ sudo apt install yarn

screenshot of Install Yarn on an Ubuntu

The command above will automatically install Yarn if you have not installed Node.js on your server. However, if you are using the Node.js Version Manager, run the command below to skip the installation of Node.js:

$ sudo apt install--no-install-recommends yarn

Next, execute the command below to check the Yarn’s version:

$ sudo yarn --version

This will give you an output similar to the one below:

screenshot of Install Yarn on an Ubuntu

Method 2: Install latest version of Yarn on Ubuntu 22.04

To install the latest version of yarn, download the ‘Yarn’ tarball by writing out the following command in the Ubuntu 22.04 terminal:

For installing npm package:

$ sudo apt install npm

screenshot of Install Yarn on an Ubuntu

Now, download and install yarn:

As we can see in the above screenshot, yarn latest version 1.22.19 has been installed on this system.

Step 2 – Using Yarn

By now you have deployed Yarn on your system, next you need to explore the most commonly used commands for Yarn:

Creating Projects

To generate a Yarn project execute the command below:

$ sudo yarn init my_yarn_project

The above init command will prompt you to answer several questions. Answer the questions accordingly or hit ENTER to skip them.

screenshot of Install Yarn on an Ubuntu

Once you answer all the question, Yarn will generate a new package.json file featuring the details you provided. The file is readily available and can be opened and edited at any time.

Adding Dependencies

The yarn add is a command used to add a new package to dependencies of a project. The command used to add the package takes the form:

$ sudo yarn add [package_name]

For example, here we are installing ‘react’ package using yarn:

$ sudo yarn add react

screenshot of Install Yarn on an Ubuntu

This command will update the yarn.lock and package.json files. Updating these files enables everyone working on the project to get similar dependencies when using yarn. Besides, you can use the yarn add command and specify the version or tag for the package you want to add:

$ sudo yarn add [package_name]@[version_or_tag]

Upgrading The Dependencies

If you want to use the latest version of a specific package, you can execute the command below:

$ sudo yarn upgrade [package_name]

Alternatively, if you want to upgrade the dependency to a specific version or tag, issue the command below:

$ sudo yarn upgrade [package_name]@[version_or_tag]

Removing A Dependency

If you want to expunge a certain dependency, execute the command below:

$ sudo yarn remove [package_name]

screenshot of Install Yarn on an Ubuntu

Installing All Dependencies

If you want to install the project dependencies specified in a package.json you can execute one of the commands below:

$ sudo yarn

Or

$ sudo yarn install

screenshot of Install Yarn on an Ubuntu

Conclusion

That is It! You have installed Yarn on Ubuntu 22.04 VPS or Dedicated server and learned how to implement some of the most commonly used Yar commands. For in-depth details regarding Yarn usage, go to the official Yarn documentation website page.

How to Set Up Cron Jobs on Your Ubuntu 18.04 Dedicated Server or VPS

This guide shows webmasters and administrators how to set up cron jobs on your U
3 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Create a Non-root User on Your Ubuntu 18.04 VPS or Dedicated Server

This guide shows you how to create a user on your Ubuntu 18.04 virtual server.
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester

How to Install a Self-Signed SSL Certificate on Your Ubuntu 18.04 VPS or Dedicated Server

This how-to article will teach you how to create a self-signed SSL certificate o
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester

How to Install WordPress on Your Ubuntu 22.04 VPS or Dedicated Server

This tutorial will show you how to install WordPress on your Ubuntu 22.04 virtua
3 min read
Idan Cohen
Idan Cohen
Marketing Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top