Arch :: Pacman

Pacman

configuration

EDIT /etc/pacman.conf

# more pacman utilities
pacman-contrib

Install and Update the package database

pacman -Syu # update
pacman -Sy  #sync but not update
pacman -Syu package # update system and install package
pacman -Syy # update database
pacman -Syyu # force update
pacman -Syyuu # force fresh, update and allow downgrade
pacman -U /var/cache/pacman/pkg/package_name.tar.xz # install from local
pacman -Sw package # download only

#install from a list
pacman -Syu --needed - < pkglist.txt

## AUR
# prerequisites
pacman -Syu --needed base-devel
git clone https://aur.archlinux.org/package_name.git
cd package_name
less PKGBUILD
# -s resolve dependencies, -i install, -r remove dependencies after build, -c clean temp files
makepkg -sirc
# upgrade
git pull
# repeat makepkg

Search and list info

# -Q local database; -S synced database; -F file database
pacman -Ss string # search a package in repo - both name and description
pacman -F string # search file name, e.g. pacman in /usr/bin/pacman 
pacman -Sg xorg # search package group
pacman -Si package # list information of a package
pacman -Sii package # including backup files and their modification states

pacman -Qs package # search installed package
pacman -Qi package  # information about an installed package
pacman -Qii package # including backup files and their modification states

pacman -Ql package # list files in the installed package
pacman -Fl package # list files in a remote package; use -Fy to sync first

pacman -Qo /path/to/filename # find out which package owns a file
pacman -F /path/to/file # which remote package a file belongs to

pacman -Qqe # list all installed packages
pacman -Qe # list explicitly installed packages
pacman -Qet # all packages explicitly installed and not required as dependencies

pacman -Qdt # check for packages that were installed as a dependency but now, no other packages depend on them

pacman -Qm # foreign packages
pacman -Qn # native packages

# save explicitly installed packages to a list
pacman -Qqen > pkglist.txt

Remove

pacman -R package
pacman -Rs package # Remove unneeded dependencies
pacman -Rsu package # removing a group which contains otherwise needed packages
pacman -Rc package # remove ALL dependencies
pacman -Rn package # remove back up configuration
pacman -Rns package # remove unneeded dependencies and config but not dotfiles

pacman -Qdt # list Orphans
pacman -Rns $(pacman -Qdtq) # remove all Orphans

pacman -Sc # cleaning cache that no longer installed
pacman -Scc # cleaning all cache
paccache -rvk3 # remove old package cache except for the latest three package version

Reset packages

#Change all "as explicitly" packages to "as dependency": 
pacman -D --asdeps $(pacman -Qqe)

#change only essential packages to "as explicitly" - add ones that not to be removed
pacman -D --asexplicit base linux linux-firmware

#remove all orphans
pacman -Qtdq | pacman -Rns -
pacman -Rns $(pacman -Qtdq) # alternative

Hooks

# default hooks
/usr/local/share/libalpm/hooks

# custom hooks, defined in pacman.conf
# sorted by number, must end with .hook
/etc/pacman.d/hooks/100-custom.hook 

# sample
[Trigger] # repeatable
Operation = Install|Upgrade|Remove #repeatable
Type = Path|Package 
Target = <Path|PkgNmae> # repeatable

[Action]
Description = ...
When = PreTransaction|PostTransaction
Exec = <Command>
Depends = <PkgName> # optional
AbortOnFail # (Optional, PreTransaction only)
NeedsTargets # Optional

Issues

# pacman locked
rm -f /var/lib/pacman/db.lck

# update after a long time
# update archlinux-keyring first
pacman -Sy archlinux-keyring && pacman -Syyu

# clean caches
paccache -r # remove and keep the last three
paccache -rk1 # keep one past version
paccache -ruk0 # limit the action to uninstalled packages, and keep zero version

# fix corrupted package
rm -r /var/lib/pacman/local/python-3.9.6-1
pacman -U --dbonly /var/cache/pacman/pkg/python-3.9.6-1-x86_64.pkg.tar.zst
pacman -Syu

Mirrors

curl -s "https://archlinux.org/mirrorlist/?country=US&protocol=https&use_mirror_status=on" | sed -e 's/^#Server/Server/' -e '/^#/d' | rankmirrors -n 5 - > /etc/pacman.d/mirrorlist

# pacman hook
remove mirrorlist.pacnew after pacman update
run mirrorlist update manually

# set up systemd-timer
# /etc/systemd/system/
pacman-mirrorlist.service
[Unit]
Description=Update pacman mirrorlist
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/bin/sh -c '/usr/bin/curl -s "https://archlinux.org/mirrorlist/?country=US&protocol=https&use_mirror_status=on" | /usr/bin/sed -e "s/^#Server/Server/" -e "/^#/d" | /usr/bin/rankmirrors -n 5 - > /etc/pacman.d/mirrorlist'

[Install]
WantedBy=multi-user.target

pacman-mirrorlist.timer
[Unit]
Description=Update pacman mirrorlist monthly

[Timer]
OnCalendar=monthly
Persistent=true

[Install]
WantedBy=timers.target

PKGBUILD

# three ways to generate sha256sum
updpkgsums # pacman-contrib needed
makepkg --geninteg
sha256sum FILE # add to PKGBUILD manually

# change source file name
source=("$pkgname-$pkgver.tar.gz::https://links-to/v$pkgver.tar.gz")

# build from source
prepare() {
    cd "$pkgname-$pkgver"
    patch -p1 -i "$srcdir/$pkgname-$pkgver.patch"
}

build() {
    cd "$pkgname-$pkgver"
    ./configure --prefix=/usr
    make
}

package () {
    cd "$pkgname-$pkgver"
    make DESTDIR="$pkgdir/" install
}

# build from a precompiled binary file
package() {
    cd "$srcdir/"

    install -Dm755 $pkgname "$pkgdir/usr/bin/$pkgname"
    install -m755 -t "$pkgdir/etc/$pkgname.conf" dot.conf
}

# install - install is really just to copy files and set attr
install [OPTION]... [-T] SOURCE DEST
install [OPTION]... SOURCE... DIRECTORY
install [OPTION]... -t DIRECTORY SOURCE...
install [OPTION]... -d DIRECTORY...

install -d # create directory with ALL arguments
install -D # create directory with the leading arguments except the last
install -m644 # set permission; -o set owner; -g set group
# example
install -Dm755 ${pkgname} "${pkgdir}/usr/bin/${pkgname}"
install -dm755 "$pkgdir/etc/$pkgname"
install -m644 dot.conf "$pkgdir/etc/$pkgname/$pkgname.conf"

## to use post_install script
# create file.install
# include in PKGBUILD: install=file.install

post_install() {
    # obtain log name
    local name=$(logname)
    local homed=$(cat /etc/passwd | grep $name | cut -d: -f6)
    install -Dm600 -o $name -t target-dir src-files
}

post_upgrade() {
    post_install $1
}