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.
yakkety 4.9.0-2 amd64
- 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.
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.
$ 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.
$ sudo apt-get upgrade
We can use following command to make upgrade job more automatic
$ 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.
$ 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.
$ sudo apt-get dist-upgrade -y
Searching packages are easy. We will just provide the term or package name we want to search
$ 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 .
$ apt-cache search tmu
We may want to get package details information about installed-size, architecture, dependencies, version, file name, description,mainainer, etc.
$ apt-cache show tmux
Installing packages is easy as other operations wejujst provide our package name to the apt-get install command like below.
$ 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.
$ 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-*
We can also install multiple package in the same command by separating them spaces .
$ sudo apt-get install tmux apache2
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 by providing yes option with -y
$ 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.
$ sudo apt-get --purge remove nginx
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.
$ sudo apt autoremove
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
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 needs the tmux package.
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.
$ apt-cache stats
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.
$ sudo apt-get update
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.
$ sudo apt-get upgrade tmux
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.
$ sudo apt-get install tmux ksh
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.
$ sudo apt-get install gnome-*
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.
$ sudo apt-get install tmux --no-upgrade
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.
$ 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
Completely Remove Packages
Package configuration can be removed if its not needed or corrupted. Purge is the command used to clear configuration of packages.
$ sudo apt-get purge tmux
OR alterntively the same operation can be done with remove command by providing –purge option
$ sudo apt-get remove --purge tmux
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.
$ 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.
Then run apt-get update to get latest information about source packages.
$ sudo apt-get --download-only source tmux
Download And Unpack Package
To make things easier source package can be downloaded and unpacked automatically like below.
$ sudo apt-get source tmux
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.
$ sudo apt-get --compile source tmux
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.
$ apt-get download tmux
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.
$ sudo apt-get changelog tmux
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
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
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.
$ sudo apt-get autoremove tmux