In the previous article, we have explained the basics of package management in Debian Linux such as Ubuntu. We have also explained how to use dpkg command line tool to manage packages in Ubuntu. However, there is a various limitation of dpkg tool. In this article, we are going to explain one of the most advanced, popular package management tool called apt or apt-get in Ubuntu. Since the command syntax will almost remain same for all the versions of Ubuntu, but we are especially using the latest version for this article that is Ubuntu 17.04.
Apt stands for Advanced Packaging Tool that is a powerful command-line tool used to manage packages in Ubuntu and other Linux variants . The following are some of the most common tasks performed by apt-get command line tool:
- Installing new software and packages.
- Updating and upgrading existing packages.
- Removing unused or older packages.
- Updating and upgrading entire Linux system.
The apt-get command line tool is more advanced and simpler to use than other available package management tools in Ubuntu. The following is apt-get command syntax:
sudo apt-get <action> <command options> <package1> <package2>
The following are some of the most common actions performed by the apt-get command in Ubuntu:
- update To retrieve new lists of packages
- upgrade To perform an upgrade
- install To install new packages
- remove To remove packages
- purge To remove packages along with config files
- clean To clean downloaded archive files
- check To verify that there are no broken dependencies
- source To download source archives
- download To download the binary package into the current directory
Working with apt-cache commands
Before explaining apt-get command line tool options and examples in detail, we would like to describe one more useful command line tool that apt-cache. If you want to be expert with apt-get commands, first you must need to understand the apt-cache command syntax and examples.
Here are some of the major package management tasks performed by apt-cache tool with examples:
Listing Available Packages
To list all the available packages using apt-cache in Ubuntu, use the following command:
sudo apt-cache pkgnames
Searching Specific Package
If you are not sure about the exact name of the package you want to install, you can take help of apt-cache command to get more list of matching packages. For example, if you want to install apache web server, but you are not sure what exact package name need to be mentioned while installing. You can use the following command to list all the matching packages with apache name:
sudo apt-cache search apache
Getting Detailed Information About a Package Using Apt-Cache
In order to get more details of a specific package, you can use the following command:
sudo apt-cache show <package-name>
Checking Dependencies of Specific Package
As mentioned earlier, few packages may depend on other packages in order to work properly. If you want to know what are dependent packages for installing a specific package? You can use the following command as shown in the following figure.
sudo apt-cache showpkg <package-name>
Working With Apt-Get Command Line Tool in Ubuntu
Once you are a little bit familiar with the apt-cache commands, let’s begin the actual package manage tasks using the apt-get commands. Here, we will explain how to install, update, and remove packages using apt-get in Ubuntu.
Installing Packages Using apt-get
I personally have worked with many Linux variants and managed packages with various tools. But managing packages with apt-get command is what that I like the most. The installation of packages using the apt-get in Ubuntu is really very simple. The following command syntax is used to install a package using apt-get command-line tool:
sudo apt-get install <package-name>
For example, if you want to install the apache2 web server package, execute the following command as shown in the following figure:
sudo apt-get install apache2
The system will also notify you about the required size to install the packages. If you want to skip Yes/No prompt, use -y command option with apt-get install.
Checking Broken Dependencies
Sometimes you may get an error indicating broken dependencies. To check the broken dependencies, use the following command:
sudo apt-get check
Updating Package Index
Package index is basically a database of available packages from the defined repositories. These repositories are defined in the /etc/apt/sources.list file and /etc/apt/sources.list.d directory. It is recommended to keep updated package index in your system. If you want to update the local package index with the up-to-date changes made in the repositories, execute the following command:
sudo apt-get update -y
Updations is an endless and rapidly changing process in IT. No one can rely on what he has now. There are various features, services, and technologies are inventing on daily basis. So, the packages you are using today will be outdated after few days. After updating your package index, execute the following command to upgrade the outdated packages and even your entire system:
sudo apt-get upgrade –y
The above command will only upgrade the packages. If you want that unused and depreciated packages should also be removed automatically, you can use the following command:
sudo apt-get dist-upgrade –y
The previous two commands will upgrade each of the packages on your system. However, there might be a situation where you only want to upgrade a specific package, not all the packages. You can use the below-mentioned command to upgrade a specific package using apt-get command-line tool:
sudo apt-get upgrade --only-upgrade <package-name>
Removing Packages Using Apt-Get in Ubuntu
If any package(s) is no longer required or need to be removed for any reason, you can remove it. Removing a package using apt-get is pretty straightforward. For example, to remove apache2 package installed in the previous step, execute the following command:
sudo apt-get remove apache2
Removing Packages and Deleting Configuration Files
If you want to remove a package along with its configuration files, you should use the following command:
sudo apt-get purge <package-name>
In this tutorial, we have explained how to install, update, upgrade, and remove packages using apt-cache and apt-get in Ubuntu. Hope, it helped you and cleared your doubts. If you have any further suggestions or queries, you may comment us, we will get in touch with you soon.
- Recommended: Step by step guide to install RHEL 7 in VMware