I'd like to get a list of packages installed manually by apt or aptitude and be able to find out whether a foobar package was installed manually or automatically.
How can we do that from the command line?
I'd like to get a list of packages installed manually by apt or aptitude and be able to find out whether a foobar package was installed manually or automatically.
How can we do that from the command line?
You can use either of these two one-liners. Both yield the exact same output on my machine and are more precise than all solutions proposed up until now (July 6, 2014) in this question.
Using apt-mark:
comm -23 <(apt-mark showmanual | sort -u) <(gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Package: //p' | sort -u)
Using aptitude:
comm -23 <(aptitude search '~i !~M' -F '%p' | sed "s/ *$//" | sort -u) <(gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Package: //p' | sort -u)
Very few packages still fall through the cracks, although I suspect these are actually installed by the user, either right after the installation through the language localization setup or e.g. through the Totem codec installer. Also, the linux-header versions also seem to accumulate, even though I've only installed the non version-specific metapackage. Examples:
libreoffice-help-en-gb
openoffice.org-hyphenation
gstreamer0.10-fluendo-mp3
linux-headers-3.13.0-29
How does it work:
sed strips out remaining whitespace at the end of the line.Other possibilities don't work as well:
ubuntu-14.04-desktop-amd64.manifest file (here for Ubuntu 14.04) instead of /var/log/installer/initial-status.gz. More packages are shown as manually installed even though they are not.apt-mark showauto instead of /var/log/installer/initial-status.gz. apt-mark for example doesn't include the xserver-xorg package, while the other file does.I used various other StackExchange posts as references, however none work as well as the above solution:
Both list more packages than the above solution.
EDIT: What to do if you've upgraded from a previous release:
If you've upgraded Ubuntu from one release to the next, you will probably need to adjust this process. In that case, I would check the manifest file of the newer release (see above) in addition to the initial-status.gz file from the current release. You can easily do that by just adding another comparison. Using just the manifest file will not work, as the manifest file unfortunately does not contain everything that the initial_status.gz file does (I checked).
In newer versions of the package apt, there is also the apt-mark command
apt-mark showmanual
To get a list of all packages (not installed, installed by user or installed by default, across all PPAs), apt employs the following method:
apt list [option]
The possible options useful for this are:
--installed to display only the packages that are installed on the system (out of some 50,000+)
--manual-installed to list the packages that were explicitly installed by a command, either directly, or as dependencies.
Alternatively, you could do:
apt list --installed | grep -F \[installed\] to get a list of packages that resulted from user commands and their dependencies only, and to get additional information on them such as version and architecture supported (x86, x86_64, amd64, all, etc.)
For Ubuntu 16.04, check out the log file /var/log/apt/history.log.
For example:
zgrep 'Commandline: apt' /var/log/apt/history.log /var/log/apt/history.log.*.gz
It's not perfect, but it's pretty good at making it clear exactly what I installed by hand. Put a -B 1 on the grep to see when it was installed.
Example output
Commandline: apt install postgresql-9.5-plv8
Commandline: aptdaemon role='role-install-file' sender=':1.85'
Commandline: apt install task
Commandline: apt autoremove
Commandline: apt install atom
Commandline: apt upgrade
Commandline: apt-get install asciinema
Commandline: apt install iperf3
Commandline: apt upgrade
Commandline: apt-get install chromium-browser
Commandline: apt install joe cpanminus build-essential postgresql libdbd-pg-perl libcrypt-openssl-bignum-perl libcrypt-openssl-rsa-perl libio-socket-ssl-perl libnet-ssleay-perl libssl-dev
Commandline: aptdaemon role='role-commit-packages' sender=':1.2314'
Commandline: apt install git
Commandline: apt install sqlite
Commandline: apt install whois
Commandline: apt install libdbd-pg-perl
Commandline: apt install perl-doc
Commandline: apt upgrade
Not sure if this picks up aptitude or not. It doesn't seem to pick up installs from the Ubuntu Software desktop app.
apt-mark showauto | grep -iE '^foobar$' will output "foobar" if the package was installed automatically, nothing otherwise.
aptitude search '!~M ~i' will list the packages that were not installed automatically. It's a pity aptitude won't be part of the default install on Ubuntu Desktop starting from 10.10.
I would like to give a GUI solution.
Open Synaptic Package Manager.
Go to Status.
Click Installed (manual).
It will give the list of packages installed manually by apt or aptitude.
Unfortunately I could not find any option in Custom Filters to find out whether a foobar package was installed manually or automatically.
If the package is under Installed but not under Installed (manual), then it was installed automatically. If the package is under Installed (manual), then it was installed manually.
The following script will print out all the packages that are not set to automatic install and hence were installed manually:
#!/usr/bin/python
try:
import apt_pkg
except ImportError:
print("Error importing apt_pkg, is python-apt installed?")
sys.exit(1)
apt_pkg.init()
STATE_FILE = apt_pkg.config.find_dir("Dir::State") + "extended_states"
auto = set()
tagfile = apt_pkg.TagFile(open(STATE_FILE))
while tagfile.step():
pkgname = tagfile.section.get("Package")
autoInst = tagfile.section.get("Auto-Installed")
if not int(autoInst):
auto.add(pkgname)
print("\n".join(sorted(auto)))
it is based on how apt-mark prints out the automatically installed packages.
As several people have commented, apt-mark showmanual seems to be a bit buggy (and I reported it as bug 727799). When I'm using it, it actually reports a lot of stuff that isn't even logged in /var/lib/apt/extended_states (where this is supposed to be stored) and apt-get isn't logging things as installed in /var/lib/apt/extended_states (just in /var/lib/dpkg/status). The python script by txwikinger above draws from /var/lib/apt/extended_states directly but if you're using it today the syntax might not work (mine just started generating errors with Kubuntu 13.10). Updated syntax is:
#!/usr/bin/python
import sys
try:
import apt_pkg
except ImportError:
print "Error importing apt_pkg, is python-apt installed?"
sys.exit(1)
apt_pkg.init()
STATE_FILE = apt_pkg.config.find_dir("Dir::State") + "extended_states"
auto = set()
tagfile = apt_pkg.TagFile(open(STATE_FILE))
while tagfile.step():
pkgname = tagfile.section.get("Package")
autoInst = tagfile.section.get("Auto-Installed")
if not int(autoInst):
auto.add(pkgname)
print "\n".join(sorted(auto))
For me this was a very short list of 5 items which doesn't seem to be accurate either.
If you're in a VM (Virtual Machine), then the following might work better for you:
egrep 'apt(-get)? +install' /var/log/apt/history.log
That finds software you installed both via apt and apt-get. If you'd like to include aptitude too, this should work:
egrep 'apt(-get|itude)? +install' /var/log/apt/history.log
(But don't think this'd find packages installed directly via dpkg — which I don't really do anyway.)
Rationale: If the Virtual Machine already included some default software installed by the VM maintainers (in the base image), but not by you, then, this answer won't show the software they installed (well at least not in the VMs I'm using), only the software you installed.
The other answers here, which uses e.g. apt-mark showmanual or aptitude search '!~M ~i' or apt list --manual-installed, in my case, in a VM, showed 90% to me uninteresting packages that I didn't install — instead, some of the VM base image maintainers did (I presume).
An updated version of @jmiserez' answer:
comm -23 \
<(apt-mark showmanual | sort -u) \
<(grep -oP '^(?:Package|Depends):\s+\K.*' /var/log/installer/status \
| grep -oP '[^\s,()]+?(?=(?:\s+\([^)]+\))?+(?:,|$))' \
| sort -u)
The log is no longer stored at /var/log/installer/initial-status.gz and the original did not omit dependent packages. The list it produces will include packages you didn't manually install, but it's a more manageable length and still very useful.
This gets you a list of manual installed packages and its corresponding version
apt list --manual-installed | sed 's/\// /' | awk '{print $1 "=" $3}'
Compiled from the awesome answers by others in this thread and additional info from the internet I assembled a command that fits my needs and perfectly replicates my apt history:
ls /var/log/apt/history.log* | sort --version-sort | xargs -d '\n' zgrep -B 1 'Commandline: apt'
So, first of all, it outputs history in chronological order, e.g.
/var/log/apt/history.log.1
/var/log/apt/history.log.2
...
/var/log/apt/history.log.10
You can add --reverse param to the sort to sort files in reverse order. That way you'll have oldest commands on the top of the list.
Plus it has date of the command.
Kudos to @s1037989 and @iruvar
If no one gives you a nice answer using a apr-something command you can do it the hard way. Apt-get stores its info in /var/lib/apt/extended_states. Any file that is installed automatically will be added to this file. If you install a package already in this file manually, the package will remain in this file but with Auto-installed: 0 in the second line. It's not deleted.
Note: As expected better answers that are likely to work if file placement changes have appeared. I keep mine just in case the info on the file location is useful.
After googling a lot, I've managed to assemble this script. It works alright for me:
#!/bin/bash
List of all packages currently installed
current=$(dpkg -l | awk '{print $2}' | sort | uniq)
List of all packages that were installed with the system
pre=$(gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Package: //p' | sort | uniq)
List of packages that don't depend on any other package
manual=$(apt-mark showmanual | sort | uniq)
(Current - Pre) ∩ (Manual)
packages=$(comm -12 <(comm -23 <(echo "$current") <(echo "$pre")) <(echo "$manual") )
for pack in $packages; do
packname=$(echo $pack | cut -f 1 -d ":")
desc=$(apt-cache search "^$packname$" | sed -E 's/.* - (.*)/\1/')
date=$(date -r /var/lib/dpkg/info/$pack.list)
echo "# $desc"
echo "# $date"
echo "sudo apt-get install $pack"
echo -e ""
done
I have found an elegant method for doing this.
Just output the ~/.bash_history file with the grep command to sort them out:
cat .bash_history | grep "apt install"
Conveniently, you can look out for an apt-get too.
An example output, I have:
sudo apt install -f
sudo apt install vim
sudo apt install dconf
sudo apt install dconf-editors
sudo apt install dconf-editor
sudo apt install nmap
sudo apt install python-tk
sudo apt install python-tk
As Li Lo said, apt-mark showauto should get you a fat list of things automatically installed.
Now to show the things that are installed manually, it turns out there's a lovely simple search modifier for aptitude. But you don't want to to do that. You want to write a huge bash command that does some rocket science.
Note: This is more an illustration of how cool you'll look busting out massive bash commands to all your friends.
comm -3 <(dpkg-query --show -f '${Package} ${Status}\n' | \n
grep "install ok installed" | cut --delimiter=' ' -f 1) <(apt-mark showauto)
I broke it onto two lines for readability. What does this do?
apt-markNot sure if this is helpful, but to find packages that were recently installed manually by the current user, search the history. E.g., for bash:
$ history | egrep '\bapt\b'
Modify the grep to check for specific packages.
If you installed all your packages from the terminal using apt, you could throw a simple regex searching for apt install * at the contents of /var/log/apt/history.log and see what it gobbles up.
The regex might be as simple as: Commandline: apt install [\w -]+ (following the log syntax of apt on Ubuntu 16.04)
From Ubuntu 22.04 and onwards, you could take care of this using a more modern apt front-end called nala (for 22.04 you have to enable the "universe" repository).
sudo apt install nala
From this point, use nala to install new packages on your system, using:
sudo nala install <package-name>
Leave security updates etc. to the unattended-upgrades package.
Now, when you run the command nala history, you get a list of the commands run with nala, including installed packages. To filter only new installations, use:
nala history | grep install
An added bonus of the history feature is that you can undo any step in the history, so that you can reverse package installation procedures - something that is unnecessarily hard using apt itself.
Though not thoroughly tested, this answer of mine has the advantage of listing only the packages that were explicitly requested by users. Other answers seem to include also packages that were installed at the time of OS install.
The trick is to check the log file and look for the line starting with Requested-By:
$ cat /var/log/apt/history.log* | awk -v RS= '/Requested-By:/'
Start-Date: 2025-01-07 18:30:40
Commandline: apt install mdadm
Requested-By: **SOME_USER_NAME** (1000)
Install: libidn12:amd64 (1.41-1, automatic), liblockfile-bin:amd64 (1.17-1+b1, automatic), exim4-config:amd64 (4.96-15+deb12u6, automatic), psmisc:amd64 (23.6-1, automatic), exim4-base:amd64 (4.96-15+deb12u6, automatic), mdadm:amd64 (4.2-5), libevent-2.1-7:amd64 (2.1.12-stable-8, automatic), libunbound8:amd64 (1.17.1-2+deb12u2, automatic), ca-certificates:amd64 (20230311, automatic), exim4-daemon-light:amd64 (4.96-15+deb12u6, automatic), bsd-mailx:amd64 (8.1.2-0.20220412cvs-1, automatic), liblockfile1:amd64 (1.17-1+b1, automatic), libgnutls-dane0:amd64 (3.7.9-2+deb12u3, automatic), openssl:amd64 (3.0.15-1~deb12u1, automatic)
End-Date: 2025-01-07 18:31:00
You can then pipe the output to sed to get the package name:
$ cat /var/log/apt/history.log* | awk -v RS= '/Requested-By:/' | sed -n 's/Commandline: apt install //p'
mdadm
Limitation:
This lists all packages that were once installed by users. If they are uninstalled, they will still be in the output.
You could analyze the log further to list them correctly, although that is rather cumbersome. This is because a later apt install can re-install those packages again after removal, but this time as dependencies. You would have to write a sophisticated script for that. At that point you'd look for other answers that point at a dedicated Debian package for that.
On earlier Ubuntu versions, I was using this method for listing manually installed apt packages.
Unfortunately, the file /var/log/installer/initial-status.gz seems to have been removed from Ubuntu in version 24.04, so I was unable to use this method.
I searched through various places, and by combining various stuff, I found the following convoluted method.
Here is the entire shell command:
comm -23 <(comm -12 <(apt-mark showmanual | sort -u) \
<(debfoster -s -i | tail -n +2 | tr -s ' ' '\n' | sort -u)) \
<(cat <(zcat $(ls /var/log/apt/history.log.* | sort -V | xargs)) /var/log/apt/history.log |
zgrep -m 1 -oP 'Commandline: apt.*?install\s+\K.*' | tr -s ' ' '\n' | sort -u)
Explanation:
The command relies heavily on process substitution via <() construct.
comm -23 shows lines existing in the first input only.
comm -12 shows lines common to both inputs.
apt-mark showmanual | sort -u prints a list of manually installed packages. However this list contains more packages than one actually manually installs.
debfoster -s -i | tail -n +2 | tr -s ' ' '\n' | sort -u lists all orphaned packages. debfoster package must be installed using sudo apt install debfoster prior.
With the comm -12 operation, we obtain the manually installed packages which are also orphaned packages. This step is useful for removing certain packages (efibootmgr, grub-efi-amd64, etc.) from the final list.
Up to now, we constructed the first input to the outer comm -23 command.
The second input is a bit more involved, so it will be explained in parts:
cat <(zcat $(ls /var/log/apt/history.log.* | sort -V | xargs)) /var/log/apt/history.log |
zgrep -m 1 -oP 'Commandline: apt.*?install\s+\K.*' | tr -s ' ' '\n' | sort -u
apt stores its history logs under the /var/log/apt/ directory. The history.log file contains the latest history. Whereas history.log.*.gz files contain the archived logs.
cat <(zcat $(ls /var/log/apt/history.log.* | sort -V | xargs)) /var/log/apt/history.log command concatenates all these log files in chronological order, from oldest to newest.
zgrep -m 1 -oP 'Commandline: apt.*?install\s+\K.*' | tr -s ' ' '\n' | sort -u command then obtains all the packages installed via the first ever apt install command from the chronologically sorted logs.
From what I could infer, the first ever apt install command contains packages that were included in the base Ubuntu installation. This assumption may be incorrect, but I think it is reasonable for now, unless proven otherwise.
Finally, the outer comm -23 command shows the packages that are both manually installed and orphaned, but not included in the first ever apt install command.
On a fresh Ubuntu 24.04 install (with default selection, third-party software, and LVM partitioning enabled), the command output should be similar to the following:
debfoster
linux-generic-hwe-24.04
lvm2
nvidia-driver-550
shim-signed
ubuntu-restricted-addons
Which is the best I could manage.
Some other methods which did not work as well as expected:
libm17n-0 etc.)sudo apt-mark minimize-manual command does not seem to help much.I could only wish that this procedure could be simpler, but it does not seem to be possible for now.
This will list all manually installed packages without: dependencies, uninstalled packages, packages installed during system installation.
unopts() {
in=`cat`
echo "$in" | sed -r 's/ --[^ ]+//g;s/ -[^ ]+//g'
}
list() {
cat '/var/log/apt/history.log' |
grep --color=never -v '\-o APT::Status-Fd=4 \-o APT::Keep-Fds::=5 \-o APT::Keep-Fds::=6' |
egrep --color=never "Commandline: apt-get.* $1" |
sed -r "s/Commandline: apt-get//;s/ $1//" |
unopts |
tr ' ' '\n' |
sed '/^$/d'
}
hapt() {
tmp=`mktemp -d`
installed=$tmp/installed
deleted=$tmp/deleted
dpkg=$tmp/dpkg
list 'install' > $installed
list '(remove|purge|autoremove)' > $deleted
dpkg --get-selections |
grep -v 'deinstall' |
cut -f 1 > $dpkg
while read package
do
sed -i "0,/$package/{//d;}" $installed
done < $deleted
while read package
do
if [ -z "`grep --color=never "^$package$" $dpkg`" ]
then
sed -i "0,/$package/{//d;}" $installed
fi
done < $installed
cat $installed
rm -r $tmp
}