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.
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:
sudo apt update # refresh the list of available versions
sudo apt upgrade # install newer versions of what you haveapt 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:
sudo apt-get install -y -qq cowsay > /dev/null 2>&1
/usr/games/cowsay "Installed with apt" 2>&1 | head -n 3 ____________________
< 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#
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}'nginx - small, powerful, scalable web/proxy server
cowsay:
Installed: 3.03+dfsg2-8
Candidate: 3.03+dfsg2-8
ii cowsay 3.03+dfsg2-8Which files did it install?#
dpkg is the lower-level tool beneath apt:
dpkg -L cowsay | grep bin/
dpkg -S /usr/bin/grepgrep: /usr/bin/grep-L lists a package's files. -S finds which package owns a file.
Remove a package#
sudo apt-get remove -y -qq cowsay > /dev/null 2>&1
dpkg -l cowsay 2>&1 | tail -n 1 | cut -c1-30dpkg-query: no packages foundOther 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:
sudo apt install ./package.debOther package managers#
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
dpkg -S /usr/bin/ls
dpkg -l | grep -c '^ii' | awk '{print ($1 > 100) ? "More than 100 packages" : "Fewer than 100"}'coreutils: /usr/bin/ls
More than 100 packagesKey takeaway#
Next: Disk Space.
Finished this lesson?