# 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](https://www.ionos.ca/digitalguide/websites/web-development/introduction-to-nodejs/) 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](https://www.ionos.ca/digitalguide/websites/web-development/install-nodejs-ubuntu-2204/) 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](https://www.ionos.ca/digitalguide/server/know-how/ubuntu-the-linux-system-for-everyone/) as your operating system. You’ll also need the **npm package manager** and an account with [sudo privileges](https://www.ionos.ca/digitalguide/server/configuration/linux-sudo-command/). If you’re not sure whether you have Node.js on your system, you can check with following command:

```bash
$ node -v
```

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

### 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:

```bash
$ sudo npm install -g yarn
```

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

```bash
$ yarn --version
```

### Install 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:

```bash
cd ~/your-project
```

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

```bash
mkdir your-project
cd your-project
```

Use the command `yarn set` to retrieve Yarn Berry:

```bash
$ yarn set version berry
```

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:

```bash
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!
```

Check the version again with the following command:

```bash
$ yarn --version
```

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

```bash
3.0.0
```

## 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:

```bash
yarn init
```

### Saving and creating dependencies

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

```bash
yarn install
```

Use the `add` command to create new dependencies. Replace the placeholder “packagename” with the actual name of the package:

```bash
yarn add packagename
```

### 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:

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

### Help and further information

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

```bash
$ yarn --help
```

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

```bash
$ yarn install --help
```

## 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 optimized 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.


This is a markdown version of: [https://www.ionos.ca/digitalguide/websites/web-development/yarn-installation/](https://www.ionos.ca/digitalguide/websites/web-development/yarn-installation/) for AI/LLM consumption.