Assign highest priority to my local repository

Solution 1:

The short answer:

The APT preferences file /etc/apt/preferences can be used to control which versions of packages will be selected for installation.

Here is the file /etc/apt/preferences, which apt-get use to decide, which version it's have to select if there are many versions of a single packge. With this, any package that comes from the local repository, will take precedence over the other packages.

Package: *  
Pin: origin ""  
Pin-Priority: 1001  

This won't control the version, but the source preference if both packages has the same version. If you need to assign highest priority for the same package version in your local repo, list them at the top to the /etc/apt/sources.list file>

Answer to the original question:

How to assign highest priority to local repository without using sources.list file?

There is no way to assign highest priority to local repository without using sources.list file. you must put them in top of "sources.list" if you want to assign highest priority to your local repo.

Summary answer to this question:

How can I make apt-get prefer local repository packages instead of downloading again the same package version from official repositories? The local repository is signed and trusted. We've already tried apt preferences priority.

Using the /etc/apt/sources.list file, You need to put your local repository line at the top of this file. like the below one.

deb cdrom:[Kubuntu 12.04 LTS _Precise Pangolin_ - Release i386 (20120424)]/ precise main restricted
deb file:/media/Main/Precise-Debs-New /
deb file:/media/Main/Linux-Software/Ubuntu/Precise /

deb http://archive.ubuntu.com/ubuntu precise main universe restricted multiverse
deb-src http://archive.ubuntu.com/ubuntu precise universe main multiverse restricted #Added by software-properties

# deb cdrom:[Xubuntu 12.04 LTS _Precise Pangolin_ - Release i386 (20120423.1)]/ precise main multiverse restricted universe
# deb cdrom:[Ubuntu 12.04 LTS _Precise Pangolin_ - Release i386 (20120423)]/ precise main restricted

# deb http://archive.canonical.com/ubuntu precise partner
# deb-src http://archive.canonical.com/ubuntu precise partner
# deb http://extras.ubuntu.com/ubuntu precise main
# deb-src http://extras.ubuntu.com/ubuntu precise main

And yes, "apt_preference" has nothing to do with selecting instances of the packages with same version. The point here is, you can't use /etc/apt/preference file to force to select/use a .deb file from your local repository by assigning highest priority to your local one. That file(preference) is used to select an specific version, not a specific instance (.deb file) of the same version. For the latter case, you need to use /etc/apt/sources.list file with above mentioned method.


Explanation:

Seems we all misunderstood the apt_preference purpose , the apt_preference manual describes this in the first 3 paragraph!!

The APT preferences file /etc/apt/preferences can be used to control which versions of packages will be selected for installation.

Several versions of a package may be available for installation when the sources.list(5) file contains references to more than one distribution (for example, stable and testing). APT assigns a priority to each version that is available. Subject to dependency constraints, apt-get(8) selects the version with the highest priority for installation. The APT preferences file overrides the priorities that APT assigns to package versions by default, thus giving the user control over which one is selected for installation.

And the very important one is in third paragraph

Several instances of the same version of a package may be available when the sources.list(5) file contains references to more than one source. In this case apt-get(8) downloads the instance listed earliest in the sources.list(5) file. The APT preferences file does not affect the choice of instance, only the choice of version.

Those Italics were made by mine

Let's see this with an example:

I have precise's version of compiz-fusion-plugins-extra, but I disabled that for demonstration

Here is the file /etc/apt/preferences, which apt-get use to decide, which version it's have to select if there are many versions of a single packge. (In this example, compiz-fusion-plugins-extra has several versions.

Package: *  
Pin: origin ""  
Pin-Priority: 1001  

This file says apt-get to install from local repository for all packages which have several versions exists in different repository.

In other words, if I have package xapps with version 1.0 in my local repository, and this same package xapps exists in Precise's repository with higher version, say 2.0, apt-get will prefer the version from local repository, i.e version 1.0.

Here is my /etc/apt/sources.list file

deb http://archive.ubuntu.com/ubuntu precise main universe restricted multiverse
deb-src http://archive.ubuntu.com/ubuntu precise universe main multiverse restricted #Added by software-properties
deb http://security.ubuntu.com/ubuntu/ precise-security universe main multiverse restricted
deb http://archive.ubuntu.com/ubuntu precise-updates universe main multiverse restricted
deb http://archive.ubuntu.com/ubuntu precise-backports universe main multiverse restricted

# deb http://archive.canonical.com/ubuntu precise partner
#deb-src http://archive.canonical.com/ubuntu precise partner
# deb http://extras.ubuntu.com/ubuntu precise main
# deb-src http://extras.ubuntu.com/ubuntu precise main


deb file:/media/Main/Linux-Software/Ubuntu/Oneiric / #this is oneiric repository.
deb file:/media/Main/Linux-Software/Ubuntu/Natty/natty /
deb file:/media/Main/Linux-Software/Ubuntu/Natty/kubuntu /
deb file:/media/Main/Linux-Software/Ubuntu/Natty/natty_new /

Although, I have listed the precise's repository earlier in the file, it does not cause apt-get to select package from that repository. As described in the manual

