How to Install Yarn on Ubuntu 20.04
3 min read
In this tutorial, we will explain how to install Yarn on Ubuntu 20.04. We will also go through the basic Yarn commands and options.
Installing Yarn on Ubuntu
Installing Yarn on Ubuntu is fairly straightforward. We’ll enable the official Yarn repository, import the repository GPG key, and install the package. The repository is consistently maintained and provides the most up-to-date version.
Import the repository’s GPG key and add the Yarn APT repository to your system by running the following commands:
curl -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
Once the repository is enabled, update the package list, and install Yarn.
sudo apt update
sudo apt install yarn
The command above will also install Node.js . If you installed Node trough nvm, skip the Node.js installation with:
sudo apt install --no-install-recommends yarn
Once completed, verify the installation by printing the Yarn version:
The output will look something like this:
The version installed on your system may differ from the one shown above.
That’s it! You have successfully installed Yarn on your Ubuntu machine, and you can start using it.
Now that Yarn has been installed on your Ubuntu system, let’s explore some of the most common Yarn commands.
Creating a new project
Start by creating a directory for your application and navigate into it:
mkdir ~/my_project && cd ~/my_project
To create a new project, run
yarn init my_project
The command will ask you several questions. Enter information as prompted, or accept the defaults:
yarn init v1.22.4 question name (vagrant): Linuxize question version (1.0.0): 0.0.1 question description: Testing Yarn question entry point (index.js): question repository url: question author: Linuxize question license (MIT): question private: success Saved package.json Done in 20.18s.
Once completed, the script creates a basic
package.json file containing the provided information. You can open and edit this file at any time.
To add an npm package to the project dependencies, use the
yarn add command followed by the package name:
yarn add [package_name]
The command above will update the
By default, when only the package name is given, Yarn installs the latest version. To install a specific version or tag, use the following syntax:
yarn add [package_name]@[version_or_tag]
To upgrade the packages, use one of the following commands:
yarn upgrade [package_name]
yarn upgrade [package_name]@[version_or_tag]
If no package name is given, the command will update the project dependencies to their latest version according to the version range specified in the package.json file. Otherwise, only the specified packages are updated.
yarn remove command followed by the package name to remove a dependency:
yarn remove [package_name]
The command will remove the package and update the project’s
Installing all project dependencies
To install all project dependencies that are specified in the
package.json file, run:
We have shown you how to install Yarn on your Ubuntu machine. For more information about Yarn visit their documentation page.
If you have any questions or feedback, feel free to comment below.