FoxuTech

Apt Command – Complete guide

apt command

apt is a command-line utility used for installing, updating, removing, and also managing deb packages on Ubuntu, Debian, and related Linux distributions. apt is designed for interactive use. Prefer using apt-get and apt-cache in your shell scripts or Dockerfile as they are backward compatible between the different versions and have more options and features. In this post will see about Apt commands.

Installing packages

Installing packages is as simple as running the following command:

# apt install package-name

You can install multiple packages with single command, you can specify like below

# apt install package1 package2

As we know, in Debian based system deb files are used to install the bundled packages, even you can install deb file using apt, to install local deb files you can provide the full path to file. Otherwise, the command will try to retrieve and install the package from the APT repositories.

# apt install /path/to/file.deb

Updating package index

The APT package index is basically a database that holds records of available packages from the repositories enabled in your system. To update the package index run the command below. This will pull the latest changes from the APT repositories:

# apt update

Always recommended to update the package index before upgrading or installing new packages.

Upgrading packages

Regularly updating your Linux system is one of the most important aspects of overall system security. To upgrade the installed packages to their latest versions run:

# apt upgrade

The command doesn’t upgrade packages that require removal of installed packages. If you want to upgrade a single package, you can mention the package name:

# apt upgrade package-name

It is always a good idea to configure automatic security updates.

Full Upgrading

The difference between upgrade and full-upgrade is that the later will remove the installed packages if that is needed to upgrade the whole system.

# apt full-upgrade

Always run this command with care, as this will remove all the old packages, where apt upgrade just install the package, it wont remove updated packages.

Dist Upgrading

The difference between upgrade and dist-upgrade in addition to performing the function of upgrade, also intelligently handles changing dependencies with new versions of packages; apt-get has a “smart” conflict resolution system, and it will attempt to upgrade the most important packages at the expense of less important ones if necessary

# apt dist-upgrade

Removing Packages

To remove/uninstall an installed package, you can use;

# apt remove package-name

You can also specify multiple packages, separated by spaces:

# apt remove package1 package2

The remove command will uninstall the given packages, but it may leave some configuration files behind. If you want to remove the package including all configuration files, use purge instead of remove:

# apt purge package-name

Remove Unused Packages

In linux based systems, there will be more number dependency package installed whenever we install any package with apt install. But when we remove it won’t remove the additional package installed while we remove/uninstall. So, the dependency packages will be stay on the VM unnecessarily and consumes the extra space.  

To remove the such a unnecessary dependencies, use the following command:

# apt autoremove
# apt --purge autoremove

Listing Packages

The list command allows you to list the available, installed and, upgradeable packages. To list all available packages, use the following command:

# apt list

The command will print a list of all packages, including information about the versions and architecture of the package. To find out whether a specific package is installed, you can filter the output with the grep command.

# apt list | grep package-name

To list only the installed packages type:

# apt list --installed

Getting a list of the upgradeable packages may be useful before actually upgrading the packages:

# apt list --upgradeable

Searching Packages

Sometime we may not able to available package on the package repository or some difficult to pull it, in that case, you can search option to pull all the available packages. This command allows you to search for a given package in the list of the available packages:

# apt search package-name

Package Information

The information about the package dependencies, installation size, the package source, and so on might be useful before removing or installing a new package. To retrieve information about a given package, use the show command:

# apt show package-name

Package dependency

The depends option shows a listing of each dependency a package has and all the possible other packages that can fulfil that dependency.

# apt depends package-name

For recursive dependency listing

# apt rdepends package-name

Package hold

Holding package is important for some of critical system or production environment, so if there is any automated package installation performed, it will skip some packages which marked as hold, this will prevent from unexpected upgrades, to hold,

# apt-mark hold package-name

Display a list of packages on hold

Run the following command to check is there any packages are on-hold, this may helpful when you are upgrading or if you notice the package not upgraded, even if you run upgrade command.

# apt-mark showhold

Package unhold

When you like the upgrade the holded package, you can unhold it and upgrade it. Please remember you should hold it back, if the environment is critical.

# apt-mark unhold package-name

Edit Source

You can edit the source information file /etc/apt/sources.list on this path or you can run edit sources to modify it. This may useful when you are troubleshooting package issue or adding new source to install like docker, kubeadm, etc.

# apt edit-sources

You can follow us on social media, to get some regular updates

Exit mobile version