How to pin an apt repository for preferential package installation
As described in my last post, pinning apt repositories is now necessary if you want to install Firefox from an apt repository (e.g. if you want to install Firefox Beta). This is not an especially difficult process, but it is significantly confusing, so I thought I'd write a post about it.
Pinning an apt repository means that even if there's a newer version of a package elsewhere, the 'older' version will still be installed from the apt repository you pin.
Be very careful with this technique. You can easily cause major issues with your system if you pin the wrong repository!
Firstly, you want to head to
/etc/apt/sources.list.d/ and find the
.list file for the repository you want to pin. Take note of the URL inside that file, and then run this command:
No root is necessary here, as it's still a read-only command. Depending on how many apt repositories you have installed in your system, there may be a significant amount of output. Find the lines that correspond to the apt repository you want to preferentially install from in this output. For this example, I'm going to pin the excellent nautilus-typeahead apt repository, so the bit I'm looking for looks like this:
999 http://ppa.launchpad.net/lubomir-brindza/nautilus-typeahead/ubuntu jammy/main amd64 Packages release v=22.04,o=LP-PPA-lubomir-brindza-nautilus-typeahead,a=jammy,n=jammy,l=nautilus-typeahead,c=main,b=amd64 origin ppa.launchpad.net
From here, take a note of the
o= bit. In my case, it's
o=LP-PPA-lubomir-brindza-nautilus-typeahead. Then, create a new file in
/etc/apt/preferences.d with the following content:
Package: * Pin: release o=LP-PPA-lubomir-brindza-nautilus-typeahead Pin-Priority: 1001
o=.... bit there? Replace it with the one for the repository you want to pin. The number there is the new priority of the repository. The numbers at the beginning of each line in the output of the
apt-cache policy command are the priorities of your existing apt repositories, so this should give you an idea as to what number you need to use here - a higher number means a higher priority regardless of the version number of the packages contained therein.
sudo apt update and
sudo apt dist-upgrade, and
apt should pick up the "upgrades" from your newly pinned repository! In some situations you may need to remove and reinstall the offending package if you encounter issues.
Sources and further reading
- How to Set Package, PPA, Apt Repository Priority in Ubuntu
- Debian version number specification
- I've got an apt repository, and you can too