Cybersecurity Updates & Tools

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.