34

I want to get a list of packages that I have installed using apt-get.

i.e. any packages that I have installed since my Linode (Debian) was initially created.

Is this possible?

1
  • 2
    both answers here mention the history.log files in /var/log/apt/. It's important to note that, by default, logrotate is configured to keep only the last 12 months worth of these files. The same is true of the /dpkg.log files in /var/log (which provide similar infomation in an (IMO) more-easily parsed format). Edit /etc/logrotate.d/{apt,dpkg} to change retention policy for your system.
    – cas
    Commented Jun 7, 2016 at 4:09

7 Answers 7

32

You can list installed package using apt using the following command:

apt --installed list

Edit

Use the following command to list installed package through apt-get :

zcat /var/log/apt/history.log.*.gz | cat - /var/log/apt/history.log | grep -Po '^Commandline: apt-get install (?!.*--reinstall)\K.*'

Source: Askubuntu

4
  • 8
    As you have stated that lists all packages installed. I want packages that I have manually installed using apt-get commands.
    – user598200
    Commented Jun 6, 2016 at 18:49
  • 1
    @user598200 - Consider accepting this answer. :) Commented Apr 12, 2017 at 8:18
  • 1
    Based on this askubuntu answer I get to (zcat $(ls -tr /var/log/apt/history.log*.gz); cat /var/log/apt/history.log) 2>/dev/null | egrep '^(Commandline: apt(-get)? install)' | grep -v aptdaemon | egrep '^Commandline:'
    – Pablo A
    Commented Jun 29, 2017 at 23:12
  • 1
    zgrep -hPo '^Commandline: apt-get install (?!.*--reinstall)\K.*' /var/log/apt/history.log{.*.gz,} produces the same output without the unnecessary zcat and cat. if you don't care about the exact order of package names in the output, /var/log/apt/history.log* will do for the filename argument.
    – cas
    Commented Nov 16, 2017 at 1:05
11

If you want to display only a list with the packages you have manually installed you could run:

apt --installed list | grep -v automatic

to not list packages flagged as automatically installed

-v, --invert-match   Invert the sense of matching, to select non-matching lines.

Another option that won't erroneously exclude an unfortunate package with "automatic" in its name is:

apt-mark showmanual
2
  • 1
    Short and sweet, and sorted alphabetically.
    – hlongmore
    Commented Dec 14, 2018 at 2:40
  • 2
    If by chance a package name contains "automatic" it will be filtered out by grep.
    – bernie
    Commented Feb 14, 2021 at 10:24
7
zgrep -h ' install ' /var/log/dpkg.log* | sort | awk '{print $4}'

This will give you a list of packages that have been installed, in the order that they were installed.

As per my comment, only the last 12 months worth of/var/log/dpkg.log* files are kept by default. To change this, edit /etc/logrotate.d/dpkg. For example, change rotate 12 to rotate 1200 to keep the last 1200 months (100 years) worth - effectively forever, never delete the old logs.

BTW, if you want to see when packages were upgraded, change install to upgrade. e.g.

zgrep -h ' upgrade ' /var/log/dpkg.log* | sort

same for remove and purge.

3

This relies on aptitude, but the following will give you a list of packages that are manually installed, i.e. aren't installed just because of dependency:

aptitude search -F %p '~i!~M'

Depending on how the image is setup, it will probably also include some installed by the base system. You can get rid of a fair bit of those by switching them to being automatically installed, then just mark as manual the few you want to keep despite not having anything depending on them. This is easy to do with M inside aptitude on a bare system.

1

Use dpkg to list all packages installed on a system: dpkg --get-selections

To list all packages installed using apt-get look at /var/log/apt/history.log. This log also contains packages you have removed. You can grep for the lines that start with Install:.

4
  • 1
    As you have stated that lists all packages installed. I want packages that I have manually installed using apt-get commands.
    – user598200
    Commented Jun 6, 2016 at 18:45
  • @user598200 Ah. Thank you for clarifying. See my edited answer. I am not sure if apt-get has that functionality. But whenever you install something, it is appended to the log file I mention in the answer.
    – Peschke
    Commented Jun 6, 2016 at 18:49
  • Thanks. Any way I can go through all of the /var/log/apt/history.log* files and print out the package names in one hit?
    – user598200
    Commented Jun 6, 2016 at 18:51
  • @user598200 You could use grep. grep "Install: package" /var/log/apt/history.log*
    – Peschke
    Commented Jun 6, 2016 at 19:00
1

Ubuntu 14.04 and above use apt list --installed for older versions use dpkg --get-selections | grep -v deinstall

1

A bit shorter and sorted

gunzip -c /var/log/apt/history.log.*.gz | grep 'apt-get install' | cut -f4- -d" " | tr ' ' $'\n' | sort -u
4
  • 2
    Some explanation as to why this is better than the answer @GAD3R gave would be helpful.
    – Anthon
    Commented Jul 12, 2017 at 20:26
  • First of all, it's shorter, second of all the output is sorted
    – jedi
    Commented Jul 12, 2017 at 21:00
  • The idea on U&L is that you improve your answer (or question in case you had posted one) by editing the post, instead of hiding valuable information in comments. As I would cut and paste the command the 26 bytes less is not so interesting, but the fact that your output is sorted really belongs in the answer itself. If such an improvement was as a reaction to a request for clarification a simple "updated my answer" as comment will do, the commenter (in this case me) will get notified of that comment. I think the terseness also is the cause that someone downvoted your answer.
    – Anthon
    Commented Jul 12, 2017 at 21:25
  • If you want to play golf then try zgrep -Pho '(?<=apt-get install ).*' /var/log/apt/history.log.*.gz | tr ' ' $'\n' | sort -u. Commented Jul 12, 2017 at 23:09

Not the answer you're looking for? Browse other questions tagged .