29

How can I view the history of apt-get install commands that I have manually executed?

It seems to me that all methods available show everything that has been installed right from the start of the Ubuntu installation.

How can I view the history of apt-get install since the time my system-installation process had completed?

TellMeWhy
  • 17,484
  • Well, yes. What else would you expect? Do you want to show only those you ran from the terminal and not those that were run by a GUI or something? Please [edit] your question and clarify. – terdon Oct 01 '15 at 11:39
  • @terdon Okay made a new edit – TellMeWhy Oct 01 '15 at 11:44
  • 1
    There are logs of dpkg in /var/log/dpkg.log*. – Velkan Oct 01 '15 at 11:55
  • You could also try just running history | grep "apt-get install". –  Oct 01 '15 at 12:27
  • 2
    @ParanoidPanda That fails in various ways. History truncations, things run in other shells, things removed from history, or never put there in the first place, things installed from scripts, things run by other users etc etc – Squidly Oct 01 '15 at 13:27
  • 1
    @MrBones: I know, and I have stated so in my answer (and in the comments below it). However the OP asked for a way to view all the instances when they have manually executed the apt-get install command to install a package, and I have provided a solution which gives this information unless the history file has been altered in certain ways (e.g.: the entries have been removed). –  Oct 01 '15 at 13:32
  • DevRobot please verify my answer as well ,whether you are getting your desired answer, – Ravan Oct 01 '15 at 16:41
  • 2

8 Answers8

26

I think the answer given here by kos is the best way I've seen so far. Though as Software Center uses apt, anything that it has installed would be listed too.

The command is:

zcat /var/log/apt/history.log.*.gz | cat - /var/log/apt/history.log | grep -Po '^Commandline: apt-get install (?!.*--reinstall)\K.*'
Arronical
  • 19,893
  • I'll test this out when I get home and accept it if it works :) – TellMeWhy Oct 01 '15 at 14:01
  • It's not! check out the update: http://askubuntu.com/a/680405/320386 – kos Oct 01 '15 at 16:56
  • Use this, is way more safer for this task: zcat /var/log/apt/history.log.*.gz | cat - /var/log/apt/history.log | grep -Po '^Commandline:(?=.* install ) \K.*' | sed '1,4d': grep -Po '^Commandline:(?=.* install ) \K.*' will filter only the apt-get commands containing install with a leading and trailing space, sed '1,4d' might be dependant on the specific installation; in mine 1,4 deletes exactly the number of entries coming from Ubiquity, which are those you want to remove. Let me know if the number is the same on your installation, that way at least we have a baseline :| – kos Oct 01 '15 at 18:10
  • @kos I get extra lines such as apt-get -o APT::Status-Fd=4 -o APT::Keep-Fds::=5 -o APT::Keep-Fds::=6 -q -y --no-remove install linux-generic with the new command. Are they the Ubiquity lines? – Arronical Oct 02 '15 at 08:29
  • No, those look like they came from Software Updater; there's no way to distinguish from commands run from the user and commands run by some apt-get frontend, aside from Ubiquity which runs only once (for example aptdaemon entries will be showed by that command as well). The best bet though is still to use that command, which at least excludes the Ubiquity entries. – kos Oct 02 '15 at 12:09
  • However just to know if deleting the first 4 lines is always safe, could you confirm that zcat /var/log/apt/history.log.*.gz | cat - /var/log/apt/history.log | grep -Po '^Commandline:(?=.* install ) \K.*' | sed -n '1,4p' outputs something similiar to this? (The first two entries don't wrap but they're massive). – kos Oct 02 '15 at 12:09
  • No it doesn't, it outputs only the first 4 lines of the 12 packages that I've installed. Each of these 4 lines starts with 'apt-get install' then just the package name. @kos – Arronical Oct 02 '15 at 12:28
  • Quite strange, maybe 15.04 is different (I'm assuming you're not running 15.04 because this is about the third time I reinstall 15.04 and the start of the very first log is always identical). Well then no luck, there's no way at all to parse those logs safely. Thanks – kos Oct 02 '15 at 12:43
  • Yeah I'm running 14.04, the command that I've copied in the answer works for me, but having only installed 12 package manually, I suppose I can't guarantee it'll work for everyone! – Arronical Oct 02 '15 at 12:46
  • 1
    You could also install with apt install, or use more than one space between words. – Pablo Bianchi Apr 19 '20 at 08:30
