How to install Yarn

To install Yarn, you first have to set up the full version and then a single project version. Then you can configure the package manager based on your wishes and needs.

How to install Yarn step by step

The JavaScript package manager Yarn was developed for Node.js and has quickly become one of the most popular solutions in its segment since its release in 2016. Since setting it up isn’t always easy, we’ll give you a step-by-step introduction. You should already have Node.js installed and set up.

Tip

Find out how to install Node.js on Ubuntu in our article.

What are the requirements for installing Yarn?

There are only a few requirements for installing Yarn. In addition to Node.js, you’ll also need a Linux distribution like Ubuntu as your operating system. You’ll also need the npm package manager and an account with sudo privileges. If you’re not sure whether you have Node.js on your system, you can check with following command:

$ node -v
bash

That will show you which version you have on your system. If you receive a confirmation, you can go ahead and install Yarn.

Compute Engine
The ideal IaaS for your workload
  • Cost-effective vCPUs and powerful dedicated cores
  • Flexibility with no minimum contract
  • 24/7 expert support included

Downloading and installing Yarn

To install Yarn, you’ll actually need to set up two versions of the package manager. First, download the full version of the program. That way you can ensure that the same version is being used by all your team members and at each stage of the project. That in turn will reduce errors. Use the npm package manager to install the full version. The command for that will look as follows:

$ sudo npm install -g yarn
bash

Then query the version number to ensure that you received the latest packages. The command for that is:

$ yarn --version
bash

Installing Yarn locally

Now you can set up Yarn for a specific JavaScript project. First, call up the project’s directory. Replace the placeholder ‘your-project’ with the name of your project. Here is the command:

cd ~/your-project
bash

If you don’t have a folder for your project, create one using mkdir:

mkdir your-project
cd your-project
bash

Use the command yarn set to retrieve Yarn Berry:

$ yarn set version berry
bash

If it’s available, the latest version of Yarn Berry will be downloaded. Save it in a project folder named .yarn/releases and create a configuration file named .yarnrc.yml. The output should look roughly as follows:

Resolving berry to a url...
Downloading https://github.com/yarnpkg/berry/raw/master/packages/berry-cli/bin/berry.js...
Saving it into /home/user/your-project/.yarn/releases/yarn-berry.cjs...
Updating /home/user/your-project /.yarnrc.yml...
Done!
bash

Check the version again with the following command:

$ yarn --version
bash

If Yarn was properly installed, you’ll get the following output (with a varying version number depending on the current version):

3.0.0
bash

What are the most important Yarn commands?

Once you’ve installed Yarn, you can start using it. It’s worth taking a look at some basic commands, so that you can get the most out of the package manager. Below we’ll introduce some of the most important commands.

Starting a new project

To start a new project, use the command init. It will create a new project and all the files you need for the project:

yarn init
bash

Saving and creating dependencies

If you already have a project and want to store dependencies in it, use the command yarn install:

yarn install
bash

Use the add command to create new dependencies. Replace the placeholder ‘packagename’ with the actual name of the package:

yarn add packagename
bash

The best configuration for .gitignore

All files are saved in the .yarn folder in your project, but you can leave out some files. To do that, use the following configuration for your .gitignore file:

.yarn/*
!.yarn/patches
!.yarn/releases
!.carn/plugins
!.yarn/sdks
!.yarn/versions
.pnp.*
bash

Help and further information

If you need help or further information after installing Yarn, use --help:

$ yarn --help
bash

If you need help with a specific command, combine the command with --help. With yarn install, for example, that would look as follows:

$ yarn install --help
bash

What are some alternatives to Yarn?

Yarn is a great solution if you want to share and use code securely. But there are also a number of good alternatives that you can use instead of Yarn.

npm

Unsurprisingly, npm (Node Project Manager) is a good option for working with the runtime environment. It’s easy to use, fast, secure and a perfect fit for Node.js. It offers a registry of over 1.3 million packages and the CLI interface. We have also used npm in this Yarn install guide.

pnpm

pnpm is an open-source package manager that’s perfectly suited to working with JavaScript. Its structure is similar to npm, but it uses Symlinks and doesn’t do multiple local installations of identical packages. It was developed as a more efficient alternative to npm, which makes it interesting as a replacement for Yarn.

Bower

Our third alternative is Bower, a free package manager for client-side web development that was optimised for working in the frontend. It offers a large selection of packages. In addition to JavaScript, the manager also works with other components like HTML and CSS. It’s very easy to use and works using the Node.js command line.

Was this article helpful?
Page top