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.
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.
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.
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.lockfile to version control. It locks exact package versions for your team, so everyone installs the same thing.
Yarn and npm do the same job, but Yarn has a few advantages:
yarn.lock file makes sure package versions stay the same across machinesIf 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.
Docker Compose is a command-line tool that lets you define and run multi-container Docker applications using a single…
The simplest approach is Ubuntu's multiverse repository. A single command installs both VirtualBox and the Extension…
If your team needs identical development environments across different operating systems, Vagrant is the tool that makes…
GCC; the GNU Compiler Collection is the backbone of open-source software development on Linux. It supports…
Redis is an open-source, in-memory key-value store built for raw speed and versatility. It works equally well as…
Skype doesn't ship with Ubuntu by default it's a proprietary application owned by Microsoft and…