13

Just type following command in your terminal to view all install logs.

grep " install " /var/log/dpkg.log
sigjuice
  • 103
5

To simplify @Arronical answer, A neat trick I learned recently is you can use zcat -qf to cat both txt and txt gzipped files.

zcat /var/log/apt/history.log.*.gz | cat - /var/log/apt/history.log | grep " install "

becomes

zcat -qf /var/log/apt/history.log* | grep " install "

From man zcat:

   -q --quiet
          Suppress all warnings.
   -f --force
          Force  compression  or  decompression  even if the file has multiple links or the corre‐
          sponding file already exists, or if the compressed data is read from  or  written  to  a
          terminal.  If  the  input  data is not in a format recognized by gzip, and if the option
          --stdout is also given, copy the input data without change to the standard  output:  let
          zcat  behave  as  cat.  If -f is not given, and when not running in the background, gzip
          prompts to verify whether an existing file should be overwritten.
jjcf89
  • 153
  • 1
  • 6
3

Here is script which prints only the currently installed top level packages, where "top level packages" are defined as apt packages upon which no other apt packages depend. If such top level programs were installed by apt or a package manager such as Synaptic, then they were manually chosen by the user.

#!/bin/sh
NumDaysAgo=18
find /var/lib/dpkg/info -name "*.list" -mtime -$NumDaysAgo \
    -exec stat -c $'%y\t%n' {} \; | \
sed -e 's,/var/lib/dpkg/info/,,' -e 's,\.list,,' | \
sort -r | \
while read Date Time Xxx Pkg
do 
    lncnt=$(apt-cache --installed rdepends $Pkg | wc -l)
    if [ $lncnt -eq "2" ]
        then echo "$Date $Time $Pkg"
    fi
done
echo "JOB COMPLETED: $BASH_SOURCE"

The packages are printed in reverse order under the assumption that the user is more likely to want the newer info sooner, and because the program is slow.

Program flow:

  • The program first gathers into a list all the installed packages by reading the filenames under /var/lib/dpkg/info/. The file mod times are the installation times.
  • That list is sorted in reverse order.
  • For each installed package $Pkg, a call to apt-cache rdepends $Pkg requests the reverse-dependencies of $Pkg. If there are no dependencies, then it is a top level package and the package info is printed: date time packagename

Notes:

  • The script depends upon the output format of apt-cache rdepends $Pkg which was intended for human eyes and could change in the future versions of apt.
  • The code for the part gathering filenames under /var/lib/dpkg/info/ came from this unix.stackexchange post. As that poster 'mikel' pointed out, the dpgk history logfiles are not reliable because they will be rotated out after reaching a certain volume.
  • Man page for apt-cache
  • The call apt-cache rdepends ... is very slow presumably because each call is computed by iterating through all the dependencies. Hence the above script starts from the newest installs to offer the user as much instant gratification as possible.
  • The --installed flag after apt-cache checks that the dpkg-installed packages are also apt-installed. If the user or another install software bypassed apt and used dpkg directly, it would be possible. THIS CASE HAS NOT BEEN TESTED, but I think something noticeable would be printed in either the standard or error output
  • The output does not include manually chosen packages which later became depended upon by a higher package. The output can also include packages which were installed via apt by other third party install software, and hence are not truly manually installed. However, if the purpose of the output is as a basis for setting up a restored Linux from a backup /home directory which included said third party software, then this output would be suitable.
  • Some of the package names include version numbers, and some do not. Mentioned just to bring awareness to the fact.
Thomas Ward
  • 74,764
1

If you want history of apt-get install commands use the following command:

