How to Use Basic APT Commands to Manage Packages on Debian-Based Alibaba Cloud Servers

By Francis Ndungu, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

APT is an acronym for Advanced Packaging Tool. It is an easy-to-use feature-rich software management tool that works on Debian Based Linux distributions (e.g. Ubuntu 14.04, Ubuntu 16.04, Ubuntu 18.04, Debian 8, Debian 9, etc). APT was first released in 1998 and a Debian version that included it by default saw the light the following year.

APT simplifies the process of installing, removing and upgrading packages through the Command Line Interface (CLI). The Debian project maintains a central repository with over 25,000 packages that can be downloaded and installed on your Linux system.

With its automated retrieval, configuration and installation of software packages, APT is considered to be one of the best Debian features. APT works by querying a software repository (networked or in a CD ROM location) to find packages and dependencies required.

In this article, we will show you how to find, install, remove and upgrade software packages on your Debian based Linux machines hosted on Alibaba Cloud Elastic Compute Service (ECS).

Prerequisites

  1. A valid Alibaba Cloud Account. You can sign up for a free trial and get over $300 worth of credit to test more than 40 Alibaba Cloud products.
  2. An ECS(Elastic Compute Service) instance running any Debian based operating system e.g. Ubuntu 14.04, Ubuntu 16.04, Debian 8, Debian 9 e.t.c
  3. A non-root user that can perform sudo tasks on your Linux system.

Step 1: SSH To Your Linux Machine

Locate the public IP address associated with your Alibaba Cloud ECS instance on the console and use it to connect to your server. You will need to provide a non-root username and password to continue.

You may establish an SSH connection through the built-in command line tool if you are running macOS or Linux on your local computer. If you are using Windows, consider downloading PuTTY SSH client.

Managing applications using APT commands on your Linux system requires superuser privileges apart from the search command. So we will run all commands using sudo.

Step 2: Listing Package Information on Your System

Linux apt-cache pkgnames Command

To display information about installable packages, use the apt-cache command. The information about the packages is obtained from different sources and stored in a local database. The cache is updated during the apt-get update operation.

Run the command as shown below to list available packages:

Sample Output:

Linux apt-cache search Command

The apt-cache pkgnames is too basic and returns a lot of results. A more elegant way is to use the apt-cache command alongside a keyword to filter results using the syntax below:

Example

Output

In case you don’t get any results, it means the package information is not available or you haven’t update the package list using apt-get update command. We will see this command later in the guide.

Linux apt-cache show Command

You can display more information about a package using the apt-cache show command. This displays the version number, maintainer, bugs, filename, size and description. For instance, to display more information about mysql-server package, run the command below:

Output:

Step 3: Updating Package Information using apt-get update Command

To ensure you have the latest package list or index, use the apt-get update command. Please note, this command does not update the software installed on your system as its name suggests.

It simply synchronizes the installable packages index to ensure that you have the newest information should you need to install any application. Indexes about available information are read from the /etc/apt/sources.list file.

Always run this command before installing or upgrading an application as shown below:

Sample Output:

Step 4: Installing New Packages using apt-get install Command

Once you ensure the package information index is up-to-date by running apt-get update, you can install any package of your choice by running the apt-get install command.

You can install multiple packages in one command or install the software one by one. Use the syntax below:

Here are some of the most popular packages that you can install on your Debian based Linux system using the apt command.

Installing Apache Web Server via APT Command

Installing PHP Scripting Language via APT Command

Installing MySQL Database Server via APT Command

Installing Postfix Email Server via APT Command

Press Y and hit Enter when prompted to confirm the installation of new packages.

Step 5: Upgrading Packages using apt-get upgrade Command

The apt-get upgrade command installs newer versions of the current installed packages on your system. As we indicated earlier, you must update the package information list before upgrading any packages so that APT can enumerate sources of new packages from /etc/apt/sources.list.

To upgrade the packages, use the command below. Please note, this command will never remove or install new packages. it will simply update the currently installed packages.

Press Y and hit Enter when prompted to confirm the upgrade.

An upgrade may take a few minutes to complete, so it is advisable to run it when the system is not busy.

You can also use apt-get dist-upgrade to upgrade the package list but this command is more advanced because it changes package dependencies with a smart conflict resolution method. Please note, this command may remove or install new packages to satisfy dependencies.

So here is the difference between apt-get upgrade and apt-get dist-upgrade:

If an upgrade is run, new packages are never installed neither are old packages removed to satisfy dependencies, it simply changes the versions of existing packages. However, a dist-upgrade may install new packages and remove others to resolve dependencies issues.

Step 6: Removing Packages and Cleaning the System

APT has great commands for removing packages and cleaning temporary installation files on your system.

Removing Packages using apt-get remove Command

You can use the apt-get remove command to remove packages from your Debian based system. Here is the syntax for the command:

Example:

Please note, the above command will uninstall the package but leave the configuration files, data files and dependency information intact. To remove everything about a package apart from the dependency packages, use the apt-get purge or apt-get remove — purge commands.

Or:

Example:

Or:

Press Y and hit Enter when prompted to confirm the uninstallation process.

Removing Orphaned Packages with apt-get autoremove Command

To remove orphaned packages, use the apt-get autoremove command shortly after uninstalling software. This will ensure that packages that worked as dependencies for other applications but are no longer required are removed from your system.

Cleaning the System using apt-get clean Command

To clean your system and free some disk space after installing packages, use the apt-get clean command. This will ensure that the retrieved deb package files are deleted from your system after installation.

This command may free up a substantial disk space on your server. For instance, a du command shows deb files may take disk space in the ranges of hundreds of megabytes:

Output:

Use the command below to reclaim the space and delete the temporary files:

Conclusion

We have taken you through the steps of searching, updating, installing, upgrading and removing packages on your Linux System through APT commands. This guide will help you to better manage the software applications running on your Alibaba Cloud Linux server based on the Debian platform.

It will also ensure that you have the latest software with new features and bug fixes. If you are new to Alibaba Cloud, Signup and enjoy up to $300 worth of credit to test over 40 products including ECS instances running Debian based Linux distributions that support apt commands.

Reference:https://www.alibabacloud.com/blog/how-to-use-basic-apt-commands-to-manage-packages-on-debian-based-alibaba-cloud-servers_594266?spm=a2c41.12420289.0.0

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store