Tutorials

Install Yarn on Ubuntu: Setup and Basic Commands

Yarn is a JavaScript package manager that works with npm. It makes it easy to install, update, and remove npm packages in your projects. Yarn also caches downloaded packages, so repeat installs are much faster. This guide shows you how to install Yarn on Ubuntu and covers the basic commands you will use every day.

How to Install Yarn on Ubuntu

The best way to get Yarn is from the official Yarn repository. This gives you the latest version and keeps it up to date.

First, add the Yarn GPG key and repository to your system:

bashcurl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

Then update your package list and install Yarn:

bashsudo apt updatesudo apt install yarn

This will also install Node.js. If you already installed Node.js through nvm, use this command instead to skip the Node.js install:

bashsudo apt install --no-install-recommends yarn

Check that Yarn installed correctly by running:

bashyarn --version

You should see a version number like 1.22.4. Yarn is ready to use.

Create a New Yarn Project

To start a new project, create a folder and go into it:

bashmkdir ~/my_project && cd ~/my_project

Run yarn init to set up the project:

bashyarn init my_project

Yarn will ask you a few questions about the project, like the name, version, and author. Press Enter to accept the defaults if you are not sure. When done, it creates a package.json file in your folder.

How to Manage Packages with Yarn

Once your project is set up, you can start adding and managing packages.

Adding a package:

bashyarn add [package_name]

This installs the latest version of the package. To install a specific version, add @ and the version number:

bashyarn add [package_name]@[version_or_tag]

Upgrading packages:

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

Running yarn upgrade with no package name updates all packages. Add a name to update just one.

Removing a package:

bashyarn remove [package_name]

This removes the package and updates both package.json and yarn.lock.

Installing all project packages:

If you clone a project or need to install all listed packages, just run:

bashyarn

Or:

bashyarn install

Yarn reads the package.json file and installs everything listed there.

Tip: Always commit your yarn.lock file to version control. It locks exact package versions for your team, so everyone installs the same thing.

A Quick Look at Yarn vs npm

Yarn and npm do the same job, but Yarn has a few advantages:

  • Faster installs because it runs operations at the same time
  • The yarn.lock file makes sure package versions stay the same across machines
  • Cleaner output that is easier to read

If your project already uses npm, you can switch to Yarn at any time. It reads package.json the same way.

Yarn is a solid choice for managing JavaScript packages on Ubuntu. The install takes just a couple of minutes, and the commands are easy to pick up. Give it a try in your next project and see if it speeds up your workflow. Any questions? Leave a comment below.

Cyber Defence

Recent Posts

Install Docker Compose on Ubuntu: Step-by-Step Setup Guide

Docker Compose is a command-line tool that lets you define and run multi-container Docker applications using a single…

20 minutes ago

Install VirtualBox on Ubuntu from Ubuntu Repositories

The simplest approach is Ubuntu's multiverse repository. A single command installs both VirtualBox and the Extension…

24 minutes ago

Install Vagrant on Ubuntu: Setup and Getting Started Guide

If your team needs identical development environments across different operating systems, Vagrant is the tool that makes…

34 minutes ago

Install GCC on Ubuntu: build-essential and Multiple Versions

GCC; the GNU Compiler Collection is the backbone of open-source software development on Linux. It supports…

47 minutes ago

Install Redis on Ubuntu: Configuration and Remote Access

Redis is an open-source, in-memory key-value store built for raw speed and versatility. It works equally well as…

22 hours ago

Install Skype on Ubuntu: Two Methods That Actually Work

Skype doesn't ship with Ubuntu by default it's a proprietary application owned by Microsoft and…

22 hours ago