Apt and Apt-Get Tutorial With Examples


Distributions like Ubuntu, Debian use APT as a default online package manager. APT works with dpkg together where apt resolves dependencies and download packages from internet, network share, etc. and dpkg installs, configure them. In this tutorial, we will look at how to use apt efficiently APT packages extensions are .deb which is actually dpkg packages. There is a GUI application manager synaptic which have base features of the APT. Here is a simple package name the information provided by the package name.

Package Information

yakkety 4.9.0-2 amd64
  • Yaketty is the packages original name which is given by the upstream provider. Some times package names can be changed and different from the upstream provider. But this situation is very rare.
  • 4.9.0 is a version that is provided by the upstream developer.
  • -2 is version added by package maintainer in the Ubuntu repository. Some times distribution-related updates may be needed differently from the original program.
  • amd64 is system architecture for this package which means it supports 64 bit.

Update Packages

Updating packages are done with two stages. First, we will update repository information. A repository is a place where packages are held in the HTTP or FTP server. The repository provides packages and metadata or information about packages. This information will get and then the required packages will updates.

$ sudo apt-get update
Update Packages
Update Packages

As we can see from the screenshot we get information about packages from tr.archive.ubuntu.com and updated our local information base. Now we will upgrade our packages.

$ sudo apt-get upgrade
Upgrade Packages
Upgrade Packages

We can use the following command to make upgrade job more automatic

$ sudo apt-get dist-upgrade

With this command upgrade, the related question will be automatically answered. To make things simple we can combine these two commands into a single line like below.

$ sudo apt-get update && sudo apt-get dist-upgrade

Upgrade Packages

While upgrading we can see questions like Do you want to continue? [Y/n] Y We can automatically answer all questions with yes like below.

$ sudo apt-get dist-upgrade -y

Search Packages

Searching packages are easy. We will just provide the term or package name we want to search

$ apt-cache search tmux
Search Packages
Search Packages

We can see that there are some packages in the list but there is no string in the line like tmux. Searching is done in the package details too where we look next. We can search a term that is like google free-text search. We get more results.

$ apt-cache search tmu
Search Packages
Search Packages

Package Information

We may want to get package details information about installed-size, architecture, dependencies, version, file name, description, maintainer,  etc.

$ apt-cache show tmux
Package Info
Package Info

Install Packages

Installing packages is easy as other operations we just provide our package name to the apt-get install command like below.

$ sudo apt-get install tmux
Install Packages
Install Packages

We can provide -y option to prevent question and directly install package. The position of the -y option is not important.

$ sudo apt-get install tmux -y

To select more than one package with globing asterisk * can be used. This will select all package those starts with xfce4-

$ sudo apt-get install xfce4-*
Install Packages
Install Packages

We can also install multiple packages in the same command by separating them spaces.

$ sudo apt-get install tmux apache2

Remove Packages

Removing packages is similar to installing them. We provide the name of the package to the apt-get remove command.

$ sudo apt-get remove tmux
Remove Packages
Remove Packages
Remove Packages
Remove Packages

Remove by providing yes option with -y .

$ sudo apt-get remove tmux -y
Remove Packages
Remove Packages

Remove Package With Configuration

Normally removing the package will uninstall the package binary files and documentation. Configuration files are do not removed for future installation. If we want to remove configuration files too we need to provide –purge option.

$ sudo apt-get --purge remove nginx
Remove Package With Configuration
Remove Package With Configuration

Remove Clean Cache

A cache is intermediate storage used APT where downloaded packages are held. After some time with new versions of packages, we can clean this cache like below.

$ sudo apt autoremove
Remove Clean Cache
Remove Clean Cache

Check Dependencies

Dependency is the requirement to install other related packages to install a package. For example, to install Gnome Desktop, Gnome Terminal must be installed and here Gnome Terminal is a dependency for Gnome Desktop. To get dependencies showpkg sub-command will be used.

$ apt-cache showpkg tmux
Check Dependencies
Check Dependencies

Here we check dependencies of the tmux package. We can see that there is two part of dependencies.

  • Dependencies part provides required packages to install tmux.
  • Reverse Dependencies provides which packages need the tmux package.
LEARN MORE  How To Apt-Get Update, Upgrade, Dist-Upgrade, Full-Upgrade, and Their Similarities and Differences?

Get Statistics Of Cache

A cache is used to store information about packages. There is a command named stat which is used to get overall statistics information.

$ apt-cache stats
Get Statistics Of Cache
Get Statistics Of Cache

Stats sub-commands provides information like;

  • Total package names 
  • Virtual packages count
  • Missing package count
  • Space used for cache

