What is /etc/apt/preferences ?

posted on
by wael

The /etc/apt/sources.list file contains one or more source from which apt will download Release files that contains the location of indices of binary and source packages . Each index contains records of packages that can be installed on the system.

The /etc/apt/preferences file is used to control the priority of a source distribution in the /etc/apt/sources.list , and it is also used to control the priority of individual or multiple binary packages .

Instead of using the /etc/apt/preferences file , we can also use the /etc/apt/preferences.d/ directory by placing multiple preferences files inside it , e.g. pref_file_one , preftwo … Files in the /etc/apt/preferences.d/ are parsed in an ascending alphanumeric order , and their name must contain only [a-z][A-Z][0-9]_-. and can have or not a .pref extension .

A priority is used to select the package that will be installed. The priority of a distribution applies to the binary packages selected from this distribution . Binary packages are selected from a distribution by using the /etc/apt/sources.list and by specifying the components and architectures.

For source packages , the priority is only affected by the target release . If the target release is not set , apt will download the latest version , else if it is set apt will download the target release version.

The /etc/apt/preferences file is best used when we have multiple distributions in /etc/apt/sources.list , for example if we have a source for the stable distribution and another source for the unstable one , we can use the /etc/apt/preferences file to control which package will be installed.

/etc/apt/preferences what is / How is

Default priorities assigned to distributions and binary packages

Each source in the /etc/apt/sources.list contains a distribution . We can set for a distribution its components , and architectures … A distribution will receive a default priority based on its Release file .

priority 1

If a distribution Release file contains NotAutomatic: yes and ButAutomaticUpgrades: no or if it contains only NotAutomatic: yes . For example the experimental distribution will receive a priority of 1.

			
Origin: Debian
Label: Debian
Suite: experimental
Codename: experimental
...
NotAutomatic: yes
...
							
priority 100

If a distribution Release file contains NotAutomatic: yes and ButAutomaticUpgrades: yes . For example the buster-backport distribution suite extension will receive a priority of 100.

			
Origin: Debian Backports
Label: Debian Backports
Suite: buster-backports
Codename: buster-backports
...
NotAutomatic: yes
ButAutomaticUpgrades: yes
...
							
priority 500

For all other distributions, for example the stable , testing and unstable distributions .

			
Origin: Debian
Label: Debian
Suite: stable
Version: 10.2
Codename: buster
...
							

Installed packages will receive a priority of 100 irrelative to the distribution that they came from .

Uninstalled packages will receive a priority of 1 if they come from a distribution that has in its Release file NotAutomatic: yes and ButAutomaticUpgrades: no or if the Release file contains only NotAutomatic: yes . They will receive a priority of 100 if they come from a distribution that has in its release file NotAutomatic: yes and ButAutomaticUpgrades: yes , and they will receive a priority of 500 for all other cases.

For example this /etc/apt/sources.list file , specify:

  • The main component for the unstable and testing distributions.
  • The main and the non-free components for the stable distribution .
  • The main component for the stable-backports and stable-updates distribution extensions.
  • The amd64 and i386 architecture for the main component of the experimental distribution.
# stable distribution
deb http://deb.debian.org/debian/ stable main non-free

# stable distribution security updates
deb http://security.debian.org/debian-security stable/updates main non-free

# stable-updates distribution extension
deb http://deb.debian.org/debian/ stable-updates main

# testing distribution
deb http://deb.debian.org/debian/ testing main

# unstable distribution
deb http://deb.debian.org/debian/ unstable main

# exprimental distribution
deb [ arch=amd64,i386 ] http://deb.debian.org/debian/ experimental main

# stable-backports distribution
deb http://deb.debian.org/debian/ stable-backports main
					

We can use the apt-cache policy command in order to view the default priorities assigned to each one of these distributions .

			
@debian:~$ apt-cache policy
# get the priorities of distributions and of packages.
Package files:

 100 /var/lib/dpkg/status
     release a=now
# /var/lib/dpkg/status contains packages status.
# For example a package can have a status of :
# deinstall ok config-files
#	The package is selected for deinstallation ,
#	it is in a know state ,  
#	only the configuration file are kept on the system .
# Or for example , It can have a status of :
# install ok installed
#	The package is selected for installation , 
#	it is in a known state, 
#	and it has been unpacked and configured.
# The package that are installed on the system
# have a priority of 100 .


 100 http://deb.debian.org/debian stable-backports/main amd64 Packages
     release o=Debian Backports,a=buster-backports,n=buster-backports,l=Debian Backports,c=main,b=amd64
     origin deb.debian.org
