How to Install Yarn on Ubuntu 22.04 / Ubuntu 20.04
With Yarn, you can install packages, start a new project, and share packages (code) quickly, securely, and reliably.
Install Node.js and npm on Ubuntu 22.04/20.04
I recommend installing the Node.js v16.x and npm v8.5.0 that come with Node.js.
Check the Node.js version
node -v and npm version
npm -v post the installation.
Install Yarn on Ubuntu 22.04/20.04
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.
The above output confirms that your system has the expected global version installed.
Now, head over to your existing project directory.
Then, install the latest Yarn binary for your project.
yarn set version berry
Now, check the Yarn version with the
yarn -v command.
The above output confirms that your project has the latest version of 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.
If you are stuck with Yarn, the help command will guide you with available commands, or visit the Yarn documentation for more 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
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]
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