Update System Packages

Installed packages can be updated easily. /etc/apt/sources.list provides information about repositories that provides updates for packages. Update operation will get the most recent information about packages from provided repositories and saved to the cache.

$ sudo apt-get update
Update System Packages
Update System Packages

Upgrade Specific Package

Upgrade operation is generally used in a complete mode where all packages are upgraded.  But there are some times where only some of the packages should be updated like below.

$ sudo apt-get upgrade tmux
Upgrade Specific Package
Upgrade Specific Package

Install Multiple Packages

Install multiple packages can be done with simple separating package names with spaces like below. For example to install tmux and ksh in a single shot apt command use the following example.

$ sudo apt-get install tmux ksh
Install Multiple Packages
Install Multiple Packages

Install Using Wildcards

Another way to install multiple packages is by using a wildcard. The wildcard is an asterisk * which means that anything goes well for the asterisk. To install gnome packages like gnome-specimen, gnome-twich, gnome-weather, … simply packages starting with gnome- we will use gnome-* like below.

$ sudo apt-get install gnome-*
Install Using Wildcards
Install Using Wildcards

We can see those selected packages with selecting lines.

Install Without Upgrading

Installing a package will upgrade if the package is already installed. To prevent this situation –no-upgrade option can be provided.

$ sudo apt-get install tmux --no-upgrade
Install Without Upgrading
Install Without Upgrading

Install Specific Version Of Package

Repositories provide different versions of packages. Some times using only a specific version of package works. In this situation, it can be provided to install only a specific version like below.

$ sudo apt-get install vsftpd=2.3.5-3ubuntu1

Remove Package Without Configuration

Normally purging packages will remove the packages configuration files too. To prevent configuration removal use remove parameter like below.

$ sudo apt-get remove tmux
Remove Package Without Configuration
Remove Package Without Configuration

Completely Remove Packages

Package configuration can be removed if it is not needed or corrupted.  Purge is the command used to clear the configuration of packages.

$ sudo apt-get purge tmux
Completely Remove Packages
Completely Remove Packages

OR alternatively, the same operation can be done with remove command by providing –purge option

$ sudo apt-get remove --purge tmux

Clean Apt Cache

The apt command downloads package information and packages into the cache. This cache gets bigger and bigger over time. This local cache or repository must be cleaned regularly with the command below.

$ sudo apt-get clean

Download Only Source Code

As we know packages are compiled from source code and provided as binary. Repositories also provide these source code if the source code option is enabled in the /etc/apt/sources.listconfiguration. The source code can be downloaded as below. Source code in repositories can be enabled by uncommenting deb-src lines and the final look will be like below.

Enable deb source
Enable deb source

Then run apt-get update to get the latest information about source packages.

$ sudo apt-get --download-only source tmux
Download Only Source Code
Download Only Source Code

Download And Unpack Package

To make things easier source packages can be downloaded and unpacked automatically like below.

$ sudo apt-get source tmux
Download And Unpack Package
Download And Unpack Package

Download, Unpack and Compile Package

One step further of downloading and extracting the package is compiling the package. All these operations can be done automatically with the command below.

$ sudo apt-get --compile source tmux
Download, Unpack and Compile Package
Download, Unpack and Compile Package

Only Download Package

There are some offline hosts and want to get a package for them to install. We will just download the package without installing it.

$ apt-get download tmux
Download Only Package
Download Only Package

Get Change Log Of Package

Package maintainers provide changelog of new versions there are different ways to get this change log like from the upstream site, maintainers site or simply using changelog package.

$ sudo apt-get changelog tmux
Get Change Log Of Package
Get Change Log Of Package

Check Broken Dependencies

Some times it is required to check broken package dependencies.  There is a command named check to accomplish this task.

$ sudo apt-get check
Check Broken Dependencies
Check Broken Dependencies

Auto Clean

To clear and remove all .deb files from /var/cache/apt/archives use following command. This will give useful disk space for other operations.

$ sudo apt-get autoclean
Auto Clean
Auto Clean

Auto Remove

To remove a package with their dependencies those will not be needed in the future autoremove can be used. This will only remove dependencies which are not used by other packages.

$ sudo apt-get autoremove tmux
Auto Remove
Auto Remove

4 thoughts on “Apt and Apt-Get Tutorial With Examples”

  1. Hi,

    Really very good summary about apt. I have doubt with “Install Without Upgrading ”

    Why this option is required, as if a package is already downloaded, and we are not upgrading too so why we will reinstall can you give an example for this case.

    Reply

Leave a Comment