Sep 27, 2023 4 min read

How to Install Yarn on Ubuntu 18.04

Install Yarn on Ubuntu 18.04 with our step-by-step tutorial. Yarn is a popular package manager for JavaScript projects.

Install Yarn on Ubuntu 18.04
Table of Contents

Choose a different version or distribution

Introduction

Before, we begin talking about how to install Yarn on Ubuntu 18.04. Let's briefly understand – What is Yarn?

Yarn is a popular package manager for JavaScript projects that was developed by Facebook. It helps developers manage their dependencies more efficiently by allowing them to install, update, and remove packages easily. With Yarn, developers can also take advantage of its caching and parallel installation features to speed up their project builds.

Yarn uses a lockfile to ensure that all team members are using the same versions of packages, which helps prevent conflicts and makes the development process more streamlined. It also provides a command-line interface that is similar to npm, another popular package manager, making it easy for developers to switch between the two.

If you're working on a JavaScript project and want to simplify your dependency management, Yarn is definitely worth considering. Its popularity and ease of use have made it a go-to tool for many developers in the JavaScript community.

In this tutorial, you will install Yarn on Ubuntu 20.04. We will also cover the core Yarn commands and options.

Prerequisites

1) Make sure to log in as a user with the sudo privileges.

Install Yarn on Ubuntu 18.04

1) First, you need to enable the Yarn repository. Start by importing the repository’s GPG key using the below curl command:

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

2) Continue to adding the Yarn APT repository to the system’s software repository list. Do it by typing:

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

3) After there is an addition of repository to the system, update the package list and install Yarn, by:

sudo apt update
sudo apt install yarn

4) If you do not have Node.js installed on your system, the above command will install it. Those using nvm can skip the Node.js installation, by:

sudo apt install --no-install-recommends yarn

5) You will now need to verify Yarn's successful installation. Run the below command, it will print the Yarn version number:

yarn --version

6) Here, the latest version of Yarn is version 1.17.3:

Output

1.17.3

How to Use Yarn on Ubuntu

Creating a New Project

1) Next, you will create a new project. So, use the yarn init command as below:

yarn init my_yarn_project

2) Then, the init script will ask you few questions. You can either answer or press enter  key. It will enable you to use the 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.

3) The script will create a basic package.json file. It contains the information you earlier provided. You can open and modify the file later.

Adding Dependency

1) Now, in case you want to use another package in your project, then you will need to add it to project dependencies. For that, use yarn add command with the package name:

yarn add [package_name]

2) The command above will update the package.json and yarn.lock files as well. So anyone working on this project, while running yarn, will get the same dependencies. You will even specify the package version or package tag, by:

yarn add [package_name]@[version_or_tag]

Upgrading Dependency

1) To upgrade the dependency packages, use one of the below commands:

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

If no package name is given, the command will update all the project dependencies. It will give the latest version as per the version range given in the package.json file otherwise, only the specified packages will get updated.

Removing Dependency

1) Next, use yarn remove command along with the package name, it will remove a dependency:

yarn remove [package_name]

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

2) Next, to install all project dependencies given in the package.json file. Do it by running:

yarn

Or

yarn install

Upgrade Yarn Version

If you want to upgrade Yarn to the latest version, execute the following command:

curl --compressed -o- -L https://yarnpkg.com/install.sh | bash

The system will now download the Yarn tarball and install the latest version.

FAQs to Install Yarn on Ubuntu 22.04

What is Yarn Install v/s NPM install?

NPM and Yarn differ significantly in terms of security performance. Yarn is faster and more efficient, as it installs packages in parallel, whereas NPM does so sequentially.

Is Yarn secure?

Yes, Yarn is secure as it uses checksums to verify the integrity of packages downloaded from the registry.

Is Yarn free?

Yes, Yarn is free and open source.

Does Yarn have an offline mirroring feature?

Yes, Yarn has an offline mirroring feature, which allows packages to be installed without an internet connection.

How to install Yarn globally?How to install Yarn globally?

The NPM package manager, which comes installed by default with all Node.js installations, is advised by the Yarn maintainers for installing Yarn globally. To accomplish this, run npm install with the -g flag:

sudo npm install -g yarn

Conclusion

You now know how to install Yarn on an Ubuntu. Visit their documentation page for more details about Yarn.

If you have any queries, feel free to post a comment below, and we'll be happy to answer them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.