# The stable-backports distribution extension 
# has a priority of 100 because its Release file contains
# NotAutomatic: yes and ButAutomaticUpgrades: yes .
# The default architecture for stable-backports  was not set 
# in /etc/apt/sources.list :
# 	deb http://deb.debian.org/debian/ stable-backports main
# as such the the default  architecture that is 
# selected is the architecture  that apt 
# was compiled for , in this case it is amd64.
# We also selected the main component , hence
# the selected packages are the 
# amd64 binary packages from the main 
# component . 
# The default priority of 
# 100 will be applied to them , since they 
# belong to the stable-backports distribution .


   1 http://deb.debian.org/debian experimental/main i386 Packages
     release o=Debian,a=experimental,n=experimental,l=Debian,c=main,b=i386
     origin deb.debian.org
   1 http://deb.debian.org/debian experimental/main amd64 Packages
     release o=Debian,a=experimental,n=experimental,l=Debian,c=main,b=amd64
     origin deb.debian.org
# The experimental distribution main component i386 and amd64
# architectures have a priority of 1 ,
# because its Release file contains
# only NotAutomatic: yes .


 500 http://deb.debian.org/debian unstable/main amd64 Packages
     release o=Debian,a=unstable,n=sid,l=Debian,c=main,b=amd64
     origin deb.debian.org
# The unstable distribution main component amd64 architecture has a 
# priority of 500 , because its Release file does not 
# contain NotAutomatic: yes  .


 500 http://deb.debian.org/debian testing/main amd64 Packages
     release o=Debian,a=testing,n=bullseye,l=Debian,c=main,b=amd64
     origin deb.debian.org
# The testing distribution main component amd64 architecture has a 
# priority of 500 , because its Release file does not 
# contain NotAutomatic: yes .


 500 http://deb.debian.org/debian stable-updates/main amd64 Packages
     release o=Debian,a=stable-updates,n=buster-updates,l=Debian,c=main,b=amd64
     origin deb.debian.org
# The stable-updates distribution extension main component 
# amd64 architecture has a 
# priority of 500 , because its Release file does not 
# contain NotAutomatic: yes .


 500 http://security.debian.org/debian-security stable/updates/non-free amd64 Packages
     release v=10,o=Debian,a=stable,n=buster,l=Debian-Security,c=non-free,b=amd64
     origin security.debian.org
 500 http://security.debian.org/debian-security stable/updates/main amd64 Packages
     release v=10,o=Debian,a=stable,n=buster,l=Debian-Security,c=main,b=amd64
     origin security.debian.org
# The stable distribution main and non-free 
# components , amd64 architecture  from the 
# http://security.debian.org/debian-security 
# URI have a priority of 500 , because its Release 
# file does not contain NotAutomatic: yes .

 500 http://deb.debian.org/debian stable/non-free amd64 Packages
     release v=10.2,o=Debian,a=stable,n=buster,l=Debian,c=non-free,b=amd64
     origin deb.debian.org
 500 http://deb.debian.org/debian stable/main amd64 Packages
     release v=10.2,o=Debian,a=stable,n=buster,l=Debian,c=main,b=amd64
     origin deb.debian.org
# The stable distribution main and non-free 
# components , amd64 architecture  , from the 
# http://deb.debian.org/debian
# URI have a priority of 500 , because its 
# Release file does not contain NotAutomatic: yes .


Pinned packages:

					

The target or the default release

The target or the default release will receive a priority of 990 .

We can set the target or the default release by using the /etc/apt/apt.conf file or by placing a configuration file inside the /etc/apt/apt.conf.d/ directory.

For example , if we have an /etc/apt/sources.list file with the following content :

		
# buster distribution
deb http://deb.debian.org/debian/ buster main
deb-src http://deb.debian.org/debian/ buster main


# buster distribution security updates
deb http://security.debian.org/debian-security buster/updates main
deb-src http://security.debian.org/debian-security buster/updates main


# buster-updates distribution extension
deb http://deb.debian.org/debian/ buster-updates main
deb-src http://deb.debian.org/debian/ buster-updates main


# buster-backports distribution
deb http://deb.debian.org/debian/ buster-backports main
deb-src http://deb.debian.org/debian/ buster-backports main
					

We can set the buster-backports distribution to be the default or target release , by creating a file named 09defaultrelease with the content of :

		
#/etc/apt/apt.conf.d/09defaultrelease file
APT::Default-Release "buster-backports";
# Set the default or target release to buster-backports,
# it will have a priority of 990 .
# We can use the suite  , the codename or the version
# to specify the target release , e.g :
# stable , buster , 10
					

This will make the buster-backports distribution extension the target or the default release , and as such it will have a priority of 990 .

		
@debian:~$ apt-cache policy
# get the priorities of distributions and of packages.
Package files:


 100 /var/lib/dpkg/status
     release a=now
# /var/lib/dpkg/status contains the status
#	of packages on the local machine .
# The priorities of installed packages 
# is 100 .


 990 http://deb.debian.org/debian buster-backports/main amd64 Packages
     release o=Debian Backports,a=buster-backports,n=buster-backports,l=Debian Backports,c=main,b=amd64
     origin deb.debian.org
