Many new Linux users are mystified the first time they try to install a software application. On Windows, you would normally search for a software package on the web, download it, and then run setup.exe. So, it seems logical to do something similar on Linux. Well, it’s actually even easier than that, but you have to know what to do.
Suppose you wanted to install Wine on your Ubuntu desktop because you heard that you can use it to run Windows programs on Linux. If you were to try to install it the Windows way, you would go to
http://www.winehq.org/download and try to find the right package to download. I will save you the trouble of running around in circles on that website because there is no obvious way to download an end-user version of Wine for Ubuntu.
At this point, you might be tempted to throw up your hands in frustration and go back to Windows—but wait!—there’s a hint on the Wine site. It mentions a command called
apt-get, which turns out to be an almost magical way to install Linux packages.
Here’s what you do. Type:
sudo apt-get install wine
That’s it! That’s all you have to do. What’s even more incredible is that if Wine has dependencies (i.e. if it requires other specific packages to be installed first), then it will automatically install those first! (Okay, I’ll stop using exclamation points now, but I think those two were justified.)
Note that this will only work on Debian-based Linux distributions, such as Ubuntu, Linux Mint, Elementary OS, and Debian. If you are using another distribution, then see the
equivalent commands at the end of the article.
How It Works
In
/etc/apt/sources.list, there is a list of software repositories that
apt-get searches. For example, my Ubuntu system lists various repositories under http://security.ubuntu.com/ubuntu. When
apt-get finds the package name (e.g. wine) in a repository’s index, it downloads the package from the repository and installs it on your system.
What If I Don’t Know the Name of the Package?
If you didn’t know that the package that lets you run Windows software is called Wine, then you could type something like this:
sudo apt-cache search windows emulator
This would list a number of packages, hopefully including at least one version of Wine. Ironically, Wine stands for “Wine Is Not an Emulator”, so searching for “windows emulator” may not have been the best idea, although it did find wine1.6 on my Ubuntu 15.04 system.
Once you realize that it’s called wine, then you could search for all versions of wine. Type:
sudo apt-cache search wine
One of the items should be something like the following:
wine - Microsoft Windows Compatibility Layer (meta-package)
There are a couple of interesting things about this package. It doesn’t have a version number and it’s a “meta-package”. That means it is simply a list of packages, so if you install it, everything you need for the default version of wine will be installed.
If you want to get more details about the package, then you can type:
sudo apt-cache show wine
Why Can’t I Just Use a Graphical Front-End Like Ubuntu Software Center?
You can. It works very well. If you just want to install a software package on your own Linux desktop, then you’re probably better off just using the graphical installer. But there are plenty of times when you need to use the command line, such as when:
- You’re logged into a remote Linux server through the command line
- You need to troubleshoot issues with software installation, which is usually easier from the command line
- There is a problem preventing you from starting the graphical desktop and you have to install, remove, or update a software package to fix it
- You want to automate software installations by putting the appropriate commands into a script
Keeping the Cache Up-to-Date
Did you find it odd that the command for searching was
apt-cache rather than
apt-get? That’s because the index of packages gets downloaded from the repositories into a cache on your computer, so that’s what it searches when you use
apt-cache. If you want to make sure your cache is not out-of-date, then type:
sudo apt-get update
This will check the repositories for any changes since the last time this command was run.
Hey, since we’re updating the cache, shouldn’t it be
apt-cache update? Well, that would make more sense, wouldn’t it, but alas, that’s not the right command.
Note that if you add a new repository to
/etc/apt/sources.list or make a change to an existing entry, then you have to run
apt-get update to get the new repository’s package index into your cache.
Removing Packages
To uninstall a package, use the remove option. For example, to remove wine, type:
sudo apt-get remove wine
This will only remove the package itself. If you want to remove the associated configuration files as well, then use the purge option instead. For example, to remove wine and its configuration files, type:
sudo apt-get purge wine
Remember when we noticed that wine is a meta-package? Well, unfortunately, that means you also have to remove all of the packages that were installed by the meta-package, and there are a lot of them. Fortunately, there is an easy way to do this. Type:
sudo apt-get autoremove --purge
This will remove all of the packages that are no longer needed on your system. The
–purge flag tells it to remove their configuration files as well.
Fixing Broken Dependencies
Although
apt-get tries to install dependencies automatically, sometimes it runs into problems. To resolve dependency issues, you could attempt to install the dependencies manually using
apt-get or you could run the following command, which attempts to fix broken dependencies:
sudo apt-get install -f
Hey, I Don’t Have apt-get on My Linux System
The apt commands are only installed by default on Debian-based Linux distributions, such as Ubuntu and Linux Mint. Although you can install apt on other distributions, you might be better off learning to use the installer that comes with the distribution. Here are the default installers on the most common non-Debian Linux distributions:
Installer |
Distributions |
yum |
Red Hat, CentOS |
dnf |
Fedora |
zypper |
SUSE, openSUSE |
pacman |
Arch Linux, Manjaro |