How To Use dpkg On Linux

Dpkg is the abbreviation of the Debian package. It is a suite management system specially developed for the “Debian” operating system, which is used for software installation, update, and removal. All Linux distributions from “Debian” use dpkg, such as “Ubuntu”.

1. How To Install dpkg On Ubuntu.

  1. Open a terminal and run the command sudo apt-get update -y in it to update the Ubuntu system.
    $ sudo apt-get update -y
    [sudo] password for jerry: 
    Hit:1 https://dl.google.com/linux/chrome/deb stable InRelease
    Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]                                  
    ......
    Get:12 http://cn.archive.ubuntu.com/ubuntu focal-backports/main amd64 DEP-11 Metadata [7,984 B]
    Get:13 http://cn.archive.ubuntu.com/ubuntu focal-backports/universe amd64 DEP-11 Metadata [12.2 kB]
    Fetched 1,112 kB in 5s (236 kB/s)                                       
    Reading package lists... Done
    
  2. Run the command sudo apt-get install -y dpkg in the terminal to install the dpkg package.
    $ sudo apt-get install -y dpkg
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    dpkg is already the newest version (1.19.7ubuntu3).
    0 upgraded, 0 newly installed, 0 to remove and 80 not upgraded.
    
  3. Verify the dpkg package has been installed successfully with the command dpkg –version.
    $ dpkg --version
    Debian 'dpkg' package management program version 1.19.7 (amd64).
    This is free software; see the GNU General Public License version 2 or
    later for copying conditions. There is NO warranty.

2. How To Use dpkg To Install Debian Packages.

  1. Download a Debian package file (.deb) file.
  2. Then you can run the command dpkg -i <debian_package_file_path_name> to install it.
    $ sudo dpkg -i ./google-chrome-stable_current_amd64.deb

3. How To Use dpkg To List The Files Associated With The Package.

  1. Add -L parameter to the dpkg command will list out all the files associated with the specified package.
  2. The command syntax is $ dpkg -L <debian_package_name>.
    $ dpkg -L google-chrome-stable 
    /.
    /etc
    /etc/cron.daily
    ......
    /usr/bin/google-chrome-stable
    /usr/share/man/man1/google-chrome.1.gz
    

4. Show The dpkg Package Version.

  1. If you want to know the Debian package version you can run the command dpkg -l <debian_package_name>.
    $ dpkg -l google-chrome-stable 
    Desired=Unknown/Install/Remove/Purge/Hold
    | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
    |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
    ||/ Name                 Version        Architecture Description
    +++-====================-==============-============-=================================
    ii  google-chrome-stable 97.0.4692.99-1 amd64        The web browser from Google

5. Remove Debian Package.

  1. The command dpkg -r <package_name> will remove the Debian package and save the configuration settings.
    $ sudo dpkg -r google-chrome-stable 
    [sudo] password for jerry: 
    (Reading database ... 186281 files and directories currently installed.)
    Removing google-chrome-stable (97.0.4692.99-1) ...
    update-alternatives: using /usr/bin/firefox to provide /usr/bin/x-www-browser (x-www-browser) in auto mode
    update-alternatives: using /usr/bin/firefox to provide /usr/bin/gnome-www-browser (gnome-www-browser) in auto mode
    Processing triggers for man-db (2.9.1-1) ...
    Processing triggers for gnome-menus (3.36.0-1ubuntu1) ...
    Processing triggers for desktop-file-utils (0.24-1ubuntu3) ...
    Processing triggers for mime-support (3.64ubuntu1) ...
  2. If you do not want to save the package configuration settings when you remove the software, you can run the command dpkg -P <package_name>.
    $ sudo dpkg -P google-chrome-stable 
    (Reading database ... 186281 files and directories currently installed.)
    Removing google-chrome-stable (97.0.4692.99-1) ...
    update-alternatives: using /usr/bin/firefox to provide /usr/bin/x-www-browser (x-www-browser) in auto mode
    update-alternatives: using /usr/bin/firefox to provide /usr/bin/gnome-www-browser (gnome-www-browser) in auto mode
    Purging configuration files for google-chrome-stable (97.0.4692.99-1) ...
    dpkg: warning: while removing google-chrome-stable, directory '/opt' not empty so not removed
    Processing triggers for man-db (2.9.1-1) ...
    Processing triggers for gnome-menus (3.36.0-1ubuntu1) ...
    Processing triggers for desktop-file-utils (0.24-1ubuntu3) ...
    Processing triggers for mime-support (3.64ubuntu1) ...

6. List Debian Package File’s Content.

  1. The command $ dpkg -c <debian_package_file_path_file> can list all the content in the .deb file.
    $ dpkg -c ./google-chrome-stable_current_amd64.deb 
    drwxr-xr-x root/root         0 2022-01-19 08:40 ./
    drwxr-xr-x root/root         0 2022-01-19 08:40 ./etc/
    ......
    lrwxrwxrwx root/root         0 2022-01-19 08:40 ./usr/share/man/man1/google-chrome.1.gz -> google-chrome-stable.1.gz
    

