Saturday, March 6, 2021

Ubuntu package management cheat sheet (including apt/apt-get/apt-cache/APT preferences files)

This is my Ubuntu package management cheat sheet (including apt/apt-get/apt-cache/APT preferences files).

1. Location of APT cache
# repository metadata 
/var/lib/apt/lists/

# a cache of already downloaded packages to avoid downloading them again
/var/cache/apt/archives

# Downloading-in-progress packages
/var/cache/apt/archives/partial
2. Source List file
/etc/apt/sources.list
3. Clean /var/cache/apt/archives except lock file and partial folder
apt-get clean
4. Clean /var/cache/apt/archives for packages which can no longer be downloaded
apt-get autoclean

# Clean /var/cache/apt/archives for packages that were installed automatically to satisfy the dependencies of another installed package.
# If that package is removed, below commands can remove those automatically installed packages.
apt autoremove
apt-get autoclean
5. Downloads the package lists from the repositories
apt-get update
apt update
6. List upgradable or installed packages
apt list --upgradable
apt list --installed
apt list --all-versions
7. Search for a package in the local APT cache
apt-cache search package_name
apt-cache search --names-only package_name
apt-cache search --names-only --full package_name

apt search package_name
apt search --names-only package_name
apt search --names-only --full package_name
8. Get detailed metadata information of a package
apt-cache show package_name
apt show package_name
9. Show Package name, Versions and Dependencies
apt-cache showpkg package_name
10. Show whether package is installed, which version is available which which repository and priority
apt-cache policy package_name
apt policy package_name

Note: Default priority for installed package : 100, for non-installed package: 500.

11. Check dependencies
apt-cache depends package_name
apt depends package_name
12. Check reverse dependencies
apt-cache rdepends package_name
apt rdepends package_name
13. Check all unmet dependencies of available packages
apt-cache unmet
14. List all available packages
apt-cache pkgnames | wc -l
15. Upgrade installed packages
apt-get upgrade
apt upgrade

# Only stable distribution
apt -t stable upgrade

# full-upgrade may remove a installed obsolete package or install new dependencies if needed.
apt full-upgrade

16. Install a package

apt-get install package_name
apt install package_name

17. Install a package without upgrade

apt-get install package_name --no-upgrade
apt install package_name --no-upgrade

18. Only upgrade without installing it

apt-get install package_name --only-upgrade
apt install package_name --only-upgrade

19. Install specific version of package

apt-get install package_name=version_number
apt install package_name=version_number
20. Remove a package but leave configuration files
apt-get remove package_name
apt remove package_name
21. Remove a package including configuration files
apt-get purge package_name
apt purge package_name

22. Retrieve all installed packages into a file

dpkg --get-selections > pkg-list

23. Dump all available packages

apt-cache dumpavail

24. Install and Remove packages at the same time

apt install package1 package2-
apt remove package1+ package2

25. Reinstall a package if it is damaged

apt --reinstall install package_name

26. Install a specific distribution of package

apt install package_name/unstable
apt install package_name/experimental

27. Install a .deb

apt install ./package_name

28. Package priority

APT checks /etc/apt/preferences and /etc/apt/preferences.d/ by folllowing below rules:

< 0
will never be installed,
1..99
will only be installed if no other version of the package is already installed,
100..499
will only be installed if there is no other newer version installed or available in another distribution,
500....989
will only be installed if there is no newer version installed or available in the target distribution,
990..1000
will be installed except if the installed version is newer,
> 1000
will always be installed, even if it forces APT to downgrade to an older version.

29. Distribution

Stable, Testing, Unstable or Experimental...

30. Sample APT preferences files

# Allow installing experimental packages
Package: *
Pin: release a=experimental
Pin-Priority: 500

# Only install Stable packages of Debian
Package: *
Pin: release a=stable
Pin-Priority: 900

Package: *
Pin: release o=Debian
Pin-Priority: -10

# Keep Perl 5.24
Package: perl
Pin: version 5.24*
Pin-Priority: 1001

# High priority for local packages
Package: *
Pin: origin ""
Pin-Priority: 999

# High priority for Codename is "buster"
Package: *
Pin: release n=buster
Pin-Priority: 900

31. APT preference manual

man apt_preferences

32. Comments in /etc/apt/preferences

Explanation: xxx

33. why an automatically installed package is present on the system

aptitude why package_name

34. List installed packages that no other package depends on

deborphan

 

References:

https://debian-handbook.info/browse/stable/sect.apt-get.html#sect.apt.priorities
https://www.howtoforge.com/a-short-introduction-to-apt-pinning-p2


No comments:

Post a Comment

Popular Posts