Why should I regularly run 'apt-get update' and similar commands? [duplicate]

On all currently supported versions of Ubuntu, we can use apt instead of apt-get. If you want to capture the output in a file, use apt-get, but otherwise use apt as its output is more readable (and who likes typing?).

sudo apt update

Downloads information from the repositories 1 APT is configured to check, and updates the dpkg database of available, installed, and uninstalled packages to reflect changes in the repositories. This means that when you query the database on your system with commands like

apt policy package-name
apt show package-name
apt search package-name

they will print accurate information, and when you run commands like

sudo apt install package-name

they will fetch the latest available version.

When you run sudo apt update you may see that some packages are upgradable, meaning new versions have been added to the repositories. Newer versions often have bug fixes and security updates, so you should install them. This can be done by upgrading all packages that have new versions available with the command

sudo apt upgrade

If the kernel was upgraded (packages like linux-image-4.15.0-20-generic) you should then run a command to remove old kernel versions (except for the second-newest which is kept as a spare, in case the new one is buggy). You can use this command:

sudo apt autoremove

to do that. This command also removes "orphaned packages". These generally only exist when you use a command like

sudo apt remove package-name

and package-name has dependencies that were installed along with it automatically, but are not needed by any other package.

For autoclean, I refer you to man apt-get

autoclean (and the auto-clean alias since 1.1)
       Like clean, autoclean clears out the local repository of retrieved
       package files. The difference is that it only removes package files
       that can no longer be downloaded, and are largely useless.

The other three commands are really important and should generally be run regularly to keep the system secure and updated and avoid filling the disk with old kernel files, but you probably won't see much difference after running autoclean. The few files it might delete occasionally are unlikely to amount to much.


1These repositories are configured by default, so you usually don't need to do anything with them. They are listed in the file /etc/apt/sources.list and in files in /etc/apt/sources.list.d. APT is strict about the format of these files (for security reasons) and will throw errors if they have bad syntax. If your system has a graphical environment, it will have an option to configure repositories in settings. You need root privilege to do that job.


While Zanna’s answer covers the purpose of these commands, I’d like to focus on the second part:

Is it really useful to run them regularly?

While it’s good to run them often enough, I do not see reasons to run them literally regularly (on the real time basis, e.g. once a day, once a week, once a month).

  • The update and clean tasks fulfilled by these commands are not created on the time basis. They begin to be useful when a new version of installed packages can be found in the repositories.

  • The apt-get update command is run by the system regularly by default (see Automatic Package Updates, AutomaticSecurityUpdates). The security updates which would be installed by apt-get upgrade, will be automatically installed by Unattended Upgrades, too.

  • The automatic update applies to all packages. Once APT knows about the updates which are not installed automatically, the system will remind you to install them manually (using apt install <package> or apt upgrade to upgrade all of them), for example:

    • on the CLI login screen:

      Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.4.113-rt128+ x86_64)
      ...
      29 packages can be updated.
      6 updates are security updates.
      
    • in the output of an APT command:

      $ sudo apt update
      ...
      5 packages can be upgraded. Run 'apt list --upgradable' to see them.
      
  • You’ll be reminded to run autoremove as well.

    $ sudo apt update
    ...
    The following packages were automatically installed and are no longer required:
      devilspie libturbojpeg:i386 libxrandr-ltsr2 language-pack-kde-en
    Use 'apt-get autoremove' to remove them.
    

If you still want to run the commands on the time basis, you can create a cron task for them.