Skip to content
Laviyx Learn

Try

Linux and Bash course menu

Linux apt Tutorial: Install, Update and Remove Packages

Install software on Ubuntu and Debian with apt, update and upgrade safely, search for packages, remove them, and see how dnf, snap and other package managers compare.

Intermediate4 min readUpdated
On this page

On Linux you rarely download installers from websites. A package manager installs software from trusted repositories, along with everything it depends on, and keeps it up to date.

On Ubuntu and Debian, the tool is apt.

Update the list, then upgrade#

These two commands are often confused:

Terminal
sudo apt update      # refresh the list of available versions
sudo apt upgrade     # install newer versions of what you have

apt update installs nothing. It only downloads the latest list, so always run it first.

Install a package#

Here we install cowsay, a small fun program. -y answers yes to the question, which is useful in scripts:

Terminal
sudo apt-get install -y -qq cowsay > /dev/null 2>&1
/usr/games/cowsay "Installed with apt" 2>&1 | head -n 3
Output
 ____________________
< Installed with apt >
 --------------------

Games like this one install to /usr/games. apt is designed for people, and apt-get is the older form, with stable output for scripts. They do the same job.

Search and inspect#

Terminal
apt-cache search '^nginx$' | cut -c1-60
apt-cache policy cowsay | head -n 3
dpkg -l cowsay | tail -n 1 | awk '{print $1, $2, $3}'
Output
nginx - small, powerful, scalable web/proxy server
cowsay:
  Installed: 3.03+dfsg2-8
  Candidate: 3.03+dfsg2-8
ii cowsay 3.03+dfsg2-8

Which files did it install?#

dpkg is the lower-level tool beneath apt:

Terminal
dpkg -L cowsay | grep bin/
dpkg -S /usr/bin/grep
Output
grep: /usr/bin/grep

-L lists a package's files. -S finds which package owns a file.

Remove a package#

Terminal
sudo apt-get remove -y -qq cowsay > /dev/null 2>&1
dpkg -l cowsay 2>&1 | tail -n 1 | cut -c1-30
Output
dpkg-query: no packages found
CommandEffect
sudo apt remove nameRemove the program, keep its settings
sudo apt purge nameAlso remove its settings
sudo apt autoremoveRemove dependencies nothing needs now

Other sources#

If a package is out of date in the official repositories, projects often offer their own repository. You add its signing key and a source file to /etc/apt/sources.list.d/. Only do this for sources you trust, because packages run as root when they install.

A .deb file downloaded by hand installs like this:

Terminal
sudo apt install ./package.deb

Other package managers#

SystemToolInstall example
Ubuntu, Debianaptsudo apt install nginx
Fedora, RHELdnfsudo dnf install nginx
Archpacmansudo pacman -S nginx
Alpineapkapk add nginx
Any, sandboxedsnap, flatpaksudo snap install code --classic
macOSbrewbrew install nginx

Common mistakes#

Practice#

Practice exercise

Find which package owns /bin/ls, and print how many packages are installed.

Show hint

/bin/ls is really /usr/bin/ls on Ubuntu. dpkg -l marks installed packages with ii.

Show solution
Terminal
dpkg -S /usr/bin/ls
dpkg -l | grep -c '^ii' | awk '{print ($1 > 100) ? "More than 100 packages" : "Fewer than 100"}'
Output
coreutils: /usr/bin/ls
More than 100 packages

Key takeaway#

Next: Disk Space.

Finished this lesson?