The Most Useful Apt-get Commands On Debian & Ubuntu
In this tutorial, I’m going to show you how to use apt-get to manage software packages from the terminal. apt-get is the default command line package manager on Debian-based Linux distributions such as Debian, Ubuntu, Linux Mint, elementary OS.
Install Software Package From Repository
Before you install any software, you always want to make sure your local package index is up to date. The local package index contains a list of all package name with their version number that can be installed from your software repository.
sudo apt-get update
sudo means switch user ID to root and do something. On every Linux distribution, you need to have root privilege to manage software package and sudo allows you to temporarily become root.
apt-get install is the command to install software packages, you need to add the package name after it. For example, you can install chromium browser on Ubuntu using the following command.
sudo apt-get install chromium-browser
Sometimes you will be asked if you really want to install the package. If you want to automatically answer yes to these question, you can add -y option like below.
sudo apt-get install -y chromium-browser
In the picture above you can see that chromium-browser is already installed on my system.
Before you install packages using apt-get install, you need to know the package name in your repository. This may sounds like a trouble at first, but once you are familiar with package names, it can be convinient to install software packages from the terminal especially when you are working on a remote Linux server.
apt-get will first download .deb package installer into /var/cache/apt/archives directory, then it install package on your system.
Install Multiple Packages With One Command
You can use apt-get install to install multiple packages at once, for example,
sudo apt-get install wireshark nmap aircrack-ng
apt-get upgrade command is used to upgrade every upgradeable package on your system.
sudo apt-get upgrade
It will tell you what packages will be upgraded and ask you if you really want to upgrade them. You can add -y option to this command to automatically answer yes to questions.
sudo apt-get -y upgrade
This command will remove packages from your system, for example, to remove firefox:
sudo apt-get remove firefox
It will not remove the configuration file of that package.
This command will remove packages and their configuration files.
sudo apt-get purge firefox
When installing or upgrading packages, apt-get will download .deb package installers from repositories to /var/cache/apt/archives/ directory on your file system. apt-get clean command can help you remove all of those .deb files. Most of the time, you don’t need those .deb file any more.
You can use the following command to check how many spaces those .deb files occupy.
du -sh /var/cache/apt/archives
apt-get autoclean can also be used to remove .deb file under /var/cache/apt/archives. However, it only remove those .deb files that can no longer be downloaded from repositories. In other words, these package are no longer maintained by Debian or Ubuntu. Or, those packages have a new name in repository.
This command will remove dependencies that are no longer needed.
This is a command that may confuse people. On Debian you use this command to upgrade the version of your system, for example, upgrade Debian 7 to Debian 8. But on Ubuntu, this command will only upgrade Linux kernel and previously kept-back packages. You need to use do-release-upgrade command to upgrade your version of Ubuntu system.
Bonus: Manually Install deb Packages
If you downloaded a deb package which has .deb extension from the Internet, you will need dpkg tool to install it on your system.
For example, head over to wps-community.org and download the free WPS office suite deb package. Once downloaded, change your current working directory to the download directory and issue the following command to install the deb package onto your system.
sudo dpkg -i wps-office*.deb
A drawback of dpkg is that it can not automatically resolve dependency problem. If your downloaded deb package require another package which is not installed on your system, you have to manually install the other package and then install the deb package.
A great tool called gdebi can also be used to install deb package. The beauty of gdebi is that it will automatically install all dependencies for you. To install gdebi on your system:
sudo apt-get install gdebi
Its syntax is as follows:
sudo gdebi <package.deb>