# The distribution extension buster-backports has a 
# priority of 100 , because its Release file contains
# NotAutomatic: yes and ButAutomaticUpgrades: yes .
# We have set this distribution to be the target release or 
# the default release , as such it will have a priority of
# 990 .


 500 http://deb.debian.org/debian buster-updates/main amd64 Packages
     release o=Debian,a=stable-updates,n=buster-updates,l=Debian,c=main,b=amd64
     origin deb.debian.org
# The distribution extension buster-updates has a 
# priority of 500 , because its Release file does not 
# contain NotAutomatic: yes .


 500 http://security.debian.org/debian-security buster/updates/main amd64 Packages
     release v=10,o=Debian,a=stable,n=buster,l=Debian-Security,c=main,b=amd64
     origin security.debian.org
 500 http://deb.debian.org/debian buster/main amd64 Packages
     release v=10.2,o=Debian,a=stable,n=buster,l=Debian,c=main,b=amd64
     origin deb.debian.org
# The distribution buster has a 
# priority of 500 , because its Release file does not 
# contain NotAutomatic: yes .


Pinned packages:

					

When installing a package from the command line, we can use the -t distribution option of apt-get to set the target release. This will give the specified distribution a priority of 990 , and it will temporarily override the target or default release set in apt configuration files.

	
apt-get install -t (distribution) package-name	
# apt-get : handles package installation ,
#    updates ...
# install : command for apt-get to 
#    install a package .
# -t :  option for apt-get to select 
#    the target release . The target release 
#    will have a priority of 990.
# (distribution) :  the distribution Suite ,
#    or Codename or version  from which to install a 
#    a package. For example experimental , 
#    or buster-backports or 10 ...
#    We cannot use oldstable-backports , 
#    stable-backports or testing-backports with 
#    this option. Instead we must use the code 
#    name. For example the current codename 
#    for stable is buster , so 
#    instead of using stable-backports
#    we must use buster-backportrs .
# package-name : the package-name that we 
#    want to install.
# We can use su or sudo to become root user 
#    before installing a package  .
					

For example using the previous /etc/apt/sources.list file , and when installing the redis package , we can override the default release set in /etc/apt/apt.conf.d/09defaultrelease and which is buster-backports and set it to buster as follow :

	
@debian:~$ apt-get -t buster install redis
# When we use the -t option from apt-get , 
# the target distribution is overridden , 
# and it will become the buster distribution. 
# The buster distribution will receive
# a priority of 990 . 

					

We can also set the target release per package by using :

	
apt-get install  package/distribution	
# package  : The name of the package that we 
#	want to install.
# distribution :  Can be the distribution code name , 
#	version , or suite . The distribution suite 
#	cannot be oldstable-backports , stable-backports 
#	or testing-backports.
					

If we set the target release per package , the package does not receive a priority but it will be selected regardless of anything else. For example :

	
@debian:~$ apt-get install redis/buster-backports
# The redis package from the buster-backports 
# distribution extension will be selected for 
# installation  regardless of anything else. 
					

/etc/apt/preferences record formats

The /etc/apt/preferences file is formed of one or more record. A record is formed of fields , and it can either be specific or general . A general record , has the Value * for the field Package , whereas a specific record does not. For both the general and the specific records, the Package and the Pin-Priority fields are obligatory.

The general record format is used to define the priority of a distribution or any of its components and architectures.

The specific form is used to define the priority for one or more binary packages.

In addition to the obligatory Pin-Priority and Package fields we must specify at least one of the fields for both the specific and the general format :

  • - Pin : version : The binary package version . It can be specified only for the specific form.
  • - Pin : origin : The origin , which means in this case the host name . It can be specified for both the general and the specific form. The information found in Pin : origin takes precedence over the information found in Pin : version.
  • - Pin : release : The release information such as a distribution suite name , code name , version , component , label , architecture and origin . Origin in this case is the author of the distribution . This field can be specified for both the general and the specific form. The information found in Pin : release takes precedence over the information found in both Pin : origin and Pin : version.

All record fields , beside Pin : version and Pin : release v=version can contain shell style wildcards for example * . , or POSIX extended regular expressions enclosed by slashes for example /.?a/ .

Defining the priority is also known as pinning .

Package

Defines the binary package name we wish to select in order to assign a priority to .

If the binary package name is * then this is the general format , else this is the specific format . In the specific format , we can use more than one package names separated by a space .

			
# /etc/apt/preferences
Package : *
Pin : release a=stable 
Pin-Priority : 1001
# Package : * 
#	This is the general form .
# Pin : release a=stable  
#	It applies to the stable distribution.
# Pin-Priority = 1001 
#	The priority that we want to 
#	assign to the stable distribution
#	is 1001 . 
# #### example one ####


