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 Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

1 day ago

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

1 day ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

1 day ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

1 day ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

1 day ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

2 days ago