Difference between revisions of "Pacman/Tips and tricks"

From ArchWiki
Jump to: navigation, search
(update templates, see Help:Style)
(cleaned and simplified)
Line 11: Line 11:
 
{{Article summary end}}
 
{{Article summary end}}
  
==General==
+
==Cosmetic and Convienence==
''Basic pacman modifications and improvements''
+
 
+
 
===Color output===
 
===Color output===
 
The most effective method of colorizing pacman is installing {{AUR|pacman-color}} from the [[AUR]].
 
The most effective method of colorizing pacman is installing {{AUR|pacman-color}} from the [[AUR]].
Line 19: Line 17:
 
{{Note|The package installs a separate pacman binary patched for colored output (pacman-color), so you may want to use an [[alias]].}}
 
{{Note|The package installs a separate pacman binary patched for colored output (pacman-color), so you may want to use an [[alias]].}}
  
Besides that, there are numerous scripts and hacks devised by members of the Arch Linux community:
+
===Shortcuts===
 
+
The following instructions allow users to run some of the more common pacman commands without the need to type them fully via a script alias.
====Simple Bash script====
+
Colorized pacman search output:
+
<nowiki>
+
pacs() {
+
local CL='\\e['
+
local RS='\\e[0;0m'
+
 
+
echo -e "$(pacman -Ss "$@" | sed "
+
/^core/ s,.*,${CL}1;31m&${RS},
+
/^extra/ s,.*,${CL}0;32m&${RS},
+
/^community/ s,.*,${CL}1;35m&${RS},
+
/^[^[:space:]]/ s,.*,${CL}0;36m&${RS},
+
")"
+
}
+
</nowiki>
+
 
+
To use it simply type {{Ic|pacs}} instead of {{Ic|pacman -Ss}}.
+
 
+
====Python script====
+
[[RepoSearch|This one]] also searches the [[AUR]].
+
 
+
====Using acoc====
+
There is another, more general possibility of colorizing arbitrary command output. Download the small [http://www.ruby-lang.org/en/ Ruby] tool [http://raa.ruby-lang.org/project/acoc/ acoc], and its requirements, [http://raa.ruby-lang.org/project/ansicolor/ term-ansicolor] and [http://raa.ruby-lang.org/cache/ruby-tpty/ tpty]. Some applications like {{Ic|ls}} will not run without tpty because they need to be started from a terminal (or pseudo terminal, in this case), or else they behave differently.
+
 
+
Installation is relatively straightforward:
+
$ tar xf tpty-0.0.1.tar.gz
+
$ cd tpty-0.0.1
+
$ ruby extconf.rb
+
$ make
+
$ ruby ./test.rb
+
# make install
+
 
+
$ tar xf term-ansicolor-1.0.1.tar.gz
+
$ cd term-ansicolor-1.0.1
+
# ruby install.rb
+
 
+
And now acoc itself:
+
$ tar xf acoc-0.7.1.tar.gz
+
$ cd acoc-0.7.1
+
# make install
+
 
+
Now, just read the section ''Advanced Installation'' in acoc's {{ic|INSTALL}} file, and configure acoc as prefered. Create a link for pacman as well, since that is primarily what this is being done for.
+
 
+
Once acoc runs, optionally add these lines to {{ic|acoc.conf}}:
+
[pacman -Si]
+
/^Name\s+:\s([\w.-]+)/                                  bold
+
[pacman -Qi]
+
/^Name\s+:\s([\w.-]+)/                                  bold
+
[pacman -Qi$]
+
/^([\w.-]+)\s([\w.-]+)/                                bold,clear
+
[pacman -Ss]
+
/^([\w.-]+)\/([\w.-]+)\s+([\w.-]+)/                    clear,bold,clear
+
[pacman -Qs]
+
/^([\w.-]+)\/([\w.-]+)\s+([\w.-]+)/                    clear,bold,clear
+
[pacman -Sl]
+
/^([\w.-]+)\s([\w.-]+)\s([\w.-]+)/                      clear,bold,clear
+
[pacman -Qo]
+
/^([\w.-\/]+)\sis\sowned\sby\s([\w.-]+)\s([\w.-]+)/    clear,bold,clear
+
[pacman -Qe$]
+
/^([\w.-]+)\s([\w.-]+)/                                bold,clear
+
[pacman -Qg$]
+
/^([\w.-]+)\s([\w.-]+)/                                clear,bold
+
 
+
The above lines make pacman print all package names in bold, which is particularly helpful when doing, e.g. {{Ic|pacman -Ss xfce}}. If desiring a more colorful output, modify the lines to suit. Read the acoc documentation contained in the source package for more information.
+
 
+
===Aliases===
+
The following instructions allow users to run some of the more common pacman commands without the need to type them fully.
+
  
 
====Configure the shell====
 
====Configure the shell====
Line 141: Line 72:
 
* You may want to get the list of installed packages sorted by size, which may be useful when freeing space on your hard drive.
 
* You may want to get the list of installed packages sorted by size, which may be useful when freeing space on your hard drive.
 
   pacman -Qi | awk '/^Name/ {pkg=$3} /Size/ {print $4$5,pkg}' | sort -n
 
   pacman -Qi | awk '/^Name/ {pkg=$3} /Size/ {print $4$5,pkg}' | sort -n
 +
 +
==Maintenance==
 +
''House keeping, in the interest of keeping a clean system and following [[The Arch Way]]''
 +
 +
===CacheClean to selectivly purge cache===
 +
[[CacheClean]] is a Python script to clean the {{ic|/var/cache/pacman/pkg}} directory while allowing to specify how many package versions should be retained. <sup>[http://bbs.archlinux.org/viewtopic.php?id=9104]</sup>
 +
 +
A corresponding {{AUR|Cacheclean}} package is also available from the [[AUR]].
 +
 +
===Listing all installed packages with size===
 +
Invoke pacgraph with the -c option to produce a list of all installed packages with their respective sizes on the system.  Pacgraph is available from [community].
 +
 +
====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:
 +
{{hc|pacman-disowned|2=
 +
<nowiki>
 +
#!/bin/sh
 +
 +
tmp=${TMPDIR-/tmp}/pacman-disowned-$UID-$$
 +
db=$tmp/db
 +
fs=$tmp/fs
 +
 +
mkdir "$tmp"
 +
trap  'rm -rf "$tmp"' EXIT
 +
 +
pacman -Qlq | sort -u > "$db"
 +
 +
find /bin /etc /lib /sbin /usr \
 +
  ! -name lost+found \
 +
  \( -type d -printf '%p/\n' -o -print \) | sort > "$fs"
 +
 +
comm -23 "$fs" "$db"
 +
</nowiki>
 +
}}
 +
 +
To generate the list:
 +
$ pacman-disowned > non-db.txt
 +
 +
Note that one ''should not'' delete all files listed in {{ic|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 {{Ic|grep}}.
 +
 +
===Removing orphaned packages===
 +
For ''recursively'' removing orphans:
 +
# pacman -Rs $(pacman -Qtdq)
 +
 +
The following function is easily inserted into {{ic|$HOME/.bashrc}} and removes orphans if found:
 +
 +
<pre>orphans() {
 +
  if [[ ! -n $(pacman -Qdt) ]]; then
 +
    echo no orphans to remove
 +
  else
 +
    sudo pacman -Rs $(pacman -Qdtq)
 +
  fi
 +
}</pre>
 +
 +
===Removing everything but base group===
 +
If it is ever necessary to remove all packages except the base group, try this one liner:
 +
# pacman -Rs $(comm -23 <(pacman -Qeq|sort) <((for i in $(pacman -Qqg base); do pactree -ul $i; done)|sort -u|cut -d ' ' -f 1))
 +
 +
Source: [https://bbs.archlinux.org/viewtopic.php?id=130176 Look discussion here]
 +
 +
Notes:
 +
# {{ic|comm}} requires sorted input otherwise you get e.g. 'comm: file 1 is not in sorted order' error.
 +
# {{ic|pactree}} prints the package name followed by what it provides e.g.
 +
{{hc|$ pactree -lu logrotate|<nowiki>
 +
logrotate
 +
popt
 +
glibc
 +
linux-api-headers
 +
tzdata
 +
dcron cron
 +
bash
 +
readline
 +
ncurses
 +
gzip</nowiki>}}
 +
The 'dcron cron' line seems to cause problems, that is why {{ic|cut -d ' ' -f 1}} is needed - to keep just the package name.
  
 
==Installation and recovery==
 
==Installation and recovery==
Line 355: Line 361:
 
  # pacman -D --asdeps $(pacman -Qq)
 
  # pacman -D --asdeps $(pacman -Qq)
 
  # pacman -D --asexplicit $(pacman -Qtq)
 
  # pacman -D --asexplicit $(pacman -Qtq)
 
===Removing everything but base group===
 
If it is ever necessary to remove all packages except the base group, try this one liner:
 
# pacman -Rs $(comm -23 <(pacman -Qeq|sort) <((for i in $(pacman -Qqg base); do pactree -ul $i; done)|sort -u|cut -d ' ' -f 1))
 
 
Source: [https://bbs.archlinux.org/viewtopic.php?id=130176 Look discussion here]
 
 
Notes:
 
# {{ic|comm}} requires sorted input otherwise you get e.g. 'comm: file 1 is not in sorted order' error.
 
# {{ic|pactree}} prints the package name followed by what it provides e.g.
 
{{hc|$ pactree -lu logrotate|<nowiki>
 
logrotate
 
popt
 
glibc
 
linux-api-headers
 
tzdata
 
dcron cron
 
bash
 
readline
 
ncurses
 
gzip</nowiki>}}
 
The 'dcron cron' line seems to cause problems, that is why {{ic|cut -d ' ' -f 1}} is needed - to keep just the package name.
 
  
 
===Recovering a USB key from existing install===
 
===Recovering a USB key from existing install===
Line 384: Line 368:
 
===Extracting contents of a .pkg file===
 
===Extracting contents of a .pkg file===
 
The .pkg files ending in .xz are simply tar'ed archives that can be decompressed using "$ tar -Jxvf package.tar.xz". If you want to extract a couple of files out of a .pkg file, this would be a way to do it.
 
The .pkg files ending in .xz are simply tar'ed archives that can be decompressed using "$ tar -Jxvf package.tar.xz". If you want to extract a couple of files out of a .pkg file, this would be a way to do it.
 
==Maintenance==
 
''House keeping, in the interest of keeping a clean system and following [[The Arch Way]]''
 
 
===Miscellaneous procedures===
 
For ''recursively'' removing orphans (''be careful''):
 
# pacman -Rs $(pacman -Qtdq)
 
 
To get a sorted list of local packages and their size:
 
$ LC_ALL=C pacman -Qi | sed -n '/^Name[^:]*: \(.*\)/{s//\1 /;x};/^Installed[^:]*: \(.*\)/{s//\1/;H;x;s/\n//;p}' | sort -nk2 | column -t
 
 
Alternatively, pacgraph when invoked with the -c option will produce a similar list.  Pacgraph is available from [community].
 
 
====Getting a list of 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:
 
{{hc|pacman-disowned|2=
 
<nowiki>
 
#!/bin/sh
 
 
tmp=${TMPDIR-/tmp}/pacman-disowned-$UID-$$
 
db=$tmp/db
 
fs=$tmp/fs
 
 
mkdir "$tmp"
 
trap  'rm -rf "$tmp"' EXIT
 
 
pacman -Qlq | sort -u > "$db"
 
 
find /bin /etc /lib /sbin /usr \
 
  ! -name lost+found \
 
  \( -type d -printf '%p/\n' -o -print \) | sort > "$fs"
 
 
comm -23 "$fs" "$db"
 
</nowiki>
 
}}
 
 
To generate the list:
 
$ pacman-disowned > non-db.txt
 
 
Note that one ''should not'' delete all files listed in {{ic|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 {{Ic|grep}}.
 
 
===Selective cache purge===
 
[[CacheClean]] is a Python script to clean the {{ic|/var/cache/pacman/pkg}} directory while allowing to specify how many package versions should be retained. <sup>[http://bbs.archlinux.org/viewtopic.php?id=9104]</sup>
 
 
A corresponding {{AUR|Cacheclean}} package is also available from the [[AUR]].
 

Revision as of 13:46, 17 January 2012

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

Summary help replacing me
This is a collection of common tips for new pacman users.
Related
pacman
Mirrors
Creating Packages
Custom local repository

Cosmetic and Convienence

Color output

The most effective method of colorizing pacman is installing pacman-colorAUR from the AUR.

Note: The package installs a separate pacman binary patched for colored output (pacman-color), so you may want to use an alias.

Shortcuts

The following instructions allow users to run some of the more common pacman commands without the need to type them fully via a script alias.

Configure the shell

Add the following examples, which work in both Bash and Zsh:

# Pacman alias examples
alias pacupg='sudo pacman -Syu'        # Synchronize with repositories before upgrading packages that are out of date on the local system.
alias pacin='sudo pacman -S'           # Install specific package(s) from the repositories
alias pacins='sudo pacman -U'          # Install specific package not from the repositories but from a file 
alias pacre='sudo pacman -R'           # Remove the specified package(s), retaining its configuration(s) and required dependencies
alias pacrem='sudo pacman -Rns'        # Remove the specified package(s), its configuration(s) and unneeded dependencies
alias pacrep='pacman -Si'              # Display information about a given package in the repositories
alias pacreps='pacman -Ss'             # Search for package(s) in the repositories
alias pacloc='pacman -Qi'              # Display information about a given package in the local database
alias paclocs='pacman -Qs'             # Search for package(s) in the local database
# Additional pacman alias examples
alias pacupd='sudo pacman -Sy && sudo abs'     # Update and refresh the local package and ABS databases against repositories
alias pacinsd='sudo pacman -S --asdeps'        # Install given package(s) as dependencies of another package
alias pacmir='sudo pacman -Syy'                # Force refresh of all package lists after updating /etc/pacman.d/mirrorlist

Usage

Perform the respective commands by simply typing the alias name. For example, to synchronize with repositories before upgrading packages that are out of date on the local system:

$ pacupg

Install packages from repositories:

$ pacin <package1> <package2> <package3>

Install a custom built package:

$ pacins /path/to/<package>

Completely remove a locally installed package:

$ pacrem <package>

Search for available packages in the repositories:

$ pacreps <keywords>

Display information about a package (e.g. size, dependencies) in the repositories:

$ pacrep <keywords>

Notes

The aliases used above are merely examples. By following the syntax samples above, rename the aliases as convenient. For example:

alias pacrem='sudo pacman -Rns'
alias pacout='sudo pacman -Rns'

In the case above, the commands pacrem and pacout both call Bash to execute the same command.

Operations and Bash syntax

In addition to pacman's standard set of features, there are ways to extend its usability through rudimentary Bash commands/syntax.

  • To install a number of packages sharing similar patterns in their names -- not the entire group nor all matching packages; eg. kdemod:
 pacman -S kdemod-{applets,theme,tools}
  • Of course, that is not limited and can be expanded to however many levels needed:
 pacman -S kdemod-{ui-{kde,kdemod},kdeartwork}
  • Sometimes, -s's builtin ERE can cause a lot of unwanted results, so it has to be limited to match the package name only; not the description nor any other field:
 pacman -Ss '^vim-'
  • pacman has the -q operand to hide the version column, so it is possible to query and reinstall packages with "compiz" as part of their name:
 pacman -S $(pacman -Qq | grep compiz)
  • You may want to get the list of installed packages sorted by size, which may be useful when freeing space on your hard drive.
 pacman -Qi | awk '/^Name/ {pkg=$3} /Size/ {print $4$5,pkg}' | sort -n

Maintenance

House keeping, in the interest of keeping a clean system and following The Arch Way

CacheClean to selectivly purge cache

CacheClean is a Python script to clean the /var/cache/pacman/pkg directory while allowing to specify how many package versions should be retained. [1]

A corresponding CachecleanAUR package is also available from the AUR.

Listing all installed packages with size

Invoke pacgraph with the -c option to produce a list of all installed packages with their respective sizes on the system. Pacgraph is available from [community].

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:

pacman-disowned
#!/bin/sh

tmp=${TMPDIR-/tmp}/pacman-disowned-$UID-$$
db=$tmp/db
fs=$tmp/fs

mkdir "$tmp"
trap  'rm -rf "$tmp"' EXIT

pacman -Qlq | sort -u > "$db"

find /bin /etc /lib /sbin /usr \
  ! -name lost+found \
  \( -type d -printf '%p/\n' -o -print \) | sort > "$fs"

comm -23 "$fs" "$db"

To generate the list:

$ pacman-disowned > non-db.txt

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:

orphans() {
  if [[ ! -n $(pacman -Qdt) ]]; then
    echo no orphans to remove
  else
    sudo pacman -Rs $(pacman -Qdtq)
  fi
}

Removing everything but base group

If it is ever necessary to remove all packages except the base group, try this one liner:

# pacman -Rs $(comm -23 <(pacman -Qeq|sort) <((for i in $(pacman -Qqg base); do pactree -ul $i; done)|sort -u|cut -d ' ' -f 1))

Source: Look discussion here

Notes:

  1. comm requires sorted input otherwise you get e.g. 'comm: file 1 is not in sorted order' error.
  2. pactree prints the package name followed by what it provides e.g.
$ pactree -lu logrotate
logrotate
popt
glibc
linux-api-headers
tzdata
dcron cron
bash
readline
ncurses
gzip

The 'dcron cron' line seems to cause problems, that is why cut -d ' ' -f 1 is needed - to keep just the package name.

Installation and recovery

Alternative ways of getting and restoring packages

Installing packages from a CD/DVD/ISO

  • First mount the CD (replace cdrom with dvd if needed):
# mount /mnt/cdrom
If working with an .iso file instead, first create a directory under /mnt:
# mkdir /mnt/iso
Then mount the image:
# mount -t iso9660 -o ro,loop /path/to/iso /mnt/iso
  • Configure pacman:
# nano -w /etc/pacman.conf
  • Add the following before other repositories (e.g. extra, core, etc.). This ensures the files from the CD/DVD/iso take precedence over those in the standard repositories:
# Settings for using a cd-rom as a repository.
[custom]
Server = file:///mnt/cdrom/arch/pkg
Again, replace cdrom as appropiate.

Once pacman.conf has been edited, sync pacman in order to be able to use the new repository.

Using packages from an Arch core image

In order to use packages from an Arch core image (e.g. to set up wireless support while offline), mount the core repository:

# mount -o loop /path/to/arch_core_image/i686/repo-core-i686.sfs /mnt/iso

Then edit pacman.conf's [core] section, (temporarily) replacing the default entry with a reference to the mounted image:

[core]
#Include = /etc/pacman.d/mirrorlist
Server = file:///mnt/iso

Finally, sync Pacman:

# pacman -Syu

Custom local repository

pacman 3 introduced a new script named repo-add which makes generating a database for a personal repository much easier. Use repo-add --help for more details on its usage.

Simply store all of the built packages to be included in the repository in one directory, and execute the following command (where repo is the name of the custom repository):

$ repo-add /path/to/repo.db.tar.gz /path/to/*.pkg.tar.xz

Note that when using repo-add, the database and the packages do not need to be in the same directory. But when using pacman with that database, they should be together.

To add a new package (and remove the old if it exists), run:

$ repo-add /path/to/repo.db.tar.gz /path/to/packagetoadd-1.0-1-i686.pkg.tar.xz
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.

If willing, add the custom repository to the list of unofficial user repositories, so that the community can benefit from it.

Network shared pacman cache

Tip: See pacserve for an alternate solution.

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.

First, install any network-supporting filesystem; for example sshfs, shfs, ftpfs, smbfs or nfs

Tip: To use sshfs or shfs, consider reading Using SSH Keys.

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:

# pacman -S --needed $(diff <(cat badpkglist|sort) <(diff <(cat badpkglist|sort) <(pacman -Slq|sort)|grep \<|cut -f2 -d' ')|grep \<|cut -f2 -d' ')

Explanation:

  • pacman -Slq lists all available softwares, but the list is sorted by repository first, hence the sort command
  • Sorted files are required in order to make the diff command work.
  • The first diff returns all unavailable packages; the second all available packages.
  • The --needed switch is used to skip already installed packages.

You may also try to install all unavailable packages (those not in the repos) from the AUR using yaourt (not recommended unless you know exactly what you are doing):

$ yaourt -S --noconfirm $(diff <(cat badpkglist|sort) <(pacman -Slq|sort) |grep \<|cut -f2 -d' ')

Finally, you may want to remove all the packages on your system that are not mentioned in the list. Warning: use this command wisely, and always check the result prompted by pacman.

# pacman -Rsu $(diff <(cat badpkglist|sort) <(pacman -Qq|sort) | grep \>|cut -f2 -d' ')

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

# comm -23 <(pacman -Qeq) <(pacman -Qmq) | pacman -S -
# comm -23 <(pacman -Qdq) <(pacman -Qmq) | pacman -S --asdeps -

Restore pacman's local database

Signs that pacman needs a local database restoration:

  • 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.

Log filter script

Create a script with the following content based on [2]:

log2pkglist.awk
#!/bin/awk -f

$3 ~ /^(installed|upgraded)$/ {
  pkg[$4] = 1
  next
} 

$3 == "removed" {
  pkg[$4] = 0
} 

END {
  for (i in pkg) if (pkg[i]) print i
}

Make the script executable:

$ chmod +x log2pkglist.awk

Generating the package recovery list

Run the script and pipe the output to a temporary list:

$ ./log2pkglist.awk /var/log/pacman.log > pkglist.orig

Optionally edit pkglist.orig and remove anything that should not be re-installed. This might be the situation with custom packages made with ABS, for example.

Here is a way to automatically restrict the list to packages available in a repository:

$ { cat pkglist.orig; pacman -Slq; } | sort | uniq -d > pkglist

Check if some important base package are missing, and add them to the list:

$ comm -23 <(pacman -Sgq base) pkglist.orig >> pkglist

Proceed once the contents of pkglist are satisfactory, since they will be used it restore pacman's installed package database; /var/lib/pacman/local.

Performing the recovery

As a normal user, make temporary directories for the cache, database, and root:

tmp=~/tmp
mkdir -p "${tmp}"

pushd "${tmp}"
dbpath=$(readlink -f ./dbpath)
root=$(readlink -f ./root)
cache=$(readlink -f ./cache)
log=/dev/null
mkdir -p "${dbpath}" "${cache}" "${root}"
popd

recovery-pacman() {
  fakeroot pacman "$@"   \
    --dbpath "${dbpath}" \
    --root   "${root}"   \
    --cache  "${cache}"  \
    --log    "${log}"    \
    --noscriptlet        \
    #
}

Populate the temporary sync database:

$ recovery-pacman -Sy

or copy the system's sync database:

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

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.

# pacman -D --asdeps $(pacman -Qq)
# pacman -D --asexplicit $(pacman -Qtq)

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)

# pacman -S $(pacman -Qq --dbpath /newarch/var/lib/pacman) --root /newarch --dbpath /newarch/var/lib/pacman

Extracting contents of a .pkg file

The .pkg files ending in .xz are simply tar'ed archives that can be decompressed using "$ tar -Jxvf package.tar.xz". If you want to extract a couple of files out of a .pkg file, this would be a way to do it.