# /etc/apt/preferences
Package : eclipse*
Pin : version 6.*     
Pin-Priority : 700
# Package : eclipse*
#	This is the specific form,
#	it applies only to a binary package 
#	that starts with eclipse and
#	end with any characters .
#	We used shell styled wildcards , to define
#	the name of the package.
# Pin : version 6.* 
#	We want to assign a priority 
#	to the version that starts with 6.
#	and that ends with any characters.
# Pin-Priority : 700
#	The pinned priority that we want to 
#	assign is 700 .
debian$ apt-cache policy
# check the priority of the pinned packages.
Package files:
 100 /var/lib/dpkg/status
     release a=now
...
Pinned packages:
     eclipse-titan -> 6.5.0-1+b1 with priority 700
# #### example two ####


# /etc/apt/preferences
Package : /eclipse/  python3
Pin : version *     
Pin : release n=buster 
Pin-Priority : 600
# Package : /eclipse/  python3
#	This is the specific form .
#	Select all the binary packages that contain
#	eclipse and select the binary package
#	that has the name python3.
#	We used POSIX extended regular expression , 
#	and we used the name of package .
# Pin : release n=buster 
#	The binary packages must belong to the 
#	distribution which has a code name  buster.
# Pin-Priority : 600
#	Assign a priority of 600 to the selected binary
#	packages.
debian$ apt-cache policy
# Check the priority of the pinned packages.
Package files:
 100 /var/lib/dpkg/status
     release a=now
...
   libeclipse-e4-ui-workbench3-java -> 0.14.200+eclipse4.10-1 with priority 600
   libeclipse-e4-ui-progress-java -> 0.2.100+eclipse4.10-1 with priority 600
   python3 -> 3.7.3-1 with priority 600
...
# #### example three ####

							
Pin : version

Defines the versions of binary packages to select , e.g 0.7-4+b1 .

This does not apply to the general form , it can only be used in the specific form.

			
# /etc/apt/preferences
Package : /.*/
Pin : version /~bpo/
Pin-Priority : 990
# Package : /.*/
#    Select all the binary packages , 
#    This is the specific form.
# Pin : version /~bpo/  
#	That have a version number 
#	which contains ~bpo .
# Pin-Priority = 990 
#	The priority that we want to 
#	assign to the selected 
#	packages is 990 . 
debian$ apt-cache policy
# check the priority of the pinned binary packages.
Package files:
 100 /var/lib/dpkg/status
     release a=now
...
Pinned packages:
     libebook1.2-dev -> 3.30.5-1.1~bpo10+1 with priority 990
     gir1.2-lokdocview-0.1 -> 1:6.3.4-2~bpo10+1 with priority 990
     libnvidia-glvkspirv -> 430.64-4~bpo10+1 with priority 990
...
# #### example one ####
							
Pin : origin

Defines a hostname to be selected , this applies to both the general and the specific form . e.g security.debian.org .

The information found in Pin : origin takes precedence over the information found in Pin : version.

			
# /etc/apt/preferences
Package : *
Pin : origin security.debian.*
Pin-Priority : 500
# Package : *
#    This the general form .
# Pin : origin security.debian.*  
#	Select the distributions , in the 
#	source list that has an
#	origin that starts with security.debian.
#	and ends with any characters .
# Pin-Priority = 500 
#	Assign a priority , to the
#	selected distributions ,
#	of 500 .
# #### example one ####


# /etc/apt/preferences
Package : redis
Pin : version 5:6*
Pin : origin security*
Pin-Priority : 600
# Package : redis
#	Select the package named 
#	redis.
# Pin : version 5:6* 
#	Select the package named redis 
#	that have a version number 
#	which starts with 5:6 and 
#	ends with anything .
# Pin : origin security.debian.*  
#	Select the package named redis , 
#	that has an origin of 
#	security.debian.*  
#	The information specified 
#	here takes precedence over
#	the information that is specified
#	in Pin : version
# Pin-Priority = 600 
#	Assign a priority , to the
#	selected package ,
#	of 600 .
debian$ apt-cache policy
# check the priority of the pinned binary packages.
Package files:
 100 /var/lib/dpkg/status
     release a=now
...
Pinned packages:
     redis -> 5:5.0.3-4+deb10u1 with priority 600
# The package that was selected has 
# a version number that starts with 5:5*
# and not with 5:6* , because the information
# specified in Pin : origin takes precedence over
# the one specified in Pin : version
# #### example two ####
							
Pin : release

Defines the release information . We can specify multiple options for a release. a=suite , n=codename , v=version , c=component , o=origin , l=label , b=architecture

All the options that are specified must apply , to be a match.

If we specify the same option for a release more than one time , for example a=suiteOne , a=suiteTwo , n=codename only the last one is taken , so in this example the options are actually a=suiteTwo , n=codename

The information found in Pin : release takes precedence over the information found in both Pin : origin and Pin : version.

