Ade Malsasa Akbar contact
Senior author, Open Source enthusiast.
Thursday, January 19, 2017 at 23:12


This beginner's guide for Part 2 explains Apt-get commands with examples for Ubuntu package management. This including some required explanations (about package, how apt works, and apt "database") and screenshots for 10 examples. This guide also includes add-apt-repository command, despite it's not part of APT, it's mentioned here to ensure beginner users happy with PPA and third-party repositories. This guide is a continuation of the Part 1 Dpkg Commands. I hope everyone can take advantage from this article. Enjoy.
Subscribe to UbuntuBuzz Telegram Channel https://telegram.me/ubuntubuzz to get article updates directly.


Package


In Ubuntu operating system platform, software is “distributed” by Ubuntu developer to all the users. Ubuntu already distributes approximately 80.000 software in the 16.10 version at 2016. Each software is distributed as certain file called “package”. The user installs software in their Ubuntu system also in form of “package”. So in other words working with Ubuntu is working with software packages.

Ubuntu distribution gives two types of package to the users, source code package and binary package. Source package is a file with .tar.gz format while binary package is a file with .deb format. Ubuntu developer puts large efforts to transform all source packages into binary packages so every users can just install the .deb instantly and run the software in their computer. All software packages of Ubuntu distribution stored at some public server named "repository". Every Ubuntu user installs software from this so-called repository.

How APT Works


APT is a complex system composed of apt-get and many other programs. But from end-user point of view, the main concept is simple, this APT system is simply a client of Ubuntu repository server. In other words, APT is a local program in your Ubuntu system to communicate with the Ubuntu repository server. In particular, apt-get is the download manager of APT system to download package from repository.

In technical sense, APT’s main purpose is to resolve dependency, and that’s why people calls APT dependency resolver. APT works by reading Ubuntu internal databases to determine what packages installed and what packages not installed, then doing a calculation resulting complete list of packages need to be installed, and then the apt-get downloads those packages from repository through the network. The final job is actually done by Dpkg to install packages one by one. APT does not install package, APT is just dependency resolver. By downloading, APT stores all packages downloaded at /var/cache/apt/archives/ directory.

In order to work with repository, of course, APT needs the address of the repository itself. This requirement is fulfilled by sources.list settings. The sources.list is actually a text file containing URL addresses of repository and some “codes” to determine what “room” of repository enabled/disabled. The settings is located in /etc/apt/sources.list file. APT system gives more options by providing /etc/apt/sources.list.d/ directory if the user wants to use third-party repository.

Important Database


APT works with its own “database” in your Ubuntu system located at /var/lib/apt/lists/.  APT database is basically a bunch of text files storing complete information information such as name, URL, size, dependencies, description, etc. of every single of thousand packages from the repository.  To make APT works, first the user must invokes APT to download the “map” from repository (I call it here: reload). Once this "map" completeley obtained, then the user can perform search for any package they want and ask APT to install any package. Without this "map" APT will not work.


1. Reload


Command synopsis:
$ sudo apt-get update

Example:
$ sudo apt-get update

Output:

Explanation:
The update command downloads "repository map". Repository map is database files containing complete information about the repository and all packages inside it. All database files downloaded stored at /var/lib/apt/lists/ as .list files and you can read them using any text editor. Whenever you change your sources.list settings, you should run this update command once. Without this “map”, APT can not download any package. This requires internet connection.

Note: I use the term "Reload" here to avoid confusion between "update" and "upgrade" terms, to make it clear that apt-get update does not install any package. Also, the term "Reload" is used in popular Synaptic Package Manager to express the same thing.

2. Install


Command synopsis:
$ sudo apt-get install <package_name>

Example:
$ sudo apt-get install flowblade

Output:


Explanation:
The install command installs a software from repository. You will be asked firstly for password, type it anyway and press Enter; then you will be asked secondly for permission, type “y” letter anyway and press Enter. The APT will download all packages needed and the software will be installed in your system. Do not close the terminal while the installation progress is still going. Therefore, this requires internet connection.


3. Remove


Command synopsis:
$ sudo apt-get remove <package_name>

Example:
$ sudo apt-get remove firefox

Output:


