Difference between revisions of "Pacman/Tips and tricks"

From ArchWiki
Jump to: navigation, search
m (Network shared pacman cache: updated the pkgd's url)
(powerpill development is discontinued)
Line 9: Line 9:
 
{{Article summary wiki|Creating Packages}}
 
{{Article summary wiki|Creating Packages}}
 
{{Article summary wiki|Custom local repository}}
 
{{Article summary wiki|Custom local repository}}
{{Article summary wiki|powerpill}}
 
 
{{Article summary end}}
 
{{Article summary end}}
 
==General==
 
==General==
Line 217: Line 216:
 
A way of achieving the above:
 
A way of achieving the above:
 
  # pacman -S $(comm -23 <(pacman -Qeq) <(pacman -Qmq))
 
  # pacman -S $(comm -23 <(pacman -Qeq) <(pacman -Qmq))
 
{{Tip|When [[powerpill]] installed, replace ''pacman'' with ''powerpill'' in the commands above to increase download speed and alleviate the bandwidth load per server.}}
 
  
 
===Restore pacman's local database===
 
===Restore pacman's local database===

Revision as of 19:59, 25 March 2011

Summary help replacing me
This is a collection of common tips for new pacman users.
Languages

Template:I18n entry

Related
pacman
Mirrors
Creating Packages
Custom local repository

General

Basic pacman modifications and improvements

Color output

The most effective method of colorizing pacman is installing Template:Package AUR from the AUR.

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

Simple Bash script

Colorized pacman search output:

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},
	")"
}

To use it simply type Template:Codeline instead of Template:Codeline.

Python script

This one also searches the AUR.

Using acoc

There is another, more general possibility of colorizing arbitrary command output. Download the small Ruby tool acoc, and its requirements, term-ansicolor and tpty. Some applications like Template:Codeline 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 Template:Filename 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 Template:Filename:

[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. Template:Codeline. 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

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 Template:Codeline and Template:Codeline 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 it's 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. Template:Package Official:
 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, Template:Codeline'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 Template:Codeline 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)

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 Template:Filename 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 Template:Codeline which makes generating a database for a personal repository much easier. Use Template:Codeline 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.gz

Note that when using Template:Codeline, 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.gz
Note: If there is a package that needs to be removed from the repository, read up on Template:Codeline.

Once the local repository has been made, add the repository to Template:Filename. The name of the Template:Filename 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 pkgd for an alternate solution.

In order to share packages between multiple computers, simply share Template:Filename 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 Template:Filename from the server to Template:Filename on every client machine.

To have shared package databases, mount Template:Filename in the same way. Proceed to place the appropriate lines in Template:Filename.

Preventing unwanted cache purges

By default, Template:Codeline 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 shouldn't be.

To clean up the cache so that only outdated tarballs are deleted, add this entry in the Template:Codeline section of Template:Filename:

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) <(pacman -Qmq) > 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 $(< pkglist)

Redownloading all installed packages (minus AUR)

Listing currently installed packages will output everything, including packages that are foreign and/or might not be available for installation any longer. So simply running:

# pacman -S $(pacman -Qeq)

will output errors because some (or many) of the packages were not found in the repositories. There needs to be a way to list only packages that can be installed from the repositories present in Template:Codeline's database. In order to do so, combining a command to list all packages, and another to hide the list of foreign packages is required.

A way of achieving the above:

# pacman -S $(comm -23 <(pacman -Qeq) <(pacman -Qmq))

Restore pacman's local database

Signs that pacman needs a local database restoration:

Most likely, pacman's database of installed software, Template:Filename, 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 [1]: Template:File

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 Template:Filename and remove anything that shouldn't 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 Template:Filename are satisfactory, since they will be used it restore pacman's installed package database; Template:Filename.

Performing the recovery

Note: this procedure has not been updated to use pacman's new -D option; however, it still produces the same results, just not optimally.

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 Template:Filename:

$ recovery-pacman -S --nodeps --needed $(< pkglist)
Note: Because Template:Codeline 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

Removing everything but base group

If it is ever necessary to remove all packages except the base group on a severely broken system, try this one liner:

# pacman -Rs $(comm -23 <(pacman -Qeq) <(pacman -Qgq base))

Copied from a message on the archlinux.org mailing list.

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's still being written to), then it's 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

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

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: Template:File

To generate the list:

$ pacman-disowned > non-db.txt

Note that one should not delete all files listed in Template:Filename 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 Template:Codeline.

Selective cache purge

Here is a Python script to clean the Template:Filename directory while allowing to specify how many package versions should be retained. [2]