We can use shell style wildcards , or POSIX extended regular expression , while defining a release in all options beside Pin : release v=version.

			
# /etc/apt/preferences
Package : *
Pin : release a=stable , a=buster-backports
Pin-Priority : 990
# Package : *
#    This the general form .
# Pin : release a=stable , a=buster-backports
#	Select only the buster-backports 
#	distribution and not the stable and
#	the buster-backports distributions.
# Pin-Priority = 990 
#	Assign a priority of 990 to the
#	selected buster-backports 
#	distribution. 
# #### example one ####


# /etc/apt/preferences
Package : /.*/
Pin : release a=/stable|buster-backports/ , origin=debian
Pin-Priority : 990
# Package : /.*/
#    This the specific form , 
#    select all the packages .
# Pin : release a=/stable|buster-backports/ , origin=Debian
#	Select the stable and buster-backports
#	distributions. The Release file must have 
#	its origin field equal to Debian. 
# Pin-Priority = 990 
#	Assign a priority to the 
#	selected packages from the
#	stable and buster-backports distributions
#	that have a Release file which have 
#	Debian in its origin field , a priority of
#	990 .
# #### example two ####


# #### The information that is specified in Pin : release ####
# #### takes precedence over the information that is ####
# #### specified in both Pin : origin and Pin : version ####
#/etc/apt/sources.list
deb http://security.debian.org/debian-security stable/updates main 
deb http://deb.debian.org/debian/ unstable main
# Let us say we have this /etc/apt/sources.list 
# file which contains both a source for the 
# stable and the unstable distribution . 
# The stable distribution has an origin of 
# security.debian.org , and the unstable distribution
# has an origin of deb.debian.org .
# /etc/apt/preferences
Package : *
Pin : version 1
Pin : origin security*
Pin : release a=unstable
Pin-Priority : 700
# Package : *
#    This the general form .
# Pin : version 1
#	Version information does 
#	not apply to the general form .
# Pin : origin security*
#	Select the distributions that 
#	has an origin which starts with 
#	security and ends with anything.
# Pin : release a=unstable
#	Select the distributions that have
#	in their release file a suite name 
#	of unstable. This takes precedence
#	over the origin , and over the 
#	package version . 
# Pin-Priority = 700 
#	Assign a priority to the 
#	unstable distribution of 700 . 
#	The stable distribution which 
#	has an origin of security.debian.org 
#	is not assigned any priority .
debian$ apt-cache policy
# check the priority of the pinned binary packages.
Package files:
 100 /var/lib/dpkg/status
     release a=now
 700 http://deb.debian.org/debian unstable/main amd64 Packages
     release o=Debian,a=unstable,n=sid,l=Debian,c=main,b=amd64
     origin deb.debian.org
 500 http://security.debian.org/debian-security stable/updates/main amd64 Packages
     release v=10,o=Debian,a=stable,n=buster,l=Debian-Security,c=main,b=amd64
     origin security.debian.org
Pinned packages:
# #### example two ####

 
							
Pin-Priority

Defines the priority of a package , or of a list of packages , or of a distribution or of any of a distribution architectures and components.

The priority can be a positive number or a negative number . If it is a negative number , it means not to select for installation . A value of 0 for a priority is illegal.

			
Package : *
Pin : release n=experimental , c=contrib
Pin-Priority : -100
# Package : *
#	This the general form . 
# Pin : release n=experimental , c=contrib
#	Select the contrib component from 
#	the experimental distribution  .
# Pin-Priority = -100
#	Assign a priority , to the
#	experimental distribution 
#	contrib component of -100 .
# #### example one ####

Package : redis
Pin : release n=sid , c=main , b=amd64
Pin-Priority : 990
# Package : redis
#	This the specific form , 
#	select the redis package.
# Pin : release n=sid , c=main , b=amd64
#	Select the redis package 
#	from the distribution which have 
#	a code name of sid , and a component
#	of main and an architecture of 
#	amd64 . 
# Pin-Priority : 990
#	Assign a priority , to the selected
#	redis package of 990.
# #### example two ####

							

Which priority is selected ?

The priority assigned to a distribution or to any of its architecture or components is assigned to the packages that belongs to this distribution .

If we have a package or a distribution , that have multiple default priorities , than the default priority that is assigned to the package or the distribution , is the highest default priority .

	
@debian:~$ apt-cache policy
redis:
  Installed: 5:5.0.3-4+deb10u1
  Candidate: 5:5.0.3-4+deb10u1
  Version table:
     5:5.0.7-1~bpo10+1 100
        100 http://deb.debian.org/debian buster-backports/main amd64 Packages
 *** 5:5.0.3-4+deb10u1 500
        500 http://deb.debian.org/debian buster/main amd64 Packages
        100 /var/lib/dpkg/status
# The redis package with version 5:5.0.3-4+deb10u1
# is installed , as such it has a default priority
# of 100 . 
# It is also part of the buster distribution
# as such it has also a default priority of 500 .
# The priority that is selected for this package
# is 500 , the highest of the two default priorities.
					

