How to Install Yarn on Ubuntu 22.04 LTS

Yarn is a package manager that helps develop JavaScript code and shares it through software packages. A team at Facebook developed it as an alternative to the Node package manager (NPM) client and focused on speed, security, and consistency. Packages, also known as modules, are used to distribute the code, and a package.json file outlines the contents of the package and all associated distributed codes. Yarn can work offline and has a deterministic algorithm used to install packages, meaning that if two developers have the same dependencies, Yarn will generate the same file structure in both projects. This allows for a better understanding of projects and more reliable builds.

In addition, Yarn uses checksums to verify the integrity of every installed package before its code is executed. Yarn will attempt to fix mismatches automatically or report an error if mismatches are found. Using Yarn, developers can share JavaScript code more quickly and efficiently while ensuring that code is secure and consistent across projects.

In the following tutorial, you will learn various methods of installing Yard on Ubuntu 22.04 LTS Jammy Jellyfish desktop or server, along with some tips on using Yarn.

Update Ubuntu

First, ensure your system is up-to-date by running the following command before proceeding to avoid conflicts.

sudo apt update && sudo apt upgrade

Install Required Packages

Please run the following command to install the required packages for users that plan to install the NVM or NodeSource instead of the Ubuntu 22.04 default repository.

sudo apt install software-properties-common apt-transport-https wget ca-certificates gnupg2 gcc make g++ curl -y

Import NodeSource Repository

For Yarn installations, you will need to install Node.js along with it, as it is a package manager. First, import the NodeSource repository that suits your needs in either the current release or LTS version to install both packages.

Node.js Current

Import the Node.js current repository.

curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -

Node.js LTS

Import the Node.js LTS repository.

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo bash -

Install Yarn Package Manager

Now that you have installed Node.js, you can install the Yarn package manager. First, import the GPG key.

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null

Next, import the repository.

echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

Now refresh your apt sources list.

sudo apt update

Lastly, install Yarn and Node.js with the following command.

sudo apt-get install yarn nodejs

Optionally, you can check the version of Yarn to confirm the installation.

yarn -v

Alternatively, you can use the apt-cache policy command.

apt-cache policy yarn

Example output:

Yarn Commands

With the package manager now installed, many commands are associated with it. However, the tutorial will quickly list the most common so you can get started.

See also
How to Upgrade to Ubuntu 23.04 Lunar Lobster

Start a new project using the following command.

yarn init

To install all dependencies for the Yarn project, use one of the following two commands.

yarn

or

yarn install

To add a single dependency to a project, use the following syntax, replacing the package with the actual package, version with the dependency version, and tag with the appropriate tag for the dependency.

yarn add [package]
yarn add [package]@[version]
yarn add [package]@[tag]

To add dependencies to a specific category, we can use one of the following three options:

  • dev Dependencies.
  • peer Dependencies. 
  • optional Dependencies.

The syntax for adding these command dependencies is as follows.

yarn add [package] --dev
yarn add [package] --peer
yarn add [package] --optional

If you want to upgrade an existing dependency, use the yarn upgrade command.

yarn upgrade [packagename]
yarn upgrade [packagename]@[version]
yarn upgrade [packagename]@[tag]

The Yarn remove command is used to remove an existing dependency.

yarn remove [package]

To upgrade Yarn to the latest version, run one of the following commands.

yarn set version latest
yarn set version from sources

Lastly, list the options available with Yarn with the help flag.

yarn --help

Example output:


  Usage: yarn [command] [flags]

  Displays help information.

  Options:

    --cache-folder <path>               specify a custom folder that must be used to store the yarn cache
    --check-files                       install will verify file tree of packages for consistency
    --cwd <cwd>                         working directory to use (default: /home/joshua)
    --disable-pnp                       disable the Plug'n'Play installation
    --emoji [bool]                      enable emoji in output (default: false)
    --enable-pnp, --pnp                 enable the Plug'n'Play installation
    --flat                              only allow one version of a package
    --focus                             Focus on a single workspace by installing remote copies of its sibling workspaces.
    --force                             install and build packages even if they were built before, overwrite lockfile
    --frozen-lockfile                   don't generate a lockfile and fail if an update is needed
    --global-folder <path>              specify a custom folder to store global packages
    --har                               save HAR output of network traffic
    --https-proxy <host>                
    --ignore-engines                    ignore engines check
    --ignore-optional                   ignore optional dependencies
    --ignore-platform                   ignore platform checks
    --ignore-scripts                    don't run lifecycle scripts
    --json                              format Yarn log messages as lines of JSON (see jsonlines.org)
    --link-duplicates                   create hardlinks to the repeated modules in node_modules
    --link-folder <path>                specify a custom folder to store global links
    --modules-folder <path>             rather than installing modules into the node_modules folder relative to the cwd, output them here
    --mutex <type>[:specifier]          use a mutex to ensure only one yarn instance is executing
    --network-concurrency <number>      maximum number of concurrent network requests
    --network-timeout <milliseconds>    TCP timeout for network requests
    --no-bin-links                      don't generate bin links when setting up packages
    --no-default-rc                     prevent Yarn from automatically detecting yarnrc and npmrc files
    --no-lockfile                       don't read or generate a lockfile
    --non-interactive                   do not show interactive prompts
    --no-node-version-check             do not warn when using a potentially unsupported Node version
    --no-progress                       disable progress bar
    --offline                           trigger an error if any required dependencies are not available in local cache
    --otp <otpcode>                     one-time password for two factor authentication
    --prefer-offline                    use network only if dependencies are not available in local cache
    --preferred-cache-folder <path>     specify a custom folder to store the yarn cache if possible
    --prod, --production [prod]         
    --proxy <host>                      
    --pure-lockfile                     don't generate a lockfile
    --registry <url>                    override configuration registry
    -s, --silent                        skip Yarn console logs, other types of logs (script output) will be printed
    --scripts-prepend-node-path [bool]  prepend the node executable dir to the PATH in scripts
    --skip-integrity-check              run install without checking if node_modules is installed
    --strict-semver                     
    --update-checksums                  update package checksums from current repository
    --use-yarnrc <path>                 specifies a yarnrc file that Yarn should use (.yarnrc only, not .npmrc) (default: )
    -v, --version                       output the version number
    --verbose                           output verbose messages on internal operations
    -h, --help                          output usage information
  Commands:
    - access
    - add
    - audit
    - autoclean
    - bin
    - cache
    - check
    - config
    - create
    - exec
    - generate-lock-entry / generateLockEntry
    - global
    - help
    - import
    - info
    - init
    - install
    - licenses
    - link
    - list
    - login
    - logout
    - node
    - outdated
    - owner
    - pack
    - policies
    - publish
    - remove
    - run
    - tag
    - team
    - unlink
    - unplug
    - upgrade
    - upgrade-interactive / upgradeInteractive
    - version
    - versions
    - why
    - workspace
    - workspaces

  Run `yarn help COMMAND` for more information on specific commands.
  Visit https://yarnpkg.com/en/docs/cli/ to learn more about Yarn.

Comments and Conclusion

The yarn package manager is a reliable, consistent, and faster alternative to the Node package manager. It can work offline and has a deterministic algorithm that guarantees the same file structure for every developer working on a project with the same dependencies. In addition, it uses checksums to verify the integrity of every installed package before its code is executed.

See also
How to Install QOwnNotes on Ubuntu 22.04 or 20.04

The Yarn package manager should become the new standard for JavaScript development with these features.

Leave a Comment

Index
Share to...