Apt and Apt-Get Tutorial With Examples

Distributions like Ubuntu, Debian uses APT as 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 how to use apt efficiently APT packages extensions are .deb which is actually dpkg packages. There is GUI application manager synaptic which have base features of the APT. Here is simple package name the information provided by package name.

1

yakkety4.9.0-2amd64

Yaketty is the packages original name which is given by upstream provider. Some times package names can be change and different from upstream provider. But this situation is very rare.

4.9.0 is version which is provided by the upstream developer.

-2 is version added by package maintaner in Ubuntu repository. Some times distribution realted updates may needed different from 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. Repository is place where packages are hold in the http or ftp server. Repository provides packages and metadata or information about packages. This information will get and then required packages will updates.

1

$sudo apt-get update

As we can see from screen shot we get information about packages from tr.archive.ubuntu.com and updated our local information base.

Now we will upgrade our packages.

1

$sudo apt-get upgrade

We can use following command to make upgrade job more automatic

1

$sudo apt-get dist-upgrade

With this command upgrade related question will be automatically answered.

To make things simple we can combine these two command in to single line like below.

1

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

Yes We Can

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.

1

$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

1

$apt-cache search tmux

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 as term which is like google free text search. We get more results .

Package Info

Install Packages

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

1

$sudo apt-get install tmux

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

1

$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-

1

$sudo apt-get install xfce4-*

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

1

$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.

1

$sudo apt-get remove tmux

Remove PackagesRemove Packages

Remove by providing yes option with -y

1

$sudo apt-get remove tmux-y

Remove Package With Configuration

Normally removing 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.

1

$sudo apt-get--purge remove nginx

Remove Package With Configuration

Remove Clean Cache

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

1

$sudo apt autoremove

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.

1

$apt-cache showpkg tmux

Check Dependencies

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

Get Statistics Of Cache

Cache is used to store information about packages. There is command named stat which is used to get overll statistics information.

1

$apt-cache stats

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 those provides updates for packages. Update operation will get most recent information about packages from provided repositories and saved to the cache.

1

$sudo apt-get update

Update System Packages

Upgrade Specific Package

Upgrade operation is generally done in total mode where all package are updated.. But there are some times where only some of the packages should be updated like below.

1

$sudo apt-get upgrade tmux

Upgrade Specific Package

Install Multiple Packages

Install multiple package 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 following example.

1

$sudo apt-get install tmux ksh

Install Multiple Packages

Install Using Wildcards

Another way to install multiple packages is using wildcard. Wildcard is 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.

1

$sudo apt-get install gnome-*

Install Using Wildcards

We can see those selected packages with selecting lines.

Install Without Upgrading

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

1

$sudo apt-get install tmux--no-upgrade

Install Without Upgrading

Install Specific Version Of Package

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

Clean Apt Cache

Apt command downloads package information and packages in to the cache. This cache get bigger and bigger over time. This local cache or repository must be cleaned regularly with the command below.

1

$sudo apt-get clean

Download Only Source Code

As we know packages are compiled from source code and provided as binary. Repositories also provides these source code if the source code option is enable in the /etc/apt/sources.list configuration. Source code can be downloaded like below. Source code in repositories can be enable by un commenting deb-src lines and final look will be like below.

Enable deb source

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

1

$sudo apt-get--download-only source tmux

Download Only Source Code

Download And Unpack Package

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

1

$sudo apt-get source tmux

Download And Unpack Package

Download, Unpack and Compile Package

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

1

$sudo apt-get--compile source tmux

Download, Unpack and Compile Package

Download Only Package

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

1

$apt-get download tmux

Download Only Package

Get Change Log Of Package

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

1

$sudo apt-get changelog tmux

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.

1

$sudo apt-get check

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.

1

$sudo apt-get autoclean

Auto Clean

Auto Remove

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