The target or the default release is assigned a priority of 990 , this will override any assigned default priority . We cannot override the target release priority in the /etc/apt/preferences file by using the general form .

	
#/etc/apt/sources.list	
deb http://deb.debian.org/debian/ stable main
# The etc/apt/sources.list file contains 
# only one source for the stable
# distribution . 


@debian:~$ apt-cache policy
# get the priorities of distributions and of packages.
Package files:
 100 /var/lib/dpkg/status
     release a=now
500 http://deb.debian.org/debian stable/main amd64 Packages
     release v=10.2,o=Debian,a=stable,n=buster,l=Debian,c=main,b=amd64
     origin deb.debian.org
Pinned packages:
# The default priority of the 
# stable distribution is 500. 


#/etc/apt/apt.conf.d/09defaultrelease file
APT::Default-Release "stable";
# We set the target release to stable.
# The target release has a priority of 
# 990.


@debian:~$ apt-cache policy
# get the priorities of distributions and of packages.
Package files:
 100 /var/lib/dpkg/status
     release a=now
 990 http://deb.debian.org/debian stable/main amd64 Packages
     release v=10.2,o=Debian,a=stable,n=buster,l=Debian,c=main,b=amd64
     origin deb.debian.org
# The target release priority overrides
# any default priority , as such the
# priority of the stable distribution is
# 990 .


#/etc/apt/preferences
Package : *
Pin : release a=stable 
Pin-Priority : 1000
# we have set the stable release
# priority to be 1000 . The stable
# release is the target release ,
# as such we cannot override its priority 
# in the /etc/apt/preferences file using
# the general form.


@debian:~$ apt-cache policy
# get the priorities of distributions and of packages.
Package files:
 100 /var/lib/dpkg/status
     release a=now
 990 http://deb.debian.org/debian stable/main amd64 Packages
     release v=10.2,o=Debian,a=stable,n=buster,l=Debian,c=main,b=amd64
     origin deb.debian.org
Pinned packages:
# The priority of the target release
# is 990 and is not overriden 
# by the general form . 
					

We can override the priority of a package or list of packages in the target or default release by using the specific form .

	
#/etc/apt/sources.list	
deb http://deb.debian.org/debian/ buster main
deb http://deb.debian.org/debian/ buster-backports main
# The etc/apt/sources.list file contains 
# two sources , one for the buster distribution ,
# and one for the buster-backports distribution
# extension . 


#/etc/apt/apt.conf.d/09defaultrelease file
APT::Default-Release "buster-backports";
# We set the target release to stable.
# The target release has a priority of
# 990.


@debian:~$ apt-cache policy redis
# get the priorities of distributions and of packages.
redis:
  Installed: (none)
  Candidate: 5:5.0.7-1~bpo10+1
  Version table:
     5:5.0.7-1~bpo10+1 990
        990 http://deb.debian.org/debian buster-backports/main amd64 Packages
     5:5.0.3-4+deb10u1 500
        500 http://deb.debian.org/debian buster/main amd64 Packages
# The redis package belong to the buster ,
# and to the buster-backports distribution.
# The redis package that belong to the 
# buster-backports distribution which is
# the target release has a priority of 990. 


#/etc/apt/preferences
Package : redis
Pin : release a=buster-backports 
Pin-Priority : -100
# The specific form can override the 
# priority of package or list of packages 
# that belong  to the target distribution.  
# We have set the priority of the 
# redis package that belongs to the 
# buster-backports distribution to 
# -100 .


@debian:~$ apt-cache policy redis
# get the priorities of distributions and of packages.
redis:
  Installed: (none)
  Candidate: 5:5.0.3-4+deb10u1
  Version table:
     5:5.0.7-1~bpo10+1 -100
        990 http://deb.debian.org/debian buster-backports/main amd64 Packages
     5:5.0.3-4+deb10u1 500
        500 http://deb.debian.org/debian buster/main amd64 Packages
# The redis package that belongs
# to the buster-backports distribution
# has now a priority of -100 , instead
# of having a priority of 990 .
					

The /etc/apt/preferences file general form will override the default priority of a distribution, as for the default priority of a package , the highest value of the two is selected . If we have more than one general form priority that matches the same release then the first one is selected .

	
#/etc/apt/sources.list	
deb http://deb.debian.org/debian/ buster main
# The etc/apt/sources.list file contains 
# only one source  for the buster
# distribution . 


@debian:~$ apt-cache policy
# get the priorities of distributions and of packages.
Package files:
 100 /var/lib/dpkg/status
     release a=now
 500 http://deb.debian.org/debian buster/main amd64 Packages
     release v=10.2,o=Debian,a=stable,n=buster,l=Debian,c=main,b=amd64
     origin deb.debian.org
Pinned packages:
# The buster distribution
# has a default priority of 500


#/etc/apt/preferences
Package : *
Pin : release a=stable 
Pin-Priority : 600
# Assign a priority of 600
# to the stable distribution .


