pacman Tips

From ArchWiki

Jump to: navigation, search

This is a collection of common tips for new pacman users.

Contents

General

Basic pacman modifications and improvements

Color output

The most effective method of colorizing pacman is installing pacman-color. To install with yaourt:

$ yaourt -S pacman-color

Besides that, there are numerous scripts and hacks devised by members of the Arch Linux community:

Simple Bash script

Colorized pacman search output:

alias pacs="pacsearch"
pacsearch () {
       echo -e "$(pacman -Ss $@ | sed \
       -e 's#core/.*#\\033[1;31m&\\033[0;37m#g' \
       -e 's#extra/.*#\\033[0;32m&\\033[0;37m#g' \
       -e 's#community/.*#\\033[1;35m&\\033[0;37m#g' \
       -e 's#^.*/.* [0-9].*#\\033[0;36m&\\033[0;37m#g' )"
}

For a system-wide script, do:

# touch /usr/bin/pacs && chmod 755 /usr/bin/pacs

Then paste this into /usr/bin/pacs:

 #!/bin/bash
 echo -e "$(pacman -Ss $@ | sed \
 -e 's#core/.*#\\033[1;31m&\\033[0;37m#g' \
 -e 's#extra/.*#\\033[0;32m&\\033[0;37m#g' \
 -e 's#community/.*#\\033[1;35m&\\033[0;37m#g' \
 -e 's#^.*/.* [0-9].*#\\033[0;36m&\\033[0;37m#g' )"

To use it simply type pacs instead of pacman -S.

Python script

This one also searches the AUR.

Using 'acoc'

There is another, more general possibility of colorizing arbitrary command output. You can download the small Ruby tool acoc, and its requirements, term-ansicolor and tpty. Some applications like 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 xvzf tpty-0.0.1.tar.gz
$ cd tpty-0.0.1
$ ruby extconf.rb
$ make
$ ruby ./test.rb
# make install
$ tar xvzf term-ansicolor-1.0.1.tar.gz
$ cd term-ansicolor-1.0.1
# ruby install.rb

And now acoc itself:

$ tar xvzf acoc-0.7.1.tar.gz
$ cd acoc-0.7.1
# make install

Now, just read the section "Advanced Installation" in acoc's INSTALL file, and configure acoc as you want to. Create a link for "pacman" as well, since that is primarily what you are doing this for.

Once acoc runs, you can add these lines to your 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 just make pacman print all package names in bold, which is particularly helpful when doing, e.g., pacman -Ss xfce. If you like it more colorful, you can modify the lines as you want. 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

Add the following lines (works 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 -Up'         # install specific package not from the repositories as 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

You can now 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, type:

$ pacupg

To install a specific package from repositories, type:

$ pacin <packagename>

To install specific packages from repositories, type:

$ pacin <packagename> <packagename> <packagename>

To install a custom built package on your local machine, type:

$ pacins /path/to/<packagename>

To completely remove a locally installed package, type:

$ pacrem <packagename>

To search for available packages in the repositories, type:

$ pacreps <keywords>

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

$ pacrep <keywords>

Notes

The aliases used above are merely examples. By following the syntax samples above, you may opt to rename the aliases to your own liking. For example:

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

In the case above, the commands pacrem and pacrem both call Bash to execute the same command: sudo pacman -Rns.

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.

Custom local repository

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

This script is very easy to run, and also very easy to keep your DB up to date. Simply store all of the built packages you want in your repository in one directory, and execute the following command (where repo is the name of your custom repository):

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

The last argument will add all pkg.tar.gz files to your repository, so be careful--if you have multiple versions of a package in your directory, it is unclear which one will take precedence and end up in the repository.

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), simply run

repo-add /path/to/repo.db.tar.gz /path/to/packagetoadd-1.0-1-i686.pkg.tar.gz
Note: If there is a package that you do not want in your repository any longer, read up on repo-remove.

Once you have made a local repository, add the repository to your pacman.conf. The name of the db.tar.gz file is the repository name. You can reference it directly using a file:// url, or you can access it via ftp using ftp://localhost/path/to/directory.

If you can and are willing, add your user-repository to the list of unofficial user repositories, so that all other users can find and install your packages.

Network shared pacman cache

Note: As of version 3.1, pacman -Sc changed behavior. By default it does no longer remove all outdated package files but all package files which 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, you will end up deleting files you don't want to delete. If you want to clean up your cache to only keep the newest packages you will have to put CleanMethod = KeepCurrent in the [Options] section of /etc/pacman.conf.

In order to share packages between multiple computers you can simply share /var/cache/pacman/ using any network-based mount protocol. This section shows you 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: If you want to use sshfs or shfs you should consider reading Using SSH Keys.

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

If you also want to have shared package databases you will need to mount /var/lib/pacman/{core,extra,testing,community} in the same way. You may put the appropriate lines in your /etc/fstab file.

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 packages (which are available in a repository):
 $ comm -13 <(pacman -Qmq | sort) <(pacman -Qqe | sort) > pkglist
  • Store the pkglist on a USB key or other convenient medium.
  • 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 $(cat pkglist)

Redownloading all installed packages (minus AUR)

Issue the following command [1]:

pacman -Qqe | grep -vx "$(pacman -Qqm)" | xargs pacman -Sdw --noconfirm

Redownloading all installed packages that are not in /var/cache/pacman/pkg/:

pacman -Sdw --noconfirm `pacman -Q | awk '{ print $1 }'`
Note: pacman will show you the size of all installed packages, not just missed from /var/cache/pacman/pkg/

If you get an error because of too many command line arguments, use this command:

for package in `pacman -Q | awk '{ print $1 }'`; do pacman -Sdw --noconfirm $package; done
Tip: If you have powerpill installed, you can replace pacman with powerpill in the commands above to increase your download speed and alleviate the bandwidth load per server.

Restore pacman's local database

Signs that pacman needs a local database restoration:

  • pacman -Q gives absolutely no results, and pacman -Syu tells you that your system is up to date, but you know it is not.
  • When you try to install a package using pacman -S package, you are presented with a list of dependencies, even though you know that they are already installed.

Most likely, pacman's database of installed software, /var/lib/pacman/local, has been corrupted or deleted. This is a serious problem, but fortunately you can restore /var/lib/pacman/local by following the instructions below.

Instructions

  • Firstly, you have to make sure you have pacman's log file.
$ ls /var/log/pacman.log

If your pacman log file does not exist, you can not continue. The only option you have is to re-install your system from scratch.

  • Create the pkglist.sh file with the following content [2]:
awk '
  $3 == "installed" || $3 == "upgraded" { pkg[$4] = 1 } 
  $3 == "removed" { pkg[$4] = 0 } 
  END { for (i in pkg) if ( pkg[i] == 1 ) print i; }
' /var/log/pacman.log

Make the script executable:

$ chmod +x pkglist.sh
  • Now you run pkglist.sh and pipe the output to pkglist-orig.
$ ./pkglist.sh > pkglist-orig
  • pkglist now contains a list of all the software you installed or upgraded. Edit pkglist and remove anything that you do not want to re-install (optional). You might want to do this if you made a custom package and installed it 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) | sort | uniq -d > pkglist

You could also check if you are missing some of the important base packages, and eventually add them to the list:

$ pacman -Sgq base | grep -v "$(cat pkglist)"
  • Once you are satisfied with the contents of pkglist, you can use it to re-install your software, and restore /var/lib/pacman/local.

There is no need to check for dependencies, and you have to "force" the install because the programs already exist:

# pacman -Sdf --needed $(cat pkglist)

pacman will now present you with a long list of software to be installed. Say Yes and wait for pacman to finish.

  • Finally, you need to find all the configuration files that have changed. You can do this by first updating the locate database:
# updatedb
  • Then you can search for all the configuration files that have changed:
# locate pacorig

This will give you a list of all the configuration files that have been replaced. Your original file will have .pacorig appended to it. Delete the new files, and rename the .pacorig files to restore your original configuration for each software package that may be affected. Some directory permissions may also have been changed. Check this if something refuses to start.

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)

For reinstalling all packages on the system (which are available in a repository):

# pacman -S $(pacman -Qq | grep -v "$(pacman -Qmq)")

To get a sorted list of local packages and their size:

$ LANG=C pacman -Qi | sed -n '/^Name[^:]*: \(.*\)/{s//\1 /;x};/^Installed[^:]*: \(.*\)/{s//\1/;H;x;s/\n//;p}' | sort -nk2

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:

#!/bin/bash
time /usr/bin/pacman -Ql | cut -d ' ' -f 2- | sort -u > db.txt
time find /arch /bin /boot /etc /lib /opt /sbin /usr 2>/dev/null | while read i; do
    if [ ! -d "$i" ]; then
        echo "$i"
    fi;
done | sort -u > fs.txt
time comm -23 fs.txt db.txt > non-db.txt
 
#test it:
#cat non-db.txt | while read i; do pacman -Qo "$i" 2>&1; done | tee test.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.

Selective cache purge

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

Personal tools