Yarn is a fast, secure, and reliable package manager for Node.js that simplifies the installation, configuration, updating, and removal of npm packages. It offers parallel processing, caching, and integrity verification, making it an excellent choice for managing project dependencies. In this tutorial, we will guide you through the process of installing Yarn on Ubuntu 20.04 and introduce you to some essential commands for working with Yarn.
Prerequisites
Before we begin, please ensure that you have the following prerequisites:
- Superuser or root privileges on your Ubuntu 20.04 system.
Installation of Yarn on Ubuntu 20.04
To install Yarn on Ubuntu 20.04, follow these steps:
Step 1: Install Curl
The first step is to install the curl
command-line tool, which will help us import the Yarn repository’s GPG key. Open the terminal by pressing Ctrl+Alt+t
and enter the following command:
sudo apt install curl
Step 2: Import the GPG key
With curl
installed, we can now import the GPG key for the Yarn repository. Run the following command to import the key:
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
Step 3: Add and Enable the official Yarn repository
Next, we need to add and enable the official Yarn Apt repository on our Ubuntu 20.04 system. Execute the following command to add the repository:
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
Step 4: Update system cache
Before installing Yarn, we should update our system’s apt repository to ensure we have the latest package information. Run the following command to update the system cache:
sudo apt update
Step 5: Install Yarn
Finally, we can install Yarn on our Ubuntu 20.04 system. Execute the following command to start the installation:
sudo apt -y install yarn
If you have already installed Node.js using nvm, you can skip the installation of Node.js packages and use the following command to install Yarn:
sudo apt install --no-install-recommends yarn
To verify that Yarn has been successfully installed, use the following command:
yarn --version
Congratulations! You have successfully installed Yarn on your Ubuntu 20.04 system.
Uninstall Yarn from Ubuntu 20.04
If you ever need to remove Yarn from your Ubuntu 20.04 system, you can do so by executing the following command:
sudo apt remove yarn
Using Yarn on Ubuntu 20.04
Now that Yarn is installed on your Ubuntu 20.04 system, let’s explore some useful Yarn commands.
Create a new project and initialize Yarn
To create a new project directory and initialize Yarn, follow these steps:
- Create a new project directory by running the following command:
mkdir ~/yarn_project && cd ~/yarn_project
- Use the
yarn init
command to initialize a new project:
yarn init yarn_project
You will be prompted with a series of questions. Provide the required information or leave the defaults as they are. Once you have provided the information, a package.json
file will be created with the specified details.
Adding dependencies to a project
Yarn makes it easy to add dependencies to your project. Use the following command to add a new project dependency:
yarn add [package-name]
For example, to install the React package as a dependency, run the following command:
yarn add react
Yarn will install the latest version of the package by default. If you need to install a specific version, use the following syntax:
yarn add [package-name]@[package-version]
Upgrading project dependencies
To upgrade project dependencies, use the following commands:
yarn upgrade yarn upgrade [package-name] yarn upgrade [package-name]@[version]
If you don’t provide a package name, Yarn will automatically upgrade all project packages to the latest version based on the specified range in the package.json
file.
Removing packages or dependencies
To remove an installed project dependency, use the following command:
yarn remove [package-name]
For example, to remove the React package from your project, run the following command:
yarn remove react
This command will remove the specified package from your project and update the package.json
and yarn.lock
files accordingly.
Installing all project dependencies
To install all project dependencies mentioned in the package.json
file, use the following command:
yarn install
Alternatively, you can simply run the yarn
command to install all project dependencies.
Conclusion
In this tutorial, we have covered the installation process of Yarn on Ubuntu 20.04. We have also introduced you to some useful Yarn commands for managing project dependencies. Yarn is a powerful package manager that simplifies the management of npm packages and offers performance improvements and security features.
To learn more about Yarn and its capabilities, we recommend visiting the official Yarn documentation on the internet. If you have any further questions or need assistance, feel free to reach out to Shape.host, a leading provider of Linux SSD VPS services. Shape.host offers reliable and scalable cloud hosting solutions to empower businesses with efficient and secure hosting environments.