How to list all installed packages in Linux?

To list all packages intentionally installed (not as dependencies) by apt commands, run the following :

(zcat $(ls -tr /var/log/apt/history.log*.gz); cat /var/log/apt/history.log) 2>/dev/null |
  egrep '^(Start-Date:|Commandline:)' |
  grep -v aptdaemon |
  egrep '^Commandline:'

This provides a reverse time based view, with older commands listed first:

Commandline: apt-get install k3b
Commandline: apt-get install jhead
...

Installation data also showing synaptic usage, but without details (the same with installation date) :

(zcat $(ls -tr /var/log/apt/history.log*.gz); cat /var/log/apt/history.log) 2>/dev/null |
  egrep '^(Start-Date:|Commandline:)' |
  grep -v aptdaemon |
  egrep -B1 '^Commandline:'

providing the following:

Start-Date: 2012-09-23  14:02:14
Commandline: apt-get install gparted
Start-Date: 2012-09-23  15:02:51
Commandline: apt-get install sysstat
...

How to list all installed packages in Linux?

To get just the packages which were expressly installed (not just installed as dependencies), you can run

aptitude search '~i!~M'

This will also include a brief description, which you may want. If not, also add the option -F '%p', as mentioned by karthick87.


Yet another option seems to be to copy the file /var/lib/apt/extended_states, which is a text file database in this format:

Package: grub-common
Architecture: amd64
Auto-Installed: 0

Package: linux-headers-2.6.35-22-generic
Architecture: amd64
Auto-Installed: 1

Auto-Installed: 0 indicates that the package was expressly installed and is not just a dependency.

Answer #3:

Ubuntu 14.04 and above

The apt tool on Ubuntu 14.04 and above makes this very easy.

apt list --installed

Older Versions

To get a list of packages installed locally do this in your terminal:

dpkg --get-selections | grep -v deinstall

(The -v tag “inverts” grep to return non-matching lines)

To get a list of a specific package installed:

dpkg --get-selections | grep postgres

To save that list to a text file called packages on your desktop do this in your terminal:

dpkg --get-selections | grep -v deinstall > ~/Desktop/packages

Alternatively, simply use

dpkg -l

(you don’t need to run any of these commands as the superuser, so no sudo or any other variants necessary here)

Answer #4:

Create a backup of what packages are currently installed:

dpkg --get-selections > list.txt

Then (on another system) restore installations from that list:

dpkg --clear-selections
sudo dpkg --set-selections < list.txt

To get rid of stale packages:

sudo apt-get autoremove

To get installed like at backup time (i.e. to install packages set by dpkg --set-selections):

sudo apt-get dselect-upgrade

Answer #5:

apt-mark showmanual

man pages state:

will print a list of manually installed packages

So, it should just give a list of explicitly installed packages (though this includes packages that were part of the default initial install) without all of the dependencies included due to these packages being installed.

To output the result into a text file:

apt-mark showmanual > list-manually-installed.txt

Answer #6:

dpkg-query (instead of dpkg --get-selections, which lists some packages that are not installed) as follows:

dpkg-query -W -f='${PackageSpec} ${Status}\n' | grep installed |  sort -u | cut -f1 -d \ > installed-pkgs

Or:

dpkg -l | grep ^ii | sed 's_  _\t_g' | cut -f 2 > installed-pkgs

How to list all installed packages in Linux?

To list all installed packages,

dpkg -l |awk '/^[hi]i/{print $2}' > 1.txt

or

aptitude search -F '%p' '~i' > 1.txt

or

dpkg --get-selections > 1.txt

Note:
You will get the result 1.txt file in your home folder or you can specify your own path.

Answer #8:

APT-Clone

This package can be used to clone/restore the packages on a apt based system.

  • It will save/restore the packages, sources.list, keyring and automatic-installed states.
  • It can also save/restore no longer downloadable packages using dpkg-repack.

source: man apt-clone

APT-Clone is used by ubiquity (Ubuntu installer) for upgrade process. It is much better than the dpkg --get-selections solution because:

  1. It preserves all repositories information.
  2. It keeps track of what packages were automatically installed.
  3. It allows to repack locally installed DEB files.

How to Use

  1. Install
    sudo apt-get install apt-clone
  2. Make backup
    sudo apt-clone clone path-to/apt-clone-state-ubuntu-$(lsb_release -sr)-$(date +%F).tar.gz
  3. Restore backup
    sudo apt-clone restore path-to/apt-clone-state-ubuntu.tar.gz
  4. Restore to newer release:
    sudo apt-clone restore-new-distro path-to/apt-clone-state-ubuntu.tar.gz $(lsb_release -sc)