@debian:~$ apt-cache policy 
# get the priorities of distributions and of packages.
Package files:
 100 /var/lib/dpkg/status
     release a=now
 600 http://deb.debian.org/debian buster/main amd64 Packages
     release v=10.2,o=Debian,a=stable,n=buster,l=Debian,c=main,b=amd64
     origin deb.debian.org
Pinned packages:
# Priority assigned in the /etc/apt/preferences
# file override the default release priority. 
# As such the priority for the stable
# distribution is 600 .


#/etc/apt/preferences
Package : *
Pin : release a=stable
Pin-Priority : 600

Package : *
Pin : release a=stable,n=buster
Pin-Priority : 100
# we have assigned a priority of 
# 600 to the buster distribution 
# and another priority of 100 
# to the buster distribution .
# Only the first priority is selected, 
# the other priority  is ignored . 


@debian:~$ apt-cache policy 
# get the priorities of distributions and of packages.
Package files:
 100 /var/lib/dpkg/status
     release a=now
 600 http://deb.debian.org/debian buster/main amd64 Packages
     release v=10.2,o=Debian,a=stable,n=buster,l=Debian,c=main,b=amd64
     origin deb.debian.org
Pinned packages:
# The priority that is assigned to the 
# buster distribution is 600 .
# This is the first priority from 
# the preferences file .


#/etc/apt/preferences
Package : *
Pin : release a=stable 
Pin-Priority : 50
# Assign a priority of 50
# to the stable distribution .


@debian:~$ apt-cache policy redis
# get the priorities of distributions and of packages.
redis:
  Installed: 5:5.0.3-4+deb10u1
  Candidate: 5:5.0.3-4+deb10u1
  Version table:
 *** 5:5.0.3-4+deb10u1 100
         50 http://deb.debian.org/debian buster/main amd64 Packages
        100 /var/lib/dpkg/status
# The version 5:5.0.3-4+deb10u1 of the redis
# package which belongs to the stable distribution 
# is installed , it has a priority of 100 . 
# We have assigned a priority of 50 to the stable 
# distribution using the /etc/apt/preferences file,
# as such the priority of the installed redis 
# package is also 50 .  
# The priority of the redis package is the highest 
# of the two priorities , as such it is 100.


#/etc/apt/preferences
Package : *
Pin : release a=stable 
Pin-Priority : 110
# Assign a priority of 110
# to the stable distribution .


@debian:~$ apt-cache policy redis
# get the priorities of distributions and of packages.
redis:
  Installed: 5:5.0.3-4+deb10u1
  Candidate: 5:5.0.3-4+deb10u1
  Version table:
 *** 5:5.0.3-4+deb10u1 110
        110 http://deb.debian.org/debian buster/main amd64 Packages
        100 /var/lib/dpkg/status
# The version 5:5.0.3-4+deb10u1 of the redis
# package which belongs to the stable distribution 
# is installed , it has a priority of 100 . 
# We have assigned a priority of 110 to the stable 
# distribution using the /etc/apt/preferences file,
# as such the priority of the installed redis 
# package is also 110 .  
# The priority of the redis package is the highest of 
# the two priorities , as such it is 110.

					

The /etc/apt/preferences file specific form overrides a general form priority , a target release priority , and any default priority . If we have more than one specific form priority that select the same package , then the first specific form priority is selected .

	
#/etc/apt/sources.list	
deb http://deb.debian.org/debian/ buster main
# The etc/apt/sources.list file contains 
# only one source for the buster
# distribution . 


#/etc/apt/preferences
Package : *
Pin : release a=stable
Pin-Priority : 70

Package : redis
Pin : release a=stable 
Pin-Priority : 50

Package : redis
Pin : release a=stable
Pin-Priority : 22

# In the /etc/apt/preferences file , 
# we have assigned a priority of 70
# to the stable distribution . 
# We have also assigned two priorities
# of 50 and 22 to the redis package
# from the stable distribution .


@debian:~$ apt-cache policy redis
# get the priorities of distributions and of packages.
redis:
  Installed: 5:5.0.3-4+deb10u1
  Candidate: 5:5.0.3-4+deb10u1
  Version table:
 *** 5:5.0.3-4+deb10u1 50
         70 http://deb.debian.org/debian buster/main amd64 Packages
        100 /var/lib/dpkg/status
# The redis package belongs to the stable distribution.
# The stable distribution has a default priority of 500.
# The general form override the default priority of
#	a distribution . We used the general form to 
#	assign a priority of 70 to the stable distribution.
#	The redis package from the stable distribution , 
#	will have the distribution priority of 70 . 
# The redis package has also a priority of 100 , because
# 	it is installed . 
# We used the specific form to assign a priority of 
# 	50 and 22 to the redis package , only the first 
# 	priority of 50 is selected . 
# A specific priority overrides any default priority , 
#	general form priority or target release priority , 
#	as such the  priority that is assigned to the
#	redis package from the stable distribution
#	is 50.

					

Which version of a package is selected for installation ?

After selecting the priority , apt will use the following algorithm to determine which package will be installed.

  • If we use apt-get install package/distribution to install a package , then the package specified from the distribution will be installed regardless of anything else .
  • If the version to be installed has the same version than an already installed package apt will not reinstall the package , unless the --reinstall option is specified , even if the meta information of the two versions are different .
  • If the version to be installed has a lower version , than an already installed version , apt will only downgrade if the version to be installed has a priority larger or equal than 1000.
  • Keeping that in mind , if we have multiple version of a package, then the package that will be installed is the package with the highest priority . If all the packages have the same priority , then apt will install the package with the max version number . If they have the same priority , and the same version number , but if their meta information is different , or if we have passed the reinstall option then apt will install the uninstalled one , else so if they have the same priority and the same version, apt will install the package from the first source in the list .
  • If the package with the highest priority has a version lower than the installed package , and if there exist another package with higher priority than the installed package and a more recent version , apt will install the other package.
			
#/etc/apt/sources.list    
deb http://deb.debian.org/debian/ buster main
deb http://deb.debian.org/debian/ unstable main
deb http://deb.debian.org/debian/ testing main
deb http://deb.debian.org/debian/ buster-backports main
deb http://deb.debian.org/debian/ experimental main
# The etc/apt/sources.list file contains 
# a source for the buster distribution , 
# another one for the unstable distribution 
# and a source for each of the testing , 
# buster-backports and experimental distribution . 


#/etc/apt/preferences
Package : *
Pin : release a=stable
Pin-Priority : 990

Package : *
Pin : release a=experimental  
Pin-Priority : 900


# In the /etc/apt/preferences file , 
# we have assigned a priority of 990
# to the stable distribution  , and 
# we have assigned a priority of 
# 900 to the experimental distribution.


@debian:~$ apt-cache policy redis
# get the priorities of distributions and of packages.
redis:
  Installed: 5:5.0.7-1
  Candidate: 5:6.0~rc1-1
  Version table:
     5:6.0~rc1-1 900
        900 http://deb.debian.org/debian experimental/main amd64 Packages
 *** 5:5.0.7-1 500
        500 http://deb.debian.org/debian unstable/main amd64 Packages
        500 http://deb.debian.org/debian testing/main amd64 Packages
        100 /var/lib/dpkg/status
     5:5.0.7-1~bpo10+1 100
        100 http://deb.debian.org/debian buster-backports/main amd64 Packages
     5:5.0.3-4+deb10u1 990
        990 http://deb.debian.org/debian buster/main amd64 Packages
# The 5:5.0.7-1  version of the redis package 
# 	is installed , its selected priority 
# 	is 500 . 
# The version 5:5.0.3-4+deb10u1 of the redis package
#	has a priority of 990 . It is larger than the
#	priority of the installed package . The version
#	5:5.0.3-4+deb10u1 is lower than the installed 
#	version. The priority of the 5:5.0.3-4+deb10u1 
#	version is smaller than 1000 , as such the 
#	installed version is not downgraded. 
# The version 5:6.0~rc1-1 of the redis package has
#	a priority of 900 . The priority of the 
#	the version 5:6.0~rc1-1 of the redis package
#	is higher than the priority of the installed 
#	one which is 500 , and the version is 
#	higher than the installed version , as such the 
#	redis package version  5:6.0~rc1-1 is a 
#	candidate for installation .


@debian:~$ apt-get install redis/buster
# The redis package from the buster distribution 
# will be installed regardless of anything else . 
# In this case this will cause the downgrade
# of the installed version 5:5.0.7-1 of the redis
# package to the version 5:5.0.3-4+deb10u1 . 


#/etc/apt/preferences
# There are no pinned priorities in the 
# /etc/apt/preferences file


@debian:~$ apt-cache policy redis
# get the priorities of distributions and of packages.
clang:
  Installed: (none)
  Candidate: 1:8.0-48.3
  Version table:
     1:9.0-49~exp3 1
          1 http://deb.debian.org/debian experimental/main amd64 Packages
     1:8.0-48.3 500
        500 http://deb.debian.org/debian unstable/main amd64 Packages
        500 http://deb.debian.org/debian testing/main amd64 Packages
     1:7.0-47 500
        500 http://deb.debian.org/debian buster/main amd64 Packages
# The version 1:9.0-49~exp3 of the clang package 
# has a priority of  1 , this is less then the
# priority of both versions 1:8.0-48.3 and 1:7.0-47 
# which are both 500 . 
# The version 1:8.0-48.3 is higher than the version  
# 1:7.0-4 , and both versions have the same priority  
# , as such the version 1:8.0-48.3  is a candidate for 
# installation . 
# This version can be installed from two sources , the
# one containing the unstable distribution , and the 
# one containing the testing distribution . The 
# unstable distribution is the first one in the  
# sources.list , as such it will be the one from which 
# this version will be installed.