Tutorials

Install Yarn on Ubuntu: Setup and Basic Commands

Yarn is a JavaScript package manager that works with npm. It makes it easy to install, update, and remove npm packages in your projects. Yarn also caches downloaded packages, so repeat installs are much faster. This guide shows you how to install Yarn on Ubuntu and covers the basic commands you will use every day.

How to Install Yarn on Ubuntu

The best way to get Yarn is from the official Yarn repository. This gives you the latest version and keeps it up to date.

First, add the Yarn GPG key and repository to your system:

bashcurl -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

Then update your package list and install Yarn:

bashsudo apt updatesudo apt install yarn

This will also install Node.js. If you already installed Node.js through nvm, use this command instead to skip the Node.js install:

bashsudo apt install --no-install-recommends yarn

Check that Yarn installed correctly by running:

bashyarn --version

You should see a version number like 1.22.4. Yarn is ready to use.

Create a New Yarn Project

To start a new project, create a folder and go into it:

bashmkdir ~/my_project && cd ~/my_project

Run yarn init to set up the project:

bashyarn init my_project

Yarn will ask you a few questions about the project, like the name, version, and author. Press Enter to accept the defaults if you are not sure. When done, it creates a package.json file in your folder.

How to Manage Packages with Yarn

Once your project is set up, you can start adding and managing packages.

Adding a package:

bashyarn add [package_name]

This installs the latest version of the package. To install a specific version, add @ and the version number:

bashyarn add [package_name]@[version_or_tag]

Upgrading packages:

bashyarn upgradeyarn upgrade [package_name]yarn upgrade [package_name]@[version_or_tag]

Running yarn upgrade with no package name updates all packages. Add a name to update just one.

Removing a package:

bashyarn remove [package_name]

This removes the package and updates both package.json and yarn.lock.

Installing all project packages:

If you clone a project or need to install all listed packages, just run:

bashyarn

Or:

bashyarn install

Yarn reads the package.json file and installs everything listed there.

Tip: Always commit your yarn.lock file to version control. It locks exact package versions for your team, so everyone installs the same thing.

A Quick Look at Yarn vs npm

Yarn and npm do the same job, but Yarn has a few advantages:

  • Faster installs because it runs operations at the same time
  • The yarn.lock file makes sure package versions stay the same across machines
  • Cleaner output that is easier to read

If your project already uses npm, you can switch to Yarn at any time. It reads package.json the same way.

Yarn is a solid choice for managing JavaScript packages on Ubuntu. The install takes just a couple of minutes, and the commands are easy to pick up. Give it a try in your next project and see if it speeds up your workflow. Any questions? Leave a comment below.

Cyber Defence

Recent Posts

rsync Command in Linux: Sync Files, Mirror, and Transfer Remotely

The rsync command in Linux synchronizes files and directories between two locations, locally or over SSH. Unlike scp,…

18 hours ago

patch Command in Linux: Apply Diff Files and Reverse Changes

The patch command in Linux applies a set of changes from a diff file to one or…

18 hours ago

basename Command in Linux: Strip Directory Paths and Suffixes

The basename command in Linux extracts the last component of a file path, removing the leading directory…

18 hours ago

timeout Command in Linux: Kill Long-Running Commands Safely

The timeout command in Linux runs a command with a time limit and terminates it when the limit is reached. It is part of GNU coreutils, available on virtually every Linux distribution. It is most useful for commands with no built-in timeout option, such as ping, curl, tcpdump, or a custom script that might hang indefinitely. How the timeout Command Works in Linux The syntax is: bashtimeout [OPTIONS] DURATION COMMAND [ARG]...…

18 hours ago

rmmod Command in Linux: Remove Kernel Modules and Blacklisting

The rmmod command in Linux removes a loaded module from the running kernel. Because the kernel has…

2 days ago

free Command in Linux: Check Memory Usage and Interpret Output

The free command in Linux gives you a quick summary of RAM and swap usage. It reads…

2 days ago