Using package managers and Repositories in Linux

I know what you might be thinking:

sudo apt install pkgname  
sudo apt remove pkgname  

All done.
It is true that between install and remove you are probably covered most of the time. There are various details about how to search for, list, and remove packages or configure repos however that will be of use in specific situations, and I am going to go over some of those.

This is going to cover the debian package management tools

Some of this is applicable more generally, like repo format for sources.list, but the commands I will cover are of the debian/ubuntu variety.

Looking at installed packages or searching for ones to install

There are a bunch of different ways to list them, search for them, and some commands that do both at the same time by showing all matching names as well as the install status.
Effective searching of the repositories allows you to determine candidates for installation without always resorting to web searches. Determining what is installed on your own system is also very useful because it is not always obvious what you have installed for a variety of reasons.

apt list --installed     - list all installed packages.  
apt list -a mongodb      - Find package names matching, show install status.  
dpkg -l                  - list all installed packages.  
dpkg -l libreoffice      - Look at specific package and install status.  
aptitude search '~iword' - Look for installed package with word in name.  
aptitude search word     - List packages with word in name, show install status.  
apt-cache pkgnames name  - List any packages that start with the search term.  
apt-cache search -n      - Search all packages, names only.  
apt search               - Search package descriptions.  

Installing from .deb files

If a package is not available from a repo, the creator may have put together a .deb file that can be downloaded and used to install it. These are also very easy to install, so don’t shy away from using packages just because they are not directly available from a repo. There are a couple combinations of commands that can be used to install the .deb package, either one of the options shown below should work.

# Install package from a .deb file then use apt-get to check dependencies:
dpkg -i downloaded.deb
sudo apt-get install -f
# Or Install a .deb with gdebi, which will deal with dependencies  
gdebi package.deb    

Checking dpkg lists for packages details

Dpkg does the lower level work with packages, and you can use its logs to get information about package install, remove, upgrade. This is useful to see when what actions were taken. The command combinations below use grep to search for useful keywords in the dpkg logs.

grep installed /var/log/dpkg.log  - See when packages were installed  
grep upgrade /var/log/dpkg.log    - look for upgrade notices.  
grep remove /var/log/dpkg.log     - check for removal.  
grep upgrade /var/log/dpkg.log.1  - check an archived log for information.  
grep upgrade /var/log/dpkg.log*   - Search current and all archived logs.  

Uninstall packages

There are some different remove operations that have different purposes. Some commands remove packages, while others look for unused dependencies or files and can cull those.

apt remove name    - remove a package
apt autoremove     - remove dependencies that are no longer needed
apt-get purge name - Completely removes a package; configs, everything.  
apt-get clean      - Clean package downloads 
apt-get autoclean  - Clean outdated package downloads 

Adding repositories

Distros will set up with a default set of repositories, but you may need to add other ones to install some programs.

Adding repos

add-apt-repository can add any PPA repo. These are Personal Package Archives hosted on launchpad.

add-apt-repository name  
sudo add-apt-repository -y ppa:ansible/ansible  

Adding non PPA repos

If you want a package from a non PPA repo, you need to add the repo manually. This line would need to be added to sources.list or a new files in sources.list.d. This example is for virtualbox, however yours will almost certainly be differnt. I give more detailed information about each part of specifying a repository below. Depending on the source of the repo, there is a good chance they will indicate how to add their repository.

deb [arch=amd64]  http\://download.virtualbox.org/virtualbox/debian   
## This command should accomplish adding the line if you don't want to manually
sudo add-apt-repository deb [arch=amd64]  http://download.virtualbox.org/virtualbox/debian   

Specifying a repository to install from

If a package is available from multiple repositories, you can use the -t flag with apt-get to specify which repo to get it from.

apt-get -t repository-name install packagename  

Also use the -t flag to specify searching a particular repo.

apt-cache -t repository-name search packagename  

How repository specification format works

There are several parts to each, so I will break down each part.

The Component

This part specifies the software license of projects in the repo.

deb http\://us.archive.ubuntu.com/ubuntu/ bionic **(main, universe, multiverse, restricted)**  
deb http\://us.archive.ubuntu.com/ubuntu/ bionic **(main, contrib, non-free)**

This is what each option means in ubuntu

main - canonical supported free and open-source
universe - community maintained free and open source.
restricted - Proprietary drivers for devices.
multiverse - Software restricted by copyright or legal issues.

Debian option meanings

main - Package and dependencies are Debian Free Software Guidelines (DFSG) compliant
contrib - DFSG, but relies on something not in main.
non-free - Software that does not comply with DFSG.

The Dirstribution/Release

This specifies the release or distribution of the OS. Most linux variants have names for releases and that is what usually goes here. If rolling releases are used this would not apply.

deb http\://us.archive.ubuntu.com/ubuntu/ < xenial bionic disco ... > main  
deb http\://us.archive.ubuntu.com/ubuntu/ < jessie stretch buster ... > main

Release/Distro “pockets”

A pocket is an extension to a release that affects what updates to take. Leaving just the plain release name in place usually means to only take software that came out with the original release, which is usually not what you want.

deb http\://us.archive.ubuntu.com/ubuntu/ < xenial xenial-security xenial-updates xenial-backports xenial-proposed >  

What these pocket categories do for a release

no modifier - Usually only what came out with the original release.
-security - These are the most important security updates only.
-recommended/updates - These are not security critical, but are often helpful for fixing bugs and compatibility.
-proposed - Before something can get into recommended, it goes here for testing. These can be unstable.
-backports - get features backported from future releases, so something you might need on an older system that has been made available for it.

URI -

Where to look for the packages and updates.

deb < http\://us.archive.ubuntu.com/ubuntu/ > bionic main  

Source type -

This will specify whether you want the already built packages (deb) for installation or to build them yourself by getting just the source (deb-src). I have not run into many instances of using deb-src, though did see it in code that was doing system builds with a lot of customization.

## You most likely want deb
< deb deb-src > http://us.archive.ubuntu.com/ubuntu/ xenial main