How to Install Yarn on Ubuntu 22.04 / Ubuntu 20.04

0

Yarn is a package manager for Node.js that allows developers to manage Javascript application dependencies. It is an alternative to the NPM (Node Package Manager) installed during the Node.js installation.

With Yarn, you can install packages, start a new project, and share packages (code) quickly, securely, and reliably.

Here, we will see how to install Yarn on Ubuntu 22.04 / Ubuntu 20.04.

Install Node.js and npm on Ubuntu 22.04/20.04

To install Yarn on Ubuntu, you need npm (Node Package Manager). So, install npm, which comes along with Node.js, using the steps mentioned in the below link.

I recommend installing the Node.js v16.x and npm v8.5.0 that come with Node.js.

READ: How to install Node.js on Ubuntu 22.04

READ: How to install Node.js on Ubuntu 20.04

Check the Node.js version node -v and npm version npm -v post the installation.

Install Yarn on Ubuntu 22.04/20.04

Global Install

Starting from Yarn v2, you can install the Yarn globally and on a by-project basis. But, first, you will need to install the global Yarn binary that you will use to spawn to a specific project.

sudo npm install -g yarn

Once you have installed Yarn global binary, check the Yarn version with the yarn -v command.

1.22.18

The above output confirms that your system has the expected global version installed.

Per-project install

Now, head over to your existing project directory.

cd your_project

Then, install the latest Yarn binary for your project.

yarn set version berry

Now, check the Yarn version with the yarn -v command.

3.2.0

The above output confirms that your project has the latest version of Yarn.

Using Yarn

Yarn is now ready for your projects, and you can start using it. Here are some of the common commands you may need for your project work.

Getting Help

If you are stuck with Yarn, the help command will guide you with available commands, or visit the Yarn documentation for more help.

yarn help

Create a New Yarn Project

To start a new project, use the yarn init command. This command will ask you a series of questions related to your project. You can either answer a question or press enter to use the default values.

mkdir my_project && cd my_project

yarn init

The yarn init command will place package.json, which contains the project information you provided, and yarn.lock, which contains all the information about project dependencies.

Add Project’s Dependencies

To add a dependency for your project, use the yarn add command followed by the package name. This command will download and install the dependency packages and updates the dependency details in package.json and yarn.lock.

yarn add [package]

yarn add [package]@[version]

yarn add [package]@[tag]

Install all Project’s Dependencies using Yarn

To install all the dependencies of your projects, run the below commands.

yarn

yarn install

Upgrade Project’s Dependencies using yarn

To upgrade an existing dependency in your project, use the yarn up command followed by the package name.

yarn up [package]

yarn up [package]@[version]

yarn up [package]@[tag]

Remove Project’s Dependencies

If you want to remove a dependency you no longer need in the project, use the yarn remove command.

yarn remove [package]

Update Yarn

You can update Yarn to the latest version with the below command in your project directory. This command will download the most recent binary from Yarn’s official site and install it in your project.

yarn set version latest

Conclusion

That’s All. I hope you have learned how to install Yarn on Ubuntu 22.04 / Ubuntu 20.04.

You might also like