Installing Tools: Homebrew and apt

A package manager is an app store you type to; on Ubuntu it's apt, which needs sudo to install, and on a Mac it's Homebrew's brew, which doesn't, and both search, install, update and remove tools by name.

Apprentice · Unix · Linux · Updated

After this lesson you can

  • Find a tool's package and check whether it's installed with apt search and apt-cache policy, or brew search and brew info
  • Install, update and remove tools with sudo apt or with brew, and tell apt update from apt upgrade

Key takeaways

  • A package manager installs programs by name from a catalog its makers check. Ubuntu and WSL use apt; a Mac uses Homebrew, typed brew.
  • apt search and apt-cache policy look things up and change nothing. brew search and brew info do the same on a Mac.
  • sudo apt install name installs a tool, and sudo apt remove name removes it. On a Mac it's brew install and brew uninstall, never with sudo.
  • apt update only refreshes the catalog. apt upgrade installs the newer versions. Run them in that order; brew update and brew upgrade work the same way.

A tutorial says "run tree to see the project's folders", and your terminal says this:

tree
Command 'tree' not found, but can be installed with:
sudo snap install tree  # version 2.1.3+pkg-5852, or
sudo apt  install tree  # version 2.3.1-1
See 'snap info tree' for additional versions.

The program isn't on your computer yet. You could search the web for it, find a download page, and hope it's the real one. Or you can ask your computer's package manager: an app store you type to. Developers get nearly every tool this way, and IT administrators set up whole servers with it, one line per program.

An app store you type to

A phone's app store has a catalog, installs an app by name, keeps every app up to date, and removes one cleanly. A package manager does the same for the terminal. Each program comes as a package, and the catalog lists thousands of them, checked by the people who look after the store.

  • Ubuntu, and Ubuntu in WSL on Windows, use apt. In WSL, apt installs into Ubuntu, not into Windows.
  • A Mac uses Homebrew, typed brew, which Setup: The Terminal on a Mac installed.

The tree message above offers a second store, snap, which Ubuntu also has. This course uses apt.

Look before you install

Searching changes nothing, so it needs no password. apt search looks through the catalog's names and descriptions. A short word like jq matches hundreds of descriptions, so --names-only with '^jq (a name that is exactly jq, as in Patterns: Regular Expressions for grep) finds the one:

apt search --names-only '^jq



jq/resolute-updates,resolute-security 1.8.1-4ubuntu2 amd64
  lightweight and flexible command-line JSON processor

The package's name, where it comes from (resolute is Ubuntu 26.04's code name; yours shows your version's), its version, and a line saying what it does. apt-cache policy says whether a package is installed, and which version you'd get:

apt-cache policy tree
tree:
  Installed: (none)
  Candidate: 2.3.1-1
  Version table:
     2.3.1-1 500
        500 http://archive.ubuntu.com/ubuntu resolute/universe amd64 Packages

Installed: (none) means not yet, and Candidate is the version apt install would bring. For something you already have, both lines show a version:

apt-cache policy bash | head -n 3
bash:
  Installed: 5.3-2ubuntu1
  Candidate: 5.3-2ubuntu1

On a Mac, brew search tree and brew info tree do the same jobs.

Install, update, remove

Installing changes the whole computer, not only your files, so apt needs the administrator's permission: sudo (sudo and Who Owns a File), and your password. These lines change your computer, so read them before you run them:

sudo apt update
sudo apt install tree

apt update refreshes apt's catalog, so it knows the newest versions; it installs nothing. apt install tree downloads tree and sets it up, and if apt asks whether to continue, press Y, then Enter. Now tree draws the folder you're in as a tree, with lines joining each folder to what's inside it.

To bring everything you've installed up to date, refresh the catalog, then upgrade. To take a tool away again, remove it:

sudo apt update
sudo apt upgrade
sudo apt remove tree
Job Ubuntu (apt) A Mac (Homebrew)
Find it apt search name brew search name
Is it installed? apt-cache policy name brew info name
Install it sudo apt install name brew install name
Refresh the catalog sudo apt update brew update
Install newer versions sudo apt upgrade brew upgrade
Remove it sudo apt remove name brew uninstall name

Never put sudo in front of brew. Homebrew installs into a folder your own account owns, so it needs no administrator, and it's built to refuse to run as one.

Four tools worth having

  • tree draws a folder and everything inside it as a tree.
  • htop shows what your computer is busy with, live, like a task manager: press q to quit.
  • jq reads and reshapes JSON, the text format most websites' data travels in.
  • tldr gives short, everyday examples for a command, where man gives the whole manual: tldr tar shows a handful of the ways people use tar most often. Its package is called tealdeer. The first time, run tldr --update so it downloads its pages.

One line installs all four, on Ubuntu or on a Mac:

sudo apt install tree htop jq tealdeer
brew install tree htop jq tealdeer

Run only the line for your computer. Then check one with tree --version: it prints its version, and command not found is gone.

Check yourself

2 questions to try on the lesson page.

So what

When a tutorial needs a tool you don't have, you're one line away: search for it, check it, install it, and keep everything up to date with two commands.

Key terms

Cheat sheets

Related