How To

Install Yarn on Ubuntu 18.04: APT Setup and Essential Commands

Yarn is a fast, reliable JavaScript package manager that works alongside npm. It was built to fix some of npm’s early limitations — particularly slow install speeds and inconsistent behavior across different machines and networks. Yarn parallelizes package downloads instead of installing them one at a time, which makes large dependency trees install significantly faster.

Two features that make Yarn especially useful for development teams are the yarn.lock file and deterministic installs. The lock file pins every package to an exact version, so everyone working on the same project gets identical dependencies regardless of when or where they run yarn install. This eliminates the “works on my machine” problem that comes from subtle version differences. The yarn.lock file should always be committed to version control.

This guide covers how to install Yarn on Ubuntu 18.04 using the official APT repository and walks through the most common Yarn commands you will use day to day.

<strong>Prerequisite:</strong>&nbsp;You need sudo access.

Install Yarn on Ubuntu via APT Repository

The official Yarn APT repository is consistently maintained and provides the latest stable release. New versions are added as soon as they are published, so you will always be on the current version after a standard system upgrade.

Import the Yarn repository’s GPG key:

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

Add the Yarn repository to your system’s source list:

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

Update the package index and install Yarn:

bashsudo apt updatesudo apt install yarn

If Node.js is not already on your system, this command installs it automatically as a dependency. If you use nvm to manage your Node.js versions, skip the bundled Node.js install with the --no-install-recommends flag instead:

bashsudo apt install --no-install-recommends yarn

Verify the installation:

bashyarn --version

Output:

1.17.3

Common Yarn Commands

Here are the Yarn commands you will use most often when working on JavaScript projects.

Create a new project

Use yarn init to scaffold a new project. Yarn asks for the project name, version, description, entry point, repository URL, author, and license. Press Enter to accept any default value. When finished, it generates a package.json file:

bashyarn init my_yarn_project

Add a dependency

To add a package to your project, use yarn add followed by the package name. Yarn installs the package, updates package.json, and records the exact version in yarn.lock:

bashyarn add [package_name]

To install a specific version or tag:

bashyarn add [package_name]@[version_or_tag]

Upgrade a dependency

To upgrade all packages to the latest versions allowed by your package.json:

bashyarn upgrade

To upgrade a specific package only:

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

Remove a dependency

To uninstall a package and update both package.json and yarn.lock:

bashyarn remove [package_name]

Install all project dependencies

When you clone a project or pull new changes, run this to install everything listed in package.json:

bashyarn

Or equivalently:

bashyarn install

Yarn is now installed and ready to use on your Ubuntu 18.04 system. The commands above cover the most common dependency management operations you will need for any JavaScript project. Leave a comment below if you run into any issues during setup.

Cyber Defence

Recent Posts

Install Atom on Ubuntu 18.04: GitHub’s Code Editor APT Setup

Atom is a free, open-source, cross-platform code editor developed by GitHub. Built on Electron, it uses…

1 hour ago

Install MariaDB on Ubuntu 18.04: Two Methods and Secure Setup

MariaDB is an open-source, multi-threaded relational database management system and a fully backward-compatible replacement for MySQL.…

13 hours ago

Install Odoo 11 on Ubuntu 18.04: Virtualenv and Nginx Proxy Setup

Odoo 11 is an open-source all-in-one business software platform covering CRM, e-commerce, billing, accounting, manufacturing,…

13 hours ago

Install Odoo on Ubuntu 18.04: Deploy Version 12 with Nginx

Odoo is the most widely used open-source all-in-one business software suite. It integrates CRM, e-commerce, billing,…

13 hours ago

Install phpMyAdmin with Nginx on Ubuntu 18.04: Setup and Config

phpMyAdmin is a free, open-source PHP application for managing MySQL and MariaDB servers through a browser.…

13 hours ago

Install phpMyAdmin on Ubuntu 18.04 with Apache: Setup Guide

phpMyAdmin is a free, open-source PHP application that provides a browser-based interface for managing MySQL and…

3 days ago