Result structure

It makes a simple gzipped tar file which can be easily edited and reviewed before restoring on the other machines. Here an example of its structure:

/
├── etc
│   └── apt
│       ├── preferences.d
│       ├── sources.list
│       ├── sources.list.d
│       │   ├── anton_-ubuntu-dnscrypt-vivid.list
│       │   ├── maarten-baert-ubuntu-simplescreenrecorder-vivid.list
│       │   └── megasync.list
│       ├── trusted.gpg
│       └── trusted.gpg.d
│           ├── anton__ubuntu_dnscrypt.gpg
│           ├── anton__ubuntu_dnscrypt.gpg~
│           ├── maarten-baert_ubuntu_simplescreenrecorder.gpg
│           └── maarten-baert_ubuntu_simplescreenrecorder.gpg~
└── var
    └── lib
        └── apt-clone
            ├── extended_states
            ├── installed.pkgs
            └── uname

Answer #9:

You want to reinstall the packages now there on 12.04, right?

If so, it’s very easy. You’ll need an “Ubuntu Single Sign On account.” (Create it before reinstalling so that your system is synced.)

  1. Go to the Software Center and look for the “Sync Between Computers…” option under the File menu.
  2. When you click on it you will see your computer registered and a list of all apps on your computer.
  3. When you will install fresh, that computer will be considered a new computer.
  4. You just have to sign in to your Ubuntu account and your previous computer will be shown.
  5. Click on it; you’ll get a list of all apps. Select “install” on the app you want to install.

Answer #10:

I’m surprised the apt-cache command designed exactly for this purpose hasn’t been mentioned above…

apt-cache pkgnames

For more info, run apt-cache --help:

**apt-cache is a low-level tool used to query information
from APT's binary cache files

Commands:
   gencaches - Build both the package and source cache
   showpkg - Show some general information for a single package
   showsrc - Show source records
   stats - Show some basic statistics
   dump - Show the entire file in a terse form
   dumpavail - Print an available file to stdout
   unmet - Show unmet dependencies
   search - Search the package list for a regex pattern
   show - Show a readable record for the package
   depends - Show raw dependency information for a package
   rdepends - Show reverse dependency information for a package
   pkgnames - List the names of all packages in the system
   dotty - Generate package graphs for GraphViz
   xvcg - Generate package graphs for xvcg
   policy - Show policy settings

Options:
  -h   This help text.
  -p=? The package cache.
  -s=? The source cache.
  -q   Disable progress indicator.
  -i   Show only important deps for the unmet command.
  -c=? Read this configuration file
  -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp
See the apt-cache(8) and apt.conf(5) manual pages for more information.
**

Answer #11:

dpkg, xargs, & apt-get

This command should accomplish the creation of a text file containing installed packages:

dpkg -l | awk  '{print $2}' > package_list.txt

To accomplish the bulk installation of the listed packages you’ll need to edit ‘package_list.txt’. Remove the weird lines at the top of the file using a text editor. You can then use this command to install packages from the created file using:

xargs < package_list.txt apt-get install -y

apt-cache, xargs, & apt-get

Only use this method if you want all current packages to be installed using the list (which includes automatically installed, etc).

Output the response of ‘apt-cache pkgnames’ to a file we’ll simply name “package_list.txt”. You can accomplish this with:

apt-cache pkgnames > package_list.txt

Then when you want to install packages from “package_list.txt” you would use this command:

xargs < package_list.txt apt-get install -y

apt-mark, xargs, & apt-get

We can use the command apt-mark showmanual to give a list of packages that were manually or initially installed with Ubuntu. We’ll want to output that to a file we’ll just call “package-list.txt”. Use this command to accomplish that:

apt-mark showmanual > package-list.txt

The command we would use to install packages from the file “package_list.txt” is below.

xargs < package_list.txt apt-get install -y

Aptik Migration Utility

Utility to simplify re-installation of software packages after upgrading/re-installing Ubuntu-based distributions.
[Launchpad | Aptik]

For information on Aptik, try visiting its official page.

Installing Aptik is simple. Follow these steps:

  1. Add the PPA with:
    sudo add-apt-repository -y ppa:teejee2008/ppa
  2. Update apt with the below command.
    sudo apt-get update
  3. Install Aptik using:
    sudo apt-get install aptik

Hope you learned something from this post.

Follow Programming Articles for more!

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at programming-articles.com, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →