Table of Contents
In this article, I will take you through the steps to install Yarn on Ubuntu 20.04 LTS (Focal Fossa). Yarn is a free and open source package manager just like any other package manager but since it manages packages for projects so it is more like a Project Manager with great set of features inbuilt. It gives guarantee that an install work today will continue to work the same way in the future.
This gives a lot of comfort to programmers and developers in managing their projects efficiently for a longer period of time. This package manager is available for almost all the Linux versions. Here we will see the steps to install Yarn on Ubuntu 20.04 LTS (Focal Fossa) based systems in great detail. More on official website.
How to Install Yarn on Ubuntu 20.04 LTS (Focal Fossa)
Also Read: How to Install pycoin(Bitcoin Client) on Linux (RHEL/Ubuntu/Rocky Linux)
Step 1: Prerequisites
a) You should have a running Ubuntu 20.04 LTS
Server.
b) You should have sudo
or root
access to run privileged commands.
c) You should have apt
and curl
utility available in your System.
Step 2: Update Your Server
In the first step, you need to update the System cache with all the latest available package information from all the enabled repositories by using apt update
command as shown below. If needed, then you can also run apt upgrade
command to upgrade all the packages.
root@cyberithub:~# apt update
Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Hit:4 https://dl.google.com/linux/chrome/deb stable InRelease
Get:5 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1,793 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [652 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu focal-updates/main Translation-en [330 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [278 kB]
Get:10 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 c-n-f Metadata [15.2 kB]
Get:11 http://in.archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [976 kB]
Get:12 http://in.archive.ubuntu.com/ubuntu focal-updates/restricted i386 Packages [24.3 kB]
................................................
Step 3: Install npm
If you are looking to install the Yarn package using npm then first you need to install the npm from default Ubuntu repo by using apt install npm
command as shown below.
root@cyberithub:~# apt install npm
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libfwupdplugin1 libllvm11 linux-headers-5.8.0-41-generic
linux-hwe-5.8-headers-5.8.0-41 linux-image-5.8.0-41-generic
linux-modules-5.8.0-41-generic linux-modules-extra-5.8.0-41-generic
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
gyp javascript-common libc-ares2 libjs-inherits libjs-is-typedarray
libjs-psl libjs-typedarray-to-buffer libnode-dev libnode64 libpython2-stdlib
libpython2.7-minimal libpython2.7-stdlib libssl-dev libssl1.1 libuv1-dev
node-abbrev node-ajv node-ansi node-ansi-align node-ansi-regex
node-ansi-styles node-ansistyles node-aproba node-archy
node-are-we-there-yet node-asap node-asn1 node-assert-plus node-asynckit
.............................................
Step 4: Install Yarn
There are two ways to install Yarn on Ubuntu 20.04 LTS system i.e either through npm
utility or through apt
package manager. We will see both the methods below.
a) Using npm
If you are looking to install yarn using npm
utility then you can use npm install --global yarn
command to install the package globally in your System as shown below.
root@cyberithub:~# npm install --global yarn
> yarn@1.22.18 preinstall /usr/local/lib/node_modules/yarn
> :; (node ./preinstall.js > /dev/null 2>&1 || true)
/usr/local/bin/yarn -> /usr/local/lib/node_modules/yarn/bin/yarn.js
/usr/local/bin/yarnpkg -> /usr/local/lib/node_modules/yarn/bin/yarn.js
+ yarn@1.22.18
added 1 package in 4.978s
b) Using Repo
If you are looking to install Yarn through repo then first you need to add the GPG Key using below curl
command.
root@cyberithub:~# curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - OK
Then add the repo by using below command.
root@cyberithub:~# echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list deb https://dl.yarnpkg.com/debian/ stable main
Finally update the package cache and then download and install the Yarn package from the newly added repo by using apt update && apt install yarn
command as shown below.
root@cyberithub:~# apt update && apt install yarn Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease Get:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB] Get:3 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB] Get:4 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB] Get:5 https://dl.yarnpkg.com/debian stable InRelease [17.1 kB] Hit:6 https://dl.google.com/linux/chrome/deb stable InRelease Get:7 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1,790 kB] Get:8 http://in.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [650 kB] Get:9 https://dl.yarnpkg.com/debian stable/main i386 Packages [10.9 kB] Get:10 http://in.archive.ubuntu.com/ubuntu focal-updates/main Translation-en [330 kB] ......................................... ......................................... The following NEW packages will be installed: yarn 0 upgraded, 1 newly installed, 0 to remove and 52 not upgraded. Need to get 893 kB of archives. After this operation, 5,415 kB of additional disk space will be used. Get:1 https://dl.yarnpkg.com/debian stable/main amd64 yarn all 1.22.19-1 [893 kB] Fetched 893 kB in 1s (1,319 kB/s) Selecting previously unselected package yarn. (Reading database ... 233202 files and directories currently installed.) Preparing to unpack .../yarn_1.22.19-1_all.deb ... Unpacking yarn (1.22.19-1) ... Setting up yarn (1.22.19-1) ...
Step 5: Check Version
To verify the installation, you can check the installed yarn utility version by using yarn --version
command as shown below.
root@cyberithub:~# yarn --version
1.22.19
Step 6: Create a Project
To start a project, you need to first initialize it by using yarn init <project_name>
. Here we are creating test_project
by using yarn init test_project
command as shown below. If this is the first time test_project
is getting initialized then it will create a new package.json
file with all the configuration. If this project already exists, then below command will just update the package.json
file.
root@cyberithub:~# yarn init test_project
yarn init v1.22.19
question name (root):
question version (1.0.0):
question description: Creating a Project
question entry point (index.js):
question repository url:
question author:
question license (MIT):
question private:
success Saved package.json
Done in 51.81s.
Step 7: Adding Dependencies
If you want to install a package of specific version along with all its dependencies then you need to use yarn add <package_name>@<tag_name>
syntax. Here we are installing express package version 4.15.5
using yarn add express@4.15.5
command as shown below.
root@cyberithub:~# yarn add express@4.15.5
yarn add v1.22.19
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 32 new dependencies.
info Direct dependencies
└─ express@4.15.5
info All dependencies
├─ accepts@1.3.8
├─ array-flatten@1.1.1
├─ content-disposition@0.5.2
├─ content-type@1.0.4
├─ cookie-signature@1.0.6
├─ cookie@0.3.1
├─ depd@1.1.2
├─ destroy@1.0.4
├─ ee-first@1.1.1
├─ etag@1.8.1
├─ express@4.15.5
├─ finalhandler@1.0.6
├─ forwarded@0.1.2
├─ http-errors@1.6.3
├─ inherits@2.0.3
├─ ipaddr.js@1.4.0
├─ media-typer@0.3.0
├─ merge-descriptors@1.0.1
├─ methods@1.1.2
├─ mime-db@1.52.0
├─ mime-types@2.1.35
├─ mime@1.3.4
├─ negotiator@0.6.3
├─ path-to-regexp@0.1.7
├─ proxy-addr@1.1.5
├─ qs@6.5.0
├─ serve-static@1.12.6
├─ setprototypeof@1.0.3
├─ type-is@1.6.18
├─ unpipe@1.0.0
├─ utils-merge@1.0.0
└─ vary@1.1.2
Done in 3.11s.
Step 8: Upgrade a Package
If you are looking to upgrade any package to the latest available version then you need to use yarn upgrade <package_name>
syntax. Here we are upgrading express package to the latest version by using yarn upgrade express
command as shown below.
root@cyberithub:~# yarn upgrade express
yarn upgrade v1.22.19
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Rebuilding all packages...
success Saved lockfile.
success Saved 32 new dependencies.
info Direct dependencies
└─ express@4.15.5
info All dependencies
├─ accepts@1.3.8
├─ array-flatten@1.1.1
├─ content-disposition@0.5.2
├─ content-type@1.0.4
├─ cookie-signature@1.0.6
├─ cookie@0.3.1
├─ depd@1.1.2
├─ destroy@1.0.4
├─ ee-first@1.1.1
├─ etag@1.8.1
├─ express@4.15.5
├─ finalhandler@1.0.6
├─ forwarded@0.1.2
├─ http-errors@1.6.3
├─ inherits@2.0.3
├─ ipaddr.js@1.4.0
├─ media-typer@0.3.0
├─ merge-descriptors@1.0.1
├─ methods@1.1.2
├─ mime-db@1.52.0
├─ mime-types@2.1.35
├─ mime@1.3.4
├─ negotiator@0.6.3
├─ path-to-regexp@0.1.7
├─ proxy-addr@1.1.5
├─ qs@6.5.0
├─ serve-static@1.12.6
├─ setprototypeof@1.0.3
├─ type-is@1.6.18
├─ unpipe@1.0.0
├─ utils-merge@1.0.0
└─ vary@1.1.2
Done in 1.53s.
Step 9: Remove a Package
If you want remove some package from your current project then you need to use yarn remove <package_name>
syntax. Here we are removing express package using yarn remove express
command as shown below.
root@cyberithub:~# yarn remove express
yarn remove v1.22.19
[1/2] Removing module express...
[2/2] Regenerating lockfile and installing missing dependencies...
success Uninstalled packages.
Done in 0.31s.
Step 10: Uninstall Yarn
Once you are done with Yarn, you can choose to uninstall the package by using any of the below method depending on how you had installed it.
a) Using npm
If you had installed the package using npm then to uninstall you need to run npm remove --global yarn
command as shown below.
root@cyberithub:~# npm remove --global yarn
removed 1 package in 0.055s
b) Using apt
If you had installed the package from repository then to uninstall you can use apt remove yarn
command as shown below.
root@cyberithub:~# apt remove yarn Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: libfwupdplugin1 libllvm11 linux-headers-5.8.0-41-generic linux-hwe-5.8-headers-5.8.0-41 linux-image-5.8.0-41-generic linux-modules-5.8.0-41-generic linux-modules-extra-5.8.0-41-generic Use 'apt autoremove' to remove them. The following packages will be REMOVED: yarn 0 upgraded, 0 newly installed, 1 to remove and 52 not upgraded. After this operation, 5,415 kB disk space will be freed. Do you want to continue? [Y/n] Y (Reading database ... 233221 files and directories currently installed.) Removing yarn (1.22.19-1) ...