7. Get The Debian Package’s Detailed Information.

  1. Run the command dpkg -s <debian_package_name> will return the Debian package’s detailed information.
    $ dpkg -s google-chrome-stable
    Package: google-chrome-stable
    Status: install ok installed
    Priority: optional
    Section: web
    Installed-Size: 282281
    Maintainer: Chrome Linux Team <[email protected]>
    Architecture: amd64
    Version: 97.0.4692.99-1
    Provides: www-browser
    Depends: ca-certificates, fonts-liberation, libasound2 (>= 1.0.16), libatk-bridge2.0-0 (>= 2.5.3), libatk1.0-0 (>= 2.2.0), libatspi2.0-0 (>= 2.9.90), libc6 (>= 2.17), libcairo2 (>= 1.6.0), libcups2 (>= 1.6.0), libcurl3-gnutls | libcurl3-nss | libcurl4 | libcurl3, libdbus-1-3 (>= 1.5.12), libdrm2 (>= 2.4.38), libexpat1 (>= 2.0.1), libgbm1 (>= 8.1~0), libgcc1 (>= 1:3.0), libglib2.0-0 (>= 2.39.4), libgtk-3-0 (>= 3.9.10) | libgtk-4-1, libnspr4 (>= 2:4.9-2~), libnss3 (>= 2:3.22), libpango-1.0-0 (>= 1.14.0), libx11-6 (>= 2:1.4.99.1), libxcb1 (>= 1.9.2), libxcomposite1 (>= 1:0.4.4-1), libxdamage1 (>= 1:1.1), libxext6, libxfixes3, libxkbcommon0 (>= 0.4.1), libxrandr2, wget, xdg-utils (>= 1.0.2)
    Pre-Depends: dpkg (>= 1.14.0)
    Recommends: libu2f-udev, libvulkan1
    Description: The web browser from Google
     Google Chrome is a browser that combines a minimal design with sophisticated technology to make the web faster, safer, and easier.
    

8. Extract The Debian Package Content.

  1. The command dpkg –x <debian_package_file_path_name.deb> <target_directory_path> can extract the Debian package contained files.
    $ dpkg -x ./google-chrome-stable_current_amd64.deb ./
    jerry@jerry-VirtualBox:~/Downloads$ ls -l
    total 624280
    drwxr-xr-x 3 jerry jerry      4096 1月  19 08:40 etc
    -rw-rw-r-- 1 jerry jerry  90862320 1月  19 09:11 google-chrome-stable_current_amd64.deb
    drwxr-xr-x 3 jerry jerry      4096 1月  19 08:40 opt
    drwxr-xr-x 4 jerry jerry      4096 1月  19 08:40 usr
    

9. Search The Debian Package Content.

  1. If you want to search a dpkg package installed path, you can run the command dpkg -S <path_contained_keyword> in the terminal.
    $ dpkg -S google-chrome
    bash-completion: /usr/share/bash-completion/completions/google-chrome
    bash-completion: /usr/share/bash-completion/completions/google-chrome-stable
    

10. List All The dpkg Packages.

  1. The command dpkg –list will list all the debian packages.
    $ dpkg --list
    Desired=Unknown/Install/Remove/Purge/Hold
    | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
    |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
    ||/ Name                                       Version                               Architecture Description
    +++-==========================================-=====================================-============-=================================>
    ii  accountsservice                            0.6.55-0ubuntu12~20.04.5              amd64        query and manipulate user account>
    ii  acl                                        2.2.53-6                              amd64        access control list - utilities
    ......
  2. If you want to list the special Debian package installation info, you can run the command dpkg –list <dpkg_package_name>.
    $ dpkg --list google-chrome-stable
    Desired=Unknown/Install/Remove/Purge/Hold
    | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
    |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
    ||/ Name                 Version        Architecture Description
    +++-====================-==============-============-=================================
    ii  google-chrome-stable 97.0.4692.99-1 amd64        The web browser from Google
    jerry@jerry-VirtualBox:~/Downloads$
  3. Run the command dpkg –list | grep keyword to Fuzzy search Debian’s package name.
    $ dpkg --list|grep google
    ii  google-chrome-stable                       97.0.4692.99-1                        amd64        The web browser from Google
  4. Get the installed Debian packages number with the command sudo dpkg-query -f ‘${binary:Package}\n’ -W | wc -l.
    $ sudo dpkg-query -f '${binary:Package}\n' -W | wc -l
    [sudo] password for jerry: 
    1627
  5. The dpkg-query command can also implement the query function on Debian packages.
    $ sudo dpkg-query -l | grep chrome
    ii  google-chrome-stable                       97.0.4692.99-1                        amd64        The web browser from Google
    

11. How To Export The Currently Installed Debian Packages To A Text File.

  1. Run the command $ sudo dpkg-query -f ‘${binary:Package}\n’ -W > packages_list.txt to export all the installed Debian packages to a text file.
    $ sudo dpkg-query -f '${binary:Package}\n' -W > packages_list.txt
    jerry@jerry-VirtualBox:~/Downloads$ ls -l
    total 624296
    -rw-rw-r-- 1 jerry jerry     28566 Jan  25 17:46 packages_list.txt
    

12. Restore The Installed Debian Packages To A New Ubuntu Server.

  1. Now you can restore the installed Debian packages to a new Ubuntu server with the above packages list text file like below.
    $ sudo xargs -a packages_list.txt apt install
    

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.