Explanation:
The remove command uninstalls software from your system. The remove command uninstalls only a single package, it does not uninstalls the package dependencies came with it. After performing this command, you can no longer run the software you removed. This does not require internet connection.


4. Upgrade


Command synopsis:
$ sudo apt-get upgrade

Example:
$ sudo apt-get upgrade

Output:

Explanation:
The upgrade command downloads and installs all new version of all packages installed in your system. This command depends on your sources.list settings, so APT can find new version of packages only if the repository you used does provide them. Remember that upgrade is completely different to update: upgrade installs packages while update does not install any.

5. Dist-Upgrade


Command synopsis:
$ sudo apt-get dist-upgrade

Example:
$ sudo apt-get dist-upgrade

Output:



Explanation:
The dist-upgrade command is a companion of upgrade command, it helps the upgrade command with a “smart conflict resolution”. To give you the difference between both commands: upgrade does not delete any package, while dist-upgrade may delete packages in order to resolve conflicts. This dist-upgrade is usually performed by the user once upgrade command finished. The dist-upgrade command does only packages upgrade, not system version upgrade, so using it will never upgrade Ubuntu 16.04 into 16.10 for example.

6. Download Only


Command synopsis:
$ sudo apt-get download <package_name>

Example:
$ sudo apt-get download gparted

Output:

Explanation:
The download command downloads package. This shows that apt-get is basically a download manager. This command will not install the package. The package will be downloaded to the same directory as your command line running. For example, when you perform this command without changing directory, then by default the package stored at your $HOME.

7. Simulate


Command synopsis:
$ sudo apt-get --simulate install <package_name>
$ sudo apt-get --simulate upgrade
$ sudo apt-get –simulate dist-upgrade

Example:
$ sudo apt-get --simulate install gparted

Output:


Explanation:
This option --simulate is available for apt-get commands to simulate (dry run) the package management being processed. This option is usable in the install, remove, upgrade, and dist-upgrade commands. The –simulate is extremely useful to prevent any error before installing/upgrading, because it shows the installing/upgrading process without doing the real installing/upgrading. So you will know if installing certain package there will be an error or not by this --simulate. It can be used when offline.


8. Add Repository


Command synopsis:
$ sudo add-apt-repository

Example
$ sudo add-apt-repository 'deb http://kambing.ui.ac.id/ubuntu yakkety main universe'
$ sudo add-apt-repository ‘ppa:gimp/gimp-stable’

Output: 


Explanation:
This add-apt-repository command is not a part of APT, rather, it is a part of software-properties-common package in Ubuntu. This command is a helper to add new repository address to sources.list easily. This command is very common among majority of Ubuntu desktop users, to add new PPA repository (third party repository) to the system so they can install the software available in that repository. To use it, run it as example, and then read the message retrieved, and then press Enter to continue adding the repository address, and after adding you should do Reload to get "the map" of the new repository. This command should be used online.

Note: the reason I put add-apt-repository is because this command is very famous among PPA users and almost all Ubuntu desktop users use PPA. So for your convenience, I put it here.

9. Print Uris


Command synopsis:
$ sudo apt-get  --print-uris install <package_name>

Example:
$ sudo apt-get --print-uris install gparted

Output:


Explanation:
This option --print-uris shows all download links of a software package installation. So for example if a gparted installation needs two packages to be downloaded, it shows the two download links of two packages. If you download the two packages manually somewhere and install them by Dpkg, you may install gparted software correctly in an offline Ubuntu system. Yes, this option --print-uris is extremely useful for offline users. This option –print-uris can be used when offline, because APT generates all download links from its “repository map” (local database /var/lib/apt/lists/ and its sources.list). Again, apt-get is basically a download manager so it certainly knows all package download links. This does not require any internet connection.

10. Always Yes


Command synopsis:
$ sudo apt-get install --yes <package_name>

Example:
$ sudo apt-get install --yes gparted
$ sudo apt-get remove --yes gparted
$ sudo apt-get upgrade --yes
$ sudo apt-get dist-upgrade --yes

Output:


Explanation:
This option --yes makes the user may leave safely the Terminal while APT is processing. The meaning of --yes here is to answer “y” automatically to APT for all questions possible while doing install/remove/upgrade/dist-upgrade. So, by using this you do not need to answer any further question one by one while doing package management.