Yarn is a fast, secure, and reliable dependency manager designed to resolve some of the shortcomings exhibited by npm.
The application relies on npm registry modules and is not a replacement for the npm. It caches all the packages it downloads and parallelizes operations to boost resource utilization to expedite install times.
This tutorial will show you how to install and use Yarn on Ubuntu 18.04 VPS or Dedicated server through the APT package manager. This repository is updated consistently and avails the most recent Yarn’s version.
Before You Begin
Step 1 - Installing Yarn
The first thing when installing Yarn on Ubuntu 18.04 is
$ sudo curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - $ sudo echo"deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
$ sudo apt update $ sudo apt install yarn
The command above will automatically install Yarn if you have not installed Node.js on your server.
However, if you are using the Node.js Version Manager,
$ sudo apt install--no-install-recommends yarn
$ sudo yarn --version
This will give you an output similar to the one below:
Step 2 - Using Yarn
By now you have deployed Yarn on your system, next you need to explore the most commonly used commands for Yarn:
To generate a Yarn project execute the command below:
$ sudo yarn init my_yarn_project
The above init command will prompt you to answer several questions. Answer the questions accordingly or hit ENTER to skip them.
yarninitv1.10.1 questionname (vagrant): Linuxize questionversion (1.0.0): 0.0.1 questiondescription: TestingYarn questionentrypoint (index.js): questionrepositoryurl: questionauthor: Linuxize questionlicense (MIT): questionprivate: successSavedpackage.json Donein 20.18s.
Once you answer all the question, Yarn will generate a new package.json file featuring the details you provided. The file is readily available and can be opened and edited at any time.
The yarn add is a command used to add a new package to dependencies of a project. The command used to add the package takes the form:
$ sudo yarn add [package_name]
This command will update the yarn.lock and package.json files. Updating these files enables everyone working on the project to get similar dependencies when using yarn.
Besides, you can use the yarn add command and specify the version or tag for the package you want to add:
$ sudo yarn add [package_name]@[version_or_tag]
Upgrading The Dependencies
If you want to use the latest version of a specific package, you can execute the command below:
$ sudo yarn upgrade [package_name]
Alternatively, if you want to upgrade the dependency to a specific version or tag, issue the command below:
$ sudo yarn upgrade [package_name]@[version_or_tag]
Removing A Dependency
If you want to expunge a certain dependency, execute the command below:
$ sudo yarn remove [package_name]
Installing All Dependencies
If you want to install the project dependencies specified in a package.json you can execute one of the commands below:
$ sudo yarn
$ sudo yarn install
That is It! You have installed Yarn on Ubuntu 18.04 VPS or Dedicated server and learned how to implement some of the most commonly used Yar commands. For in-depth details regarding Yarn usage, go to the official Yarn documentation website page.