Listing all installed packages with name, version, size and unique

Identify files not owned by any package

Periodic checks for files outside of pacman database are recommended. These files are often some 3rd party applications installed using the usual procedure (e.g. ./configure && make && make install). Search the file-system for these files (or symlinks) using this simple script:

Note that one should not delete all files listed in non-db.txt without confirming each entry. There could be various configuration files, logs, etc., so use this list responsibly and only proceed after extensively searching for cross-references using grep.

Removing orphaned packages

For recursively removing orphans:

# pacman -Rs $(pacman -Qtdq)

The following function is easily inserted into $HOME/.bashrc and removes orphans if found:

Note: If there is a package that needs to be removed from the repository, read up on repo-remove.

Once the local repository has been made, add the repository to pacman.conf. The name of the db.tar.gz file is the repository name. Reference it directly using a file:// url, or access it via FTP using ftp://localhost/path/to/directory.

Network shared pacman cache

In order to share packages between multiple computers, simply share /var/cache/pacman/ using any network-based mount protocol. This section shows how to use shfs or sshfs to share a package cache plus the related library-directories between multiple computers on the same local network. Keep in mind that a network shared cache can be slow depending on the file-system choice, among other factors.

Then, to share the actual packages, mount /var/cache/pacman/pkg from the server to /var/cache/pacman/pkg on every client machine.

To have shared package databases, mount /var/lib/pacman/sync/{core,extra,testing,community} in the same way. Proceed to place the appropriate lines in /etc/fstab.

Preventing unwanted cache purges

By default, pacman -Sc removes package tarballs from the cache that correspond to packages that are not installed on the machine the command was issued on. Because pacman cannot predict what packages are installed on all machines that share the cache, it will end up deleting files that should not be.

To clean up the cache so that only outdated tarballs are deleted, add this entry in the [options] section of /etc/pacman.conf:

CleanMethod = KeepCurrent

Backing up and retrieving a list of installed packages

It is good practice to keep periodic backups of all pacman-installed packages. In the event of a system crash which is unrecoverable by other means, pacman can then easily reinstall the very same packages onto a new installation.

First, backup the current list of non-local packages:

$ comm -23 <(pacman -Qeq|sort) <(pacman -Qmq|sort) > pkglist

Store the pkglist on a USB key or other convenient medium or gist.github.com or evernote or dropbox.

Copy the pkglist file to the new installation, and navigate to the directory containing it.

Issue the following command to install from the backup list:

# pacman -S $(< pkglist)

In the case you have a list which was not generated like mentioned above, there may be foreign packages in it (i.e. packages not belonging to any repos you have configured, or packages from the AUR).

In such a case, you may still want to install all available packages from that list:

List downloaded packages that are not in base or base-devel

The following command will list any installed packages that are not in base/base-devel, and as such were likely installed manually by the user:

comm -23 <(pacman -Qeq|sort) <(pacman -Qgq base base-devel|sort)

Reinstalling all installed packages

If you mess up your system (rm -rf) you can repair by having pacman reinstall all of your packages.

If your system does not contain any foreign (AUR) packages you can run:

# pacman -Qeq | pacman -S -
# pacman -Qdq | pacman -S --asdeps -

If you have foreign packages this will error as packages will not be found in the repositories. The following will make a list of all packages and remove the foreign packages seen with pacman -Qmq. Combining a command to list all packages, and another to hide the list of foreign packages is required.

The following will reinstall every package found in the repositories, preserving asdeps/explicitly installed info:

Restore pacman's local database

pacman -Q gives absolutely no output, and pacman -Syu erroneously reports that the system is up to date.

When trying to install a package using pacman -S package it outputs a list of already satisfied dependencies.

Most likely, pacman's database of installed software, /var/lib/pacman/local, has been corrupted or deleted. While this is a serious problem, it can be restored by following the instructions below.

Firstly, make sure pacman's log file is present:

$ ls /var/log/pacman.log

If it does not exist, it is not possible to continue with this method. You may be able to use Xyne's package detection script to recreate the database. If not, then the likely solution is to re-install the entire system.

To avoid downloading and/or processing packages that are present in the system's local database (or whatever remains of it), optionally copy it into the temporary location:

$ cp -r /var/lib/pacman/local "${dbpath}"

Generate the temporary local recovery database from the previously generated pkglist:

$ recovery-pacman -S --nodeps --needed $(< pkglist)

Note: Because --noscriptlet is needed, the files owned by packages in the fake root will not be representative of a real install.

After revising the database, conclude by copying it to the real destination:

# cp -r "${dbpath}"/local /var/lib/pacman

Finally update the local database so that packages that are not required by any other package are marked as explicitly installed and the other as dependences. You will need be extra careful in the future when removing packages, but with the original database lost is the best we can do.

Recovering a USB key from existing install

If you have Arch installed on a USB key and manage to mess it up (e.g. removing it while it is still being written to), then it is possible to re-install all the packages and hopefully get it back up and working again (assuming USB key is mounted in /newarch)