In this tutorial, we will explain how to install Yarn on Ubuntu 20.04. We will also go through basic Yarn orders and options.
Installing Yarn on Ubuntu
Installing Yarn on Ubuntu is quite easy. We will activate the official Thread repository, import the GPG key repository, and install packages. The repository is managed consistently and provides the most up-to-date version.
Import the GPG repository key and add the APT Thread repository to your system by running the following command:
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
After the repository is activated, update the package list, and install Yarn.
sudo apt update
sudo apt install yarn
The above command will also install Node.js. If you installed Node via nvm, skip installing Node.js with:
sudo apt install --no-install-recommends yarn
When finished, verify installation by printing a Yarn version:
The output will look like this:
The version installed on your system may differ from the one shown above.
There she is! You have successfully installed Thread 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.
Create a new project
Start by creating a directory for your application and navigate to it:
mkdir ~/my_project && cd ~/my_project
To create a new project, run the Yarn init:
yarn init my_project
The command will ask several questions. Enter information as requested, or accept the default:
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.
After completion, the script creates a basic package.json file that contains the information provided. You can open and edit this file at any time.
To add the npm package to the project dependencies, use the yarn add command followed by the package name:
yarn add [package_name]
The above command will update the package.json and yarn.lock files.
By default, when only package names are given, Thread installs the latest version. To install a specific version or tag, use the following syntax:
yarn add [package_name]@[version_or_tag]
To update a package, 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.
Use the Yarn remove command command followed by the package name to remove the 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.