How to master the Linux terminal with apt-get

Linux terminal apt-get

One of the biggest changes that catches Windows users moving to Linux is the way that software is installed. Instead of downloading an executable file from some website or other, running it and hoping it doesn't clobber your existing library files (DLLs) or install some dubious adware or malware, Linux distributions maintain repositories of software, which are all packaged up for that distro and tested for compatibility with the rest of the distro.

In this tutorial we will look at how this is done by distros that use the Advanced Packaging Tool (apt) software management system, as developed by Debian and used by distros from Ubuntu to Raspbian on the Raspberry Pi.

Repositories

A repository is a collection of software packages for a distro. Each major release of a distro will have its own repositories and the packages will have been built for and tested with that release, but a repository is more than a collection of files.

Each repo (as they are usually called) is indexed, making it easy to find what you want. It can also be quickly checked for updates for your package manager without any need to visit websites to check for updates or the need for software to 'phone home' to check.

More importantly, each package in a repo is signed with the repository's GPG (encryption) key, which is checked when installing packages. This means you can trust the software installed from there to be what it says it is and not some infected trojan that's been uploaded maliciously.

A repository also makes dependency handling simple. A dependency is a program that the program you want to install needs to run, such as a library. Instead of bundling everything in the package and ending up with multiple copies of the same library on your computer (which is what Windows does), a package simply lists its dependencies so that your package manager can check whether they are already installed and grab them from the repo if not.

In addition to the default repositories provided by the distro, there are several third-party ones that can be added to your package manager. These are not guaranteed to be tested to the same standards as the official repos, but many of them are very good and if you stick to the popularly recommended repos for your distro you won't go far wrong.

Ubuntu has also introduced the concept of the PPA or Personal Package Archive, which are small repositories for individual projects. These may each be added individually to your package manager, but be careful about adding any untrusted sources.

Package management

We have used the term package manager a few times now but what is it? Basically this is a program that enables you to install, update and remove software including taking care of dependencies. It also enables you to search for programs of interest as well as performing other functions. All distros will have command line package management tools. You can access it either by using your system's search and look for 'terminal' (or using Ctrl+Alt+T in desktops such as Unity, Gnome or Xfce) even if they also provide a fancy graphical front end too. The main commands are:

  • apt-get Installs, upgrades and uninstalls packages.
  • apt-cache This works with the repository index files, such as searching for packages.
  • add-apt-repository Adds extra repositories to the system.
  • dpkg A lower level package manipulation command.

These commands generally require root (superuser) access, so should be run at the root user or with sudo – we will stick with the sudo approach here. We've already mentioned that repos are indexed, so the first thing to do is update your index files to match the current contents of the repositories with:

sudo apt-get update

Then you probably want to make sure that your system is up to date:

sudo apt-get upgrade

This will list the packages it wants to install, tell you how much space it needs for the download and then get on with it when you tell it to. When you want to install some new software, unless you have been told the exact name to install, you may want to search for it first, like this:

apt-cache search gimp

This will spit out a long list of packages because it searches both name and description and lists anything mentioning gimp, and there are a lot of them. To search only the names, use the -n or --names-only option, like so:

apt-cache search -n gimp

This often gives a more manageable output, but still a lot in this case, maybe too much to fit in your terminal window. The solution to this is to pipe the output from this command to the program less:

apt-cache search -n gimp | less

The less command is a pager, it lets you read text page by page and scroll through it. It can be used with any program that generates lots of terminal output to make it easier to read.

Once you have found the package you want, installation is as simple as:

sudo apt-get install gimp

You can install multiple programs by giving them all to apt-get at once:

sudo apt-get install program1 program2...

Not every program you try will be what you want, so you can tidy up your hard drive by uninstalling it with:

sudo apt-get remove program1

or you can use

sudo apt-get purge program1

Both commands remove the program, but remove leaves its configuration files in place while purge deletes those too.

There are a number of extra options you can use with apt-get, the man page lists them all (type man apt-get in the terminal), but one of the most useful is --dry-run. This has apt-get show you what it would do without actually doing it, a useful choice to check that you are giving the right command. Remember, computers do what you tell them to, which is not necessarily what you want them to do! Finally, you don't normally need to use dpkg, but it is useful for listing everything you have installed with dpkg -L.

  • This article was provided to TechRadar by Linux Format magazine. Each issue is packed with news analysis, in-depth features on vital Linux topics, helpful tutorials from our expert writers, and more. Subscribe to the print or digital version of Linux Format here
TOPICS