grep "apt-get install" .bash_history

Output:

ravan@ravan:~$ grep "apt-get install" .bash_history

sudo apt-get install --no-install-recommends ubuntu-mate-core ubuntu-mate-desktop
sudo apt-get install xfce4
sudo apt-get install xfce4.12
sudo apt-get install pgadmin
sudo apt-get install touchegg
sudo apt-get install aptitude
sudo apt-get install aptitude
sudo gedit .bash_history | grep "apt-get install" 
sudo apt-get installvim
grep "apt-get install" .bash_history
cat .bash_history | grep "apt-get install" 

For other information referExtra information.

There is also more detailed installation information in /var/log/apt/ in the history.log and history.log.X.gz files and term.log and term.log.X.gz files

If you want history of only apt-get included commands, then

history | grep apt-get

Ravan
  • 9,379
  • apt-mark showmanual shows all packages installed when the OS was installed too, rather than just those actually manually installed. It's an odd way for it to behave! – Arronical Oct 01 '15 at 12:42
  • 2
    This doesn't necessarily give specific details as to the history. What people need to look at is /var/log/apt/history.log and similar files. You only get the list of manually selected and autoselected packages. You don't give any chronological history of what was installed, what was removed, what was upgraded, etc. from this answer. – Thomas Ward Oct 01 '15 at 12:54
  • @Arronical - Where is the man page for apt -mark showmanual? I can't see it on the Ubuntu 16.04 LTS man page for apt: http://manpages.ubuntu.com/manpages/xenial/en/man8/apt.8.html. – Craig Hicks Aug 22 '17 at 05:44
  • @CraigHicks http://manpages.ubuntu.com/manpages/xenial/en/man8/apt-mark.8.html – Arronical Aug 22 '17 at 08:27
1

If you want see all of the things you have installed by running:

sudo apt-get install [package]

And you have not messed with the bash history, nor are you wanting to view the history of this sort of installation type for another user (or all users) then you can just run:

history | grep "apt-get install"

And that should get you mostly relevant results.

  • 1
    Will I get all the history with this? – TellMeWhy Oct 01 '15 at 13:10
  • @DevRobot: Unless you have manually altered the entries with the apt-get install in them in the .bash_history file, or deleted this file, the command will show you all the instances when you have executed a command with the string apt-get install within it on your user account (even if you run the command as root and therefore not exactly as your user account - that is running a command with sudo). –  Oct 01 '15 at 13:26
  • 2
    It might have a problem if you've entered more than 200 commands though – Arronical Oct 01 '15 at 13:51
  • 2
    @Arronical proof is expected when making claims ;-) – Rinzwind Oct 01 '15 at 14:03
  • @Arronical: Really? I did not know that there was a limit of 200 commands in the .bash_history file and there certainly isn't such a limit on my system. Could you please clarify what you mean. –  Oct 01 '15 at 14:08
  • 1
    I myself would assume 500 to be the default ( http://www.gnu.org/software/bash/manual/html_node/Bash-History-Facilities.html ). – Rinzwind Oct 01 '15 at 14:10
  • Oops I meant 2000 commands, which I think is the default number of commands displayed by the history command, but I may have imagined this! Looking now... – Arronical Oct 01 '15 at 15:40
  • history displays the number of commands, from your.bash_history file, specified by the $HISTSIZE environment variable. It seems to be set to 1000 as default in 14.04 server installations. @Rinzwind , cheers for getting me to check! – Arronical Oct 01 '15 at 15:47
0

Note that some arguments can be set before the install one, like the virtualmin script is doing during install: apt-get -y install packagename.

So if you want capture all install commands, you need to change the regex query.

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

In order to see what You've installed and removed, reinstalled, etc. More info about the journey of apt-get installs.

The answer can be modified into an alias:

alias apted='zcat /var/log/apt/history.log.*.gz | cat - /var/log/apt/history.log | fgrep Commandline: | cut -d " " -f 2- | grep -P "^|install"'
userDepth
  • 2,010
Mike
  • 101