How to get list of installed packages on Ubuntu / Debian Linux

Posted on in Categories , , , last updated February 9, 2017

I am a new Ubuntu Linux sysadmin. How do I get a list of all installed packages on server1 and install those on server2? How to list all installed packages using apt-get command on Ubuntu or Debian server?

You need to use dpkg and other commands as follow to see a list of all installed packages on Ubuntu or Debian Linux server.

Just list all installed packages with a short description

Type the following command:
$ dpkg-query -l
Sample outputs:

| 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.40-2ubuntu11.3                amd64        query and manipulate user account information
ii  acct                          6.5.5-2.1ubuntu1                  amd64        The GNU Accounting utilities for process and login accounting
ii  acl                           2.2.52-3                          amd64        Access control list utilities
ii  adduser                       3.113+nmu3ubuntu4                 all          add and remove users and groups
ii  apparmor                      2.10.95-0ubuntu2.5                amd64        user-space parser utility for AppArmor
ii  apt                           1.2.19                            amd64        commandline package manager
ii  apt-transport-https           1.2.19                            amd64        https download transport for APT
ii  apt-utils                     1.2.19                            amd64        package management related utility programs
ii  atop                          1.26-2build1                      amd64        Monitor for system resources and process activity
ii  base-files                    9.4ubuntu4.3                      amd64        Debian base system miscellaneous files
ii  base-passwd                   3.5.39                            amd64        Debian base system master password and group files
ii  bash                          4.3-14ubuntu1.1                   amd64        GNU Bourne Again SHell
ii  bash-completion               1:2.1-4.2ubuntu1.1                all          programmable completion for the bash shell
ii  bc                            1.06.95-9build1                   amd64        GNU bc arbitrary precision calculator language
ii  bind9-host                    1:9.10.3.dfsg.P4-8ubuntu1.4       amd64        Version of 'host' bundled with BIND 9.X
ii  bsdmainutils                  9.0.6ubuntu3                      amd64        collection of more utilities from FreeBSD

Just want to see a list of packages only? Try:
$ dpkg-query -f '${binary:Package}\n' -W
Sample outputs:


How to get a list of packages installed

Type the following command
$ dpkg --get-selections | grep -v deinstall
Sample outputs:

Fig.01: Show a list of packages installed
Fig.01: Show a list of packages installed

Another option is to use apt command as follows:
$ apt list --installed
OR just search all php based packages using grep command:
$ apt list --installed | grep php
Sample outputs:

accountsservice/xenial-updates,now 0.6.40-2ubuntu11.3 amd64 [installed,automatic]
acl/xenial,now 2.2.52-3 amd64 [installed,automatic]
adduser/xenial,xenial,now 3.113+nmu3ubuntu4 all [installed]
apparmor/xenial-updates,now 2.10.95-0ubuntu2.5 amd64 [installed]
apt/xenial-updates,now 1.2.19 amd64 [installed]
apt-transport-https/xenial-updates,now 1.2.19 amd64 [installed]
apt-utils/xenial-updates,now 1.2.19 amd64 [installed]
atop/xenial,now 1.26-2build1 amd64 [installed]
base-files/xenial-updates,now 9.4ubuntu4.3 amd64 [installed]
base-passwd/xenial,now 3.5.39 amd64 [installed]
bash/xenial-updates,now 4.3-14ubuntu1.1 amd64 [installed]
bash-completion/xenial-updates,xenial-updates,now 1:2.1-4.2ubuntu1.1 all [installed]
bc/xenial,now 1.06.95-9build1 amd64 [installed]
bind9-host/xenial-updates,xenial-security,now 1:9.10.3.dfsg.P4-8ubuntu1.4 amd64 [installed]
bsdmainutils/xenial,now 9.0.6ubuntu3 amd64 [installed]
bsdutils/xenial-updates,now 1:2.27.1-6ubuntu3.2 amd64 [installed]
busybox-initramfs/xenial,now 1:1.22.0-15ubuntu1 amd64 [installed]
busybox-static/xenial,now 1:1.22.0-15ubuntu1 amd64 [installed]
bzip2/xenial,now 1.0.6-8 amd64 [installed]
ca-certificates/xenial,xenial,now 20160104ubuntu1 all [installed]
command-not-found/xenial-updates,xenial-updates,now 0.3ubuntu16.04.2 all [installed]
command-not-found-data/xenial-updates,now 0.3ubuntu16.04.2 amd64 [installed]
console-setup/xenial-updates,xenial-updates,now 1.108ubuntu15.2 all [installed]
console-setup-linux/xenial-updates,xenial-updates,now 1.108ubuntu15.2 all [installed]
xkb-data/xenial,xenial,now 2.16-1ubuntu1 all [installed]
xml-core/xenial,xenial,now 0.13+nmu2 all [installed,automatic]
xz-utils/xenial,now 5.1.1alpha+20120614-2ubuntu2 amd64 [installed,automatic]
zlib1g/xenial,now 1:1.2.8.dfsg-2ubuntu4 amd64 [installed]

How to save a list to a text file

The syntax is as follows on server1:
$ dpkg --get-selections | grep -v deinstall > mylist.txt
$ dpkg -l | grep ^ii | awk ‘{ print $2}’ > mylist.txt
Now copy mylist.txt to server named server2:
$ scp mylist.txt [email protected]:~/

How to restore a list on server2

On server2 restore installations from the mylist.txt, run:
$ dpkg --clear-selections
$ sudo dpkg --set-selections < mylist.txt

$ sudo apt-get install -y $(< mylist.txt)
Finally run the following command to remove unwanted packages:
$ sudo apt-get autoremove
Above command should restore/install packages from backups or another server. Another option is to use aptitude command as follows:

### Create the list of software to a file called /tmp/packages.txt ###
dpkg --get-selections > /tmp/packages.txt
### copy file to another server called ##
scp /tmp/packages.txt
### Now that your list is copyied use apt-get/synaptic to install the packages###
# 1. Login to another server
# 2. Restore it 
aptitude install $(cat /tmp/packages.txt | awk '{print $1}')

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

3 comment

  1. Alternative:
    ( zcat $( ls -tr /var/log/apt/history.log*.gz ) ; cat /var/log/apt/history.log ) | grep apt-get | grep install

    It is not perfect but always worked fine for me.

Leave a Comment