The APT preferences file does not affect the choice of instance, only the choice of version.

So, the output of the command apt-cache policy compiz-fusion-plugins-extra was as expected as below

anwar@edubuntu-lenovo:/etc/apt$ apt-cache policy compiz-fusion-plugins-extra 
compiz-fusion-plugins-extra:
  Installed: (none)
  Candidate: 0.9.5.94-0ubuntu1  / **selecting lower versions for priority** /
  Version table:
     0.9.7.0~bzr9-0ubuntu6 0
        500 http://archive.ubuntu.com/ubuntu/ precise/universe i386 Packages
     0.9.5.94-0ubuntu1 0
       1001 file:/media/Main/Linux-Software/Ubuntu/Oneiric/  Packages
     0.9.4-0ubuntu3 0
       1001 file:/media/Main/Linux-Software/Ubuntu/Natty/natty/  Packages

Here, the candidate version is 0.9.5.94-0ubuntu1, (which is in the local repository), not 0.9.7.0~bzr9-0ubuntu6 0, which is in precises' repository. although the precises' one was listed first in the sources.list file.

An example run of apt-get trying to install compiz-fusion-plugins-extra after setting highest priority to local repository.

anwar@edubuntu-lenovo:~$ sudo apt-get install compiz-fusion-plugins-extra 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  x11-apps x11-xfs-utils xinit x11-session-utils libfs6
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  compiz-core compiz-plugins-default compiz-plugins-extra
  compiz-plugins-main-default libdecoration0 xserver-xorg-core
  xserver-xorg-video-intel
Suggested packages:
  compizconfig-settings-manager xfonts-100dpi xfonts-75dpi
The following packages will be REMOVED:
  compiz compiz-gnome compizconfig-backend-gconf edubuntu-desktop
  libcompizconfig0 ubuntu-desktop unity xorg xserver-xorg
  xserver-xorg-input-all xserver-xorg-input-evdev xserver-xorg-input-mouse
  xserver-xorg-input-synaptics xserver-xorg-input-vmmouse
  xserver-xorg-input-wacom xserver-xorg-video-all xserver-xorg-video-ati
  xserver-xorg-video-cirrus xserver-xorg-video-fbdev xserver-xorg-video-geode
  xserver-xorg-video-mach64 xserver-xorg-video-mga xserver-xorg-video-neomagic
  xserver-xorg-video-nouveau xserver-xorg-video-openchrome
  xserver-xorg-video-qxl xserver-xorg-video-r128 xserver-xorg-video-radeon
  xserver-xorg-video-s3 xserver-xorg-video-savage
  xserver-xorg-video-siliconmotion xserver-xorg-video-sis
  xserver-xorg-video-sisusb xserver-xorg-video-tdfx xserver-xorg-video-trident
  xserver-xorg-video-vesa xserver-xorg-video-vmware
The following NEW packages will be installed:
  compiz-fusion-plugins-extra compiz-plugins-extra
The following packages will be DOWNGRADED:
  compiz-core compiz-plugins-default compiz-plugins-main-default
  libdecoration0 xserver-xorg-core xserver-xorg-video-intel
0 upgraded, 2 newly installed, 6 downgraded, 37 to remove and 698 not upgraded.
Need to get 0 B/5,772 kB of archives.
After this operation, 8,294 kB disk space will be freed.
Do you want to continue [Y/n]? 

