Install and Use Yarn on Ubuntu 22.04

This tutorial intends to teach you How To Install and Use Yarn on Ubuntu 22.04.

Yarn is a JavaScript package manager used to automate the installation, configuration, update, and removal of npm packages. Yarn stands for “Yet Another Resource Navigator.” It was developed by Facebook and is very similar to the npm package manager, with a focus on speed, security, and consistency. Yarn replaces the existing workflow of the npm client or other package managers while remaining compatible with the npm registry.

Steps To Install and Use Yarn on Ubuntu 22.04

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide the Initial Server Setup with Ubuntu 22.04.

Installing Node.js on Ubuntu 22.04

To install Yarn, you must install Node.js on your server. First, update your local package index with the following command:

sudo apt update

Then, install the required packages on your server by running the command below:

sudo apt install software-properties-common apt-transport-https wget ca-certificates gnupg2 gcc make g++ curl -y

Next, import the NodeSource repository that suits your needs in either the current release or the LTS version to install both packages:

# curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
# curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo bash -

Add Yarn GPG Key and Repository

Now that you have Node.js installed on your server, you can start to install Yarn on your server.

First, import the Yarn package manager GPG key:

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null

Then, import the Yarn repository:

echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

Run the system update:

sudo apt update

Install Yarn Package Manager on Ubuntu 22.04

At this point, you can simply install Yarn and Node.js on your server. To do this, run the command below:

sudo apt install yarn nodejs

Verify your Yarn installation by checking its version:

yarn -v
Output
1.22.19

Also, you can use the command below instead:

apt-cache policy yarn
Output
yarn:
  Installed: 1.22.19-1
  Candidate: 1.22.19-1
  Version table:
 *** 1.22.19-1 500
        500 https://dl.yarnpkg.com/debian stable/main amd64 Packages
        500 https://dl.yarnpkg.com/debian stable/main all Packages
        100 /var/lib/dpkg/status
     1.22.18-1 500
        500 https://dl.yarnpkg.com/debian stable/main amd64 Packages
        500 https://dl.yarnpkg.com/debian stable/main all Packages
     1.22.17-1 500
        500 https://dl.yarnpkg.com/debian stable/main amd64 Packages
        500 https://dl.yarnpkg.com/debian stable/main all Packages
     1.22.15-1 500
...

How To Use Yarn Command

At this step, we will show you how to use Yarn by creating a new project and adding/removing dependencies.

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

Here we create a project named my_project, you can choose another name for it:

yarn init my_project

This command will ask you some questions. Press enter to accept the defaults or fill them with your answers.

This will look like this:

Output
yarn init v1.22.19
question name (root):
question version (1.0.0):
question description:
question entry point (index.js):
question repository url:
question author:
question license (MIT):
question private:
success Saved package.json
Done in 21.27s.

This command will create a basic package.json file that contains the information you have provided.

You can modify this file any time you want.

Also, you can initiate a Yarn project in an existing directory on Ubuntu 22.04. To do this, switch to the directory and run the following command:

yarn init

To add a package as a dependency to your project you can use the following command:

yarn add [package_name]

With this command, you can install the package and any packages that it depends on. Also, it will update the project’s package.json and yarn.lock files.

If you use only the package name in the command, Yarn will install it in the latest version.

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

yarn add [package_name]@[version_or_tag]

Also, you can upgrade the packages with the following commands:

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

If no package name is given, the command will update the project dependencies to their latest version according to the version range specified in the package.json file. Otherwise, only the specified packages are updated.

To remove a package from your project dependency you can use the Yarn remove command on Ubuntu 22.04:

yarn remove [package_name]

This command will also update the project’s package.json and yarn.lock files.

In an existing project you can install all dependencies that are specified in the package.json file with the following command:

yarn

Or you can use:

yarn install

Conclusion

Yarn is becoming increasingly popular thanks to its superior performance, easy installation, and numerous convenient features.

Hope you enjoy this article about installing and using Yarn on Ubuntu 22.04.

For more articles and guides, you can visit the Orcacore website.

14 COMMENTS
  1. Hello friends, pleasant piece of writing and nice urging commented at this
    place, I am actually enjoying by these.

  2. I like the valuable info you supply to your articles.
    I’ll bookmark your weblog and check again here frequently.
    I am slightly sure I’ll learn many new stuff proper here!

    Best of luck for the following!

  3. Thank you for any other fantastic post. Where else may anyone get that kind of info in such
    an ideal manner of writing? I’ve a presentation subsequent week, and I am on the search
    for such information.

  4. Sweet blog! I found it while browsing on Yahoo News. Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get
    there! Thank you

    • Hi there, we didn’t do anything special. When you search for a subject yahoo or google look for the topic on the sites that are trending for that subject and they’ll show you about it.

  5. When someone writes an article he/she keeps the
    thought of a user in his/her mind that how a user can understand it.
    So that’s why this paragraph is amazing. Thanks!

  6. My brother recommended I might like this web site. He was entirely right.

    This post truly made my day. You can not imagine just how much time I had spent for this information! Thanks!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular