This article explains how you can use apt-get and apt-cache commands from the command line to install, remove, update and search software packages. This article provides some useful commands to help you manage package management in systems based on Debian / Ubuntu.

APT-GET COMMANDS

What is apt-get?

The apt-get utility is a powerful and free command line package management program used to work with Ubuntu’s APT (Advanced Packaging Tool) library to install new software packages, remove existing software packages, upgrade existing software packages, and even upgrade the entire operating system.

1. How to Update System Packages

The ‘ update ‘ command is used to re-synchronize package index files from the sources specified in /etc / apt / sources.list file. The update command retrieved the packages from their locations and updated the packages to a newer version.

sudo apt-get update

2. How to Upgrade Software Packages

The ‘upgrade’ command is used to upgrade all the software packages currently installed on the system. Under any circumstances, packages currently installed are not removed or packages that are not already installed are not retrieved and installed to satisfy dependencies for upgrades.

sudo apt-get upgrade

If you want to upgrade, unworried about adding or removing software packages to fulfill dependencies, use the sub-command ‘dist-upgrade’.

sudo apt-get dist-upgrade

3. How to Install or Upgrade Specific Packages

The sub-command ‘install’ is tracked by one or more installation or upgrade desired packages.

sudo apt-get install netcat

4. How to Install Multiple Packages

With the command, you can add more than one package name to install multiple packages simultaneously. The following command, for example, installs ‘nethogs’ and ‘goaccess’ packages.

sudo apt-get install nethogs goaccess

5. How to Install Several Packages using Wildcard

You can add multiple packages with one string with the help of regular expression. For instance, we use * wildcard to install multiple packages containing the string ‘*name *’, ‘naming’ , ‘package name’. Replace the name with package name.

sudo apt-get install '*name*'

6. How to install Packages without Upgrading

Using the command sub ‘–no-upgrade’ will prevent the upgrade of already installed packages.

sudo apt-get install packageName --no-upgrade

7. How to Upgrade Only Specific Packages

The command ‘–only-upgrade’ does not install new packages, but only upgrades packages already installed and disables new packages installation.

sudo apt-get install packageName --only-upgrade

Replace ‘packagename’ with the package name.

8. How to Install Specific Package Version

Let’s take if you only want to install specific package versions, just use the ‘ = ‘ with the package name and add the version you want.

sudo apt-get install vsftpd=2.3.5-3ubuntu1

9. How to Remove Packages Without removing the Configuration

Uninstall software packages without removing their configuration files (the same configuration will be re-used later). Use the command ‘ remove ‘ as shown.

sudo apt-get remove vsftpd

10. How to Completely Remove Packages

Use the ‘purge’ sub-command as shown below to remove software packages including their configuration files.

sudo apt-get purge vsftpd

Alternatively, as shown below, you can combine the commands together.

sudo apt-get remove --purge vsftpd

11. How to Clean Up Disk Space

By cleaning retrieved (downloaded) files (packages) from the local repository, the ‘clean’ command is used to free up disk space.

sudo apt-get clean

12. How to Download Only Source Code of Package

Use the option ‘–download-only source’ with ‘package-name’ as shown to download only source code for a particular package.

sudo apt-get --download-only source vsftpd

13. How to Download and Unpack a Package

Type the following command to download and unpack a package’s source code to a specific directory.

sudo apt-get source vsftpd

14. How to Download, Unpack and Compile a Package

You can also simultaneously download, unpack and compile the source code, using the ‘ –compile ‘ option as shown below.

sudo apt-get --compile source goaccess

15. How to Download a Package Without Installing

You can download any package without installing it using the ‘download’ option. The following command, for example, will only download the package ‘nethogs’ to the current working directory.

sudo apt-get download nethogs

16. How to Check Change Log of Package

The ‘changelog’ command downloads change-log of a package and shows the installed package version.

sudo apt-get changelog vsftpd

17. How to Check Broken Dependencies

The command ‘check’ is a tool of diagnosis. Used to update the cache of packages and check for broken dependencies.

sudo apt-get check

18. How to Search and Build Dependencies

This command ‘build-dep’ searches the system’s local repositories and installs the package build dependencies. If there is no package in the local repository, an error code will be returned.

sudo apt-get build-dep netcat

19. How to auto-clean apt-get Cache

The command ‘autoclean’ removes all.deb files from /var / cache / apt / archives to free up substantial disk space volume.

sudo apt-get autoclean

20. How to auto-remove Installed Packages

The sub-command ‘autoremove’ is used to auto-remove packages that were installed to satisfy dependencies for other packages and that were no longer required now. The following command, for example, will remove a package with its dependencies installed.

sudo apt-get autoremove vsftpd

APT-CACHE COMMANDS

What is apt-cache?

The command line tool apt-cache is used to search the apt package cache software. Simply put, this tool is used to search for software packages, collect package information and also search for available packages that are ready to be installed on Debian or Ubuntu-based systems.

21. How to list all packages that are available

To list all the available packages, do use the following command.

apt-cache pkgnames

22. How to Find Package Name and Description of Software

Use the ‘ search ‘ flag to find out the package name and description before installation. Using apt-cache’s “search” will display a short description list of matched packages. Let’s say you’d like to find a description of the ‘ vsftpd ‘ package, then it would be a command.

apt-cache search vsftpd

You can use the following command to find and list all the packages starting with ‘vsftpd’.

apt-cache pkgnames vsftpd

23. How to check information about the package

For instance, if you want to check package information along with it, say short description (version number, size, check sums, category, installed size, etc). Use the sub-command ‘ show ‘ as shown below.

apt-cache show netcat

24. How to Check statistics of Cache

The sub-command ‘ states ‘ will display overall cache statistics. The following command displays Total package names, for example, is the number of packages found in the cache.

apt-cache stats

I covered maximum commands available with apt-get and apt-cache commands, but there are still more options available, you can check them out from the terminal using ‘man apt-get’ or ‘man apt-cache’. I hope you’ve enjoyed reading this article, if you want me to add something it to the list. Please mention it in the comment section below.