120

I am about do move a server from one Ubuntu box to another. I'm not cloning the old box to the new; I'm creating a new system and will move data as needed. I want to install all the software that I have on the old box on the new one.

Is there a simple way to find the history of all the "sudo apt-get install" commands I have given over time? That is, dpkg -l shows me all the packages that have been installed, but not which top-level package installed them. If there is a way for dpkg to give me the installing package, I can find the unique ones there; otherwise, I want something else to say "you installed these 24 packages".

raphink
  • 11,337
  • 6
  • 36
  • 47
Paul Hoffman
  • 2,094
  • 4
  • 18
  • 23
  • 4
    Aha! [it's been asked before, and has a good answer.](http://serverfault.com/questions/161412/how-to-get-a-list-of-all-root-main-installed-packages-on-debian) – Gilles 'SO- stop being evil' Aug 29 '10 at 23:04
  • Can you accept an answer below? To be grateful to people who answered. – m-ric Dec 01 '16 at 21:01
  • Possible duplicate of [How to Get a List of All Root / Main Installed Packages on Debian](http://serverfault.com/questions/161412/how-to-get-a-list-of-all-root-main-installed-packages-on-debian) – Rob Moir Jan 22 '17 at 19:40
  • These solutions will only check what has happened AFTER the last log rotation. They do not check all history. – Kraang Prime Feb 25 '18 at 02:21
  • 1
    Simple and fast way is given by this answer, https://askubuntu.com/a/250530/197730, to this question [How to list all installed packages](https://askubuntu.com/q/17823/197730). – TooTone May 12 '19 at 11:48

9 Answers9

106

The apt history is in /var/log/apt/history.log as said in a comment above. That said, this will not list packages that were installed manually, using dpkg or GUIs such as gdebi. To see all the packages that went through dpkg, you can look at /var/log/dpkg.log.

raphink
  • 11,337
  • 6
  • 36
  • 47
  • On my Ubuntu 18.04 install, the log only goes back to the beginning of the current month. – MrMas Mar 23 '21 at 17:55
15

You can list packages whose installation has been explicitly requested with apt-mark.

apt-mark showmanual

In case you're running an ancient release of Debian, here's a manual way.

The following command gives the list of packages whose installation was requested, whether manually or automatically. Unless you're in the middle of (de)installing packages, this is the list of installed packages.

dpkg --get-selections | sed -n 's/\t\+install$//p'

The following command gives a superset of automatically installed packages:

</var/lib/apt/extended_states awk -v RS= '/\nAuto-Installed: *1/{print$2}'

Putting it all together, the following command lists manually installed packages:

comm -23 <(dpkg --get-selections | sed -n 's/\t\+install$//p') \
         <(</var/lib/apt/extended_states \
           awk -v RS= '/\nAuto-Installed: *1/{print$2}' |sort)
  • 1
    --- very different result from `comm -23 <(apt-mark showmanual | sort -u) <(gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Package: //p' | sort -u)` – Cbhihe Oct 27 '15 at 15:09
  • On Debian, there is `apt-mark showmanual` which is equivalent to your last command. – maxschlepzig Jan 22 '17 at 15:55
8

http://www.debianadmin.com/clone-your-ubuntu-installation.html

and /var/adm/apt/history.log

M_1
  • 363
  • 2
  • 10
  • 5
    The procedure in the first link doesn't distinguish between automatically-installed packages and manually-installed packages. `/var/log/apt/history.log` (you got the location wrong btw) will have rotated away after a few months. – Gilles 'SO- stop being evil' Aug 28 '10 at 07:55
  • Gilles is right on both counts. I only have about of month's worth of those logs, and I can't even see how the log deletion rate is set (it's done with 'newsyslog' on FreeBSD). So I still don't have a solution, but I have some pointers. I can at least grep the output from the command in the cloning article, look for "high-level" names, install them on the new one, do the same thing on the new box, diff the two results, and repeat until satisfied. I would still ove to hear more ideas. – Paul Hoffman Aug 29 '10 at 15:42
6

I'm also "greping" tar.gz-ed history files this way:

zgrep -E "Commandline: apt(|-get) install" /var/log/apt/history.log*

If you need timestamp too, just add an extra parameter -B1.

CraZ
  • 200
  • 1
  • 5
3

Instead of tac / head combination, it is better to use tail (for last 25 lines):

tail -n 25 /var/log/apt/history.log
Integer
  • 139
  • 2
  • This doesn't differ sufficiently from @ℝaphink's answer and should be a comment to it. – Kalle Richter Jun 17 '17 at 16:16
  • Also, it's not the same. tail will list the last lines in the file, in the order in which they're in the file. tac will reverse the order so that the last line is now first, second to last is second, etc. Also again, seems you kind of risk being wrong when you say something is "better" without explaining why. I mean, "better" according to whom? For what requirement? Seems kinda over-confident. – Todd Walton Jul 06 '18 at 18:36
3
grep -i "Commandline" /var/log/apt/history.log

Shows all the packages you've installed using: sudo apt-get install xxxxx

agc
  • 111
  • 4
0

Getting an uncluttered list turned out to be a fair bit more complicated than I imagined. In the end, I have wrapped it up in a bash script (export-custom-packages). Have a look and use it if you wish. It

  • scans the Apt history.log as well as the archived logs
  • lists only those packages which have been installed manually
  • excludes packages which have been installed, then uninstalled
  • creates and updates a separate log file for these packages, which prevents them from dropping off the radar once the archived Apt logs are purged.

"Setup":

sudo curl -o /usr/local/bin/export-custom-packages https://gist.githubusercontent.com/hashchange/7ec8185b2fce93b5ac490f4ae0809bda/raw/21b0646475da7cfeb5b7c7703b2238b336dfa3d7/export-custom-packages
sudo chmod +x /usr/local/bin/export-custom-packages

Usage: See export-custom-packages --help.

The annotated code is on Github.

hashchange
  • 276
  • 2
  • 6
0

The other answers helped but gave me too much output. To cut down on the output, I started with apt-mark showmanual as in this answer, and then filtered out packages originally installed (see this answer for how to get a list of packages originally installed; I'm using ubuntu 18.04.2 hence the link below).

BASE_PACKAGES_MANIFEST=http://releases.ubuntu.com/18.04.2/ubuntu-18.04.2-desktop-amd64.manifest
REMOVE_VERSIONS_REGEX='[0-9][.-][0-9][.-][0-9]|[0-9][.-][0-9]|[0-9]'
paste  <( apt-mark showmanual ) <( apt-mark showmanual | sed -r "s/$REMOVE_VERSIONS_REGEX//g" ) |
    grep -vf <( curl $BASE_PACKAGES_MANIFEST | cut -f1 | sed -r "s/$REMOVE_VERSIONS_REGEX|:amd//g" ) |
    cut -f1 |
    sort |
    uniq

The script filters out packages that were in the original manifest by doing a version-independent comparison, so that upgraded packages don't appear in the list. I ended up with a list of about 60 packages.

The other way I like is this answer that searches all the apt logs.

TooTone
  • 103
  • 4
-1

To get the list of most recent installed packages in descending order, I like using (e.g. 25 lines):

tac /var/log/apt/history.log |head --lines=25
Hartmut Pfarr
  • 157
  • 1
  • 3