Strange!!!, It is trying to downgrade the already installed compiz packages of precise to resolve dependencies (matching with Oneiric's one) with newly selected compiz-fusion-plugins-extra!!!.

Which is more dangerous is , it is trying to remove graphics driver packages (xserver-xorg-....) to avoid conflict with new compizes.

(Probably, If I also had them in my local repository, they may safely want to downgrade. But as those packages are usually installed, i did not download them, so aren't in local repo, hence this result).

Conclusion: So, using /etc/apt/preferences is only recommended to use with local repository, if you have complete repository in local system.

What if several instances (.deb file) of the same version exists in many repository.

Which is described by the apt_preference manual

Several instances of the same version of a package may be available when the sources.list(5) file contains references to more than one source. In this case apt-get(8) downloads the instance listed earliest in the sources.list(5) file.

An example of use cases for /etc/apt/preferences file:

An example of forcing apt-get to select version available from a custom repo, instead of Ubuntu's standard one may be the "Linux Mint" distribution. They use this custom preference file.

Package: *
Pin: release o=linuxmint
Pin-Priority: 700

Package: *
Pin: origin packages.linuxmint.com
Pin-Priority: 700

Package: *
Pin: release o=Ubuntu
Pin-Priority: 500

This effectively causes them, to provide packages when they have those in their repositories. But if they don't have them, they use Ubuntu's standard repository.


An Example running apt-get after setting highest priority to local repository

I just wanted to install kubuntu-desktop package over Ubuntu to test KDE. (I forgot that, i have set a preference using /etc/apt/preference file). I had an iso of the kubuntu-12.04-alternate-installer, so i thought I can use that.

(after properly setting the iso as repository source), I ran
sudo apt-get install kubuntu-desktop, then surprised by seeing the output below.

 
anwar@edubuntu-lenovo:~$ sudo apt-get install kubuntu-desktop
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 kubuntu-desktop : Depends: language-selector-kde but it is not going to be installed
                   Recommends: amarok but it is not going to be installed
                   Recommends: apturl-kde but it is not going to be installed
                   Recommends: gdebi-kde but it is not going to be installed
                   Recommends: gpgsm but it is not going to be installed
                   Recommends: ibus-qt4 but it is not going to be installed
                   Recommends: jockey-kde but it is not going to be installed
                   Recommends: kdegraphics-strigi-plugins but it is not going to be installed
                   Recommends: kdepim-wizards but it is not going to be installed
                   Recommends: kpackagekit but it is not going to be installed
                   Recommends: libreoffice-kde but it is not going to be installed
                   Recommends: libreoffice-style-oxygen but it is not going to be installed
                   Recommends: network-manager-pptp-kde but it is not going to be installed
                   Recommends: plasma-widget-facebook but it is not going to be installed
                   Recommends: plasma-widget-kimpanel but it is not going to be installed
                   Recommends: usb-creator-kde but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

I was surprised by the result. suddenly i remember that, I have set a preference (for writing this answer) in /etc/apt/preference to set highest priority to my local repository.

so, I ran this: apt-cache policy kubuntu-desktop, the output was as below:

anwar@edubuntu-lenovo:~$ apt-cache policy kubuntu-desktop 
kubuntu-desktop:
  Installed: (none)
  Candidate: 1.222.1kde47~ppa2
  Version table:
     1.254 0
        500 cdrom://Kubuntu 12.04 LTS _Precise Pangolin_ - Release i386 (20120424)/ precise/main i386 Packages
        500 http://archive.ubuntu.com/ubuntu/ precise/main i386 Packages
     1.222.1kde47~ppa2 0
       1001 file:/media/Main/Linux-Software/Ubuntu/Natty/natty/  Packages
     1.222.1 0
       1001 file:/media/Main/Linux-Software/Ubuntu/Natty/natty/  Packages
     1.222 0
       1001 file:/media/Main/Linux-Software/Ubuntu/Natty/kubuntu/  Packages

In this output, we see that both the "cdrom" and "precise main repository" has version number 1.254, while the local repository has version 1.222.1kde47~ppa2, which is lower version. Although, they are placed before the local ones in sources.list file, local packages got precedence.

To explicitly see that apt-get is indeed trying to install from local one, I ran synaptic and got this, which is showing that, the Apt system consider 1.222.1kde47~ppa2 as the candidate version to install.

enter image description here

Also apt-cache policy kubuntu-desktop showing the same things

anwar@edubuntu-lenovo:~$ apt-cache policy kubuntu-desktop
kubuntu-desktop:
  Installed: (none)
  Candidate: 1.222.1kde47~ppa2
  Version table:
     1.254 0
        500 cdrom://Kubuntu 12.04 LTS _Precise Pangolin_ - Release i386 (20120424)/ precise/main i386 Packages
        500 http://archive.ubuntu.com/ubuntu/ precise/main i386 Packages
     1.222.1kde47~ppa2 0
       1001 file:/media/Main/Linux-Software/Ubuntu/Natty/natty/  Packages
     1.222.1 0
       1001 file:/media/Main/Linux-Software/Ubuntu/Natty/natty/  Packages
     1.222 0
       1001 file:/media/Main/Linux-Software/Ubuntu/Natty/kubuntu/  Packages

To solve this, I removed the /etc/apt/preference file by sudo rm /etc/apt/preference.

Then ran again this again apt-cache policy kubuntu-desktop. only seeing this expected output.

anwar@edubuntu-lenovo:~$ sudo rm /etc/apt/preferences
anwar@edubuntu-lenovo:~$ sudo apt-cache policy kubuntu-desktop
kubuntu-desktop:
  Installed: (none)
  Candidate: 1.254
  Version table:
     1.254 0
        500 cdrom://Kubuntu 12.04 LTS _Precise Pangolin_ - Release i386 (20120424)/ precise/main i386 Packages
        500 http://archive.ubuntu.com/ubuntu/ precise/main i386 Packages
     1.222.1kde47~ppa2 0
        500 file:/media/Main/Linux-Software/Ubuntu/Natty/natty/  Packages
     1.222.1 0
        500 file:/media/Main/Linux-Software/Ubuntu/Natty/natty/  Packages
     1.222 0
        500 file:/media/Main/Linux-Software/Ubuntu/Natty/kubuntu/  Packages

Also ran this again. sudo apt-get install kubuntu-desktop to see the output below:

......... Unnecessary details deleted .........

0 upgraded, 213 newly installed, 0 to remove and 150 not upgraded.
Need to get 0 B/89.6 MB of archives.
After this operation, 269 MB of additional disk space will be used.
Do you want to continue [Y/n]? 

You can check the full result here

Then I pretty much sure about the Explanation portion.


Reference 1 : The apt_preference manual

Reference 2: How to Keep a specific version of packages installed