Skip to main content
Engineering LibreTexts

11-C.3: dpkg / apt How To

  • Page ID
    41949
  • EXAM OBJECTIVES COVERED
    2.1 Given a scenario, conduct software installations, configurations, updates, and removals.

    Using dpkg / apt

    The dkpg Command

    dpkg is a tool to install, build, remove and manage Debian packages. The primary and more user-friendly front-end for dpkg is aptitude (also known as apt). dpkg itself is controlled entirely via command line parameters, which consist of exactly one action and zero or more options. The action-parameter tells dpkg what to do and options control the behavior of the action in some way.

    Syntax:

    dpkg [ OPTIONS ] [ PACKAGE_NAME ]

    A few of the more common options are:

    Options Option Meaning
    -i, –install Install a package using the dpkg command. The command will extract all control files for the specified package, remove any previously installed older instance of the package, and install the new package on our system.
    -r, –remove Remove an installed package from our system. It removes every file belonging to the specific package except the configuration files. This can be seen as the uninstallation option.
    -P, –purge An alternative way to remove an installed package from our system. It completely removes every fie belonging to the specific package, including the configuration files. This can be seen as the ‘complete uninstallation’ option.
    –update-avail The information of the dpkg command about available packages in its repositories. If new packages are available, they are synced from the official repositories.
    –merge-avail Merge the information of the dpkg command about available packages in its repositories with previously available information. It is usually run right after the previous command.
    –help Display the help page for the dpkg command and exit.

    The apt Command

    Debian Linux uses dpkg packaging system. A packaging system is a way to provide programs and applications for installation. This way, the user or the system administrator does not have to build a program from the source code. APT (Advanced Package Tool) is the command line tool to interact with the packaging system in Debian-based Linux distributions.

    There is already dpkg command to manage it. But apt is a more friendly way to handle packaging. You can use it to find and install new packages, upgrade packages, and remove the packages, etc.

    Syntax:

    apt [ OPTIONS ] [ sub-command ] [ package ]

    apt: Update Package Database

    apt actually works on a database of available packages. If the database is not updated, the system won’t know if there are any newer packages available. This is why updating the repository should be the first thing to do in any Linux system after a fresh install.

    Updating the package database requires superuser privileges so you’ll need to use sudo.

    pbmac@pbmac-server $ sudo apt update
    Hit:1 http://packages.microsoft.com/repos/vscode stable InRelease
    Hit:2 http://dl.google.com/linux/chrome/deb stable InRelease
    Hit:3 http://us.archive.ubuntu.com/ubuntu bionic InRelease
    Ign:4 http://dl.google.com/linux/webdesigner/deb stable InRelease
    Hit:5 https://deb.nodesource.com/node_10.x bionic InRelease
    Hit:6 http://dl.google.com/linux/webdesigner/deb stable Release
    

    You’ll see three types of lines here, Hit, Get, and Ign. Basically these are:

    • Hit: there is no change in package version from the previous version.
    • Ign: the package is being ignored. Either the package is too recent that it doesn’t even bother to check, or there was an error in retrieving the file but the error was trivial and thus it is being ignored. Don’t worry, this is not an error.
    • Get: There is a new version available. It will download the information about the version (not the package itself). You can see that there is download information (size in kb) with the ‘get’ line in the screenshot above.

    apt: Upgrade Installed Packages

    Once you have updated the package database, you can now upgrade the installed packages. The most convenient way is to upgrade all the packages that have available updates. You can simply use the command below:

    pbmac@pbmac-server $ sudo apt upgrade
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    Calculating upgrade...Done
    The following packaged will be upgraded:
    

    This will show you how many and which packages are going to be upgraded and ask if you want to continue.

    apt: Install New Packages

    If you already know the name of the package, you can install it using the command below:

    pbmac@pbmac-server $ sudo apt install <package_name>

    Just replace the <package_name> with the desired package. Suppose you want to install mplayer, you can simply use the command below:

    pbmac@pbmac-server $ sudo apt install mplayer
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    The following extra packages will be installed:
      esound-common libaacs0 libaudiofile1 libbluray1 libesd0 libgif4 libvdpau1
    Suggested packages:
      libbluray-bdj pulseaudio-esound-compat nvidia-vdpau-driver
      nvidia-vdpau-driver-ia32 vdpau-driver
    The following NEW packages will be installed:
      esound-common libaacs0 libaudiofile1 libbluray1 libesd0 libgif4 libvdpau1
      mplayer2
    0 upgraded, 8 newly installed, 0 to remove and 1 not upgraded.
    Need to get 1,626 kB of archives.
    After this operation, 4,365 kB of additional disk space will be used.
    Do you want to continue [Y/n]? y

    The output shows extra packages that will be installed, a few suggested packages, and any new packages. The output tells you how much additional disk space will be used and then ask if you want to continue.

    You are not bound to install just one package at a time. You can install several packages at a time by providing the package names all together:

    pbmac@pbmac-server $ sudo apt install <package_1> <package_2> <package_3>

    apt: Remove Installed Packages

    Removing packages is as easy as installing them. Just use the command below:

    pbmac@pbmac-server $ sudo apt remove <package_name>

    Another way of uninstalling packages is to use purge. The command is used in the following manner:

     sudo apt purge <package_name> 

    What is the difference between apt remove and apt purge?

    • apt remove just removes the binaries of a package. It leaves residue configuration files.

     

    • apt purge removes everything related to a package including the configuration files.

    If you used apt remove to a get rid of a particular software and then install it again, your software will have the same configuration files. Of course, you will be asked to override the existing configuration files when you install it again.

    Purge is useful when you have messed up with the configuration of a program. You want to completely erase its traces from the system and perhaps start afresh. And yes, you can use apt purge on an already removed package.

    The apt-get and apt-cache

    There are also two other commands, apt-get and apt-cache, that are almost the same as the apt command. Some of the Debian distributions have opted to use apt as opposed to apt-get and apt-cache command. They all work, and are still viable commands. If you want to know more about the differences simply do a web search of the difference between these commands.

    Adapted from:
    "Using apt Commands in Linux [Complete Guide]" by Abhishek Prakash, Its FOSS is licensed under CC BY-SA 4.0

    • Was this article helpful?