How to Use Basic APT Commands to Manage Packages on Debian-Based Alibaba Cloud Servers
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).
- 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.
- 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
- 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:
$ apt-cache pkgnames
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:
$ sudo apt-cache search [keyword]
$ sudo apt-cache search mysql-server
mysql-server - MySQL database server (metapackage depending on the latest version)
mysql-server-5.7 - MySQL database server binaries and system database setup
mysql-server-core-5.7 - MySQL database server binaries
default-mysql-server - MySQL database server binaries and system database setup (metapackage)
default-mysql-server-core - MySQL database server binaries (metapackage)
mariadb-server-10.1 - MariaDB database server binaries
mariadb-server-core-10.1 - MariaDB database core server files
percona-xtradb-cluster-server-5.7 - Percona XtraDB Cluster database server binaries
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:
$ apt-cache show mysql-server
Maintainer: Ubuntu Developers <firstname.lastname@example.org>
Original-Maintainer: Debian MySQL Maintainers <email@example.com>
Description-en: MySQL database server (metapackage depending on the latest version)
This is an empty package that depends on the current "best" version of
mysql-server (currently mysql-server-5.7), as determined by the MySQL
maintainers. Install this package if in doubt about which MySQL
version you need. That will install the version recommended by the
MySQL is a fast, stable and true multi-user, multi-threaded SQL database
server. SQL (Structured Query Language) is the most popular database query
language in the world. The main goals of MySQL are speed, robustness and
ease of use.
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:
$ sudo apt-get update
Ign:1 http://mirrors.cloud.aliyuncs.com/debian stretch InRelease
Get:2 http://mirrors.cloud.aliyuncs.com/debian stretch-updates InRelease [91.0 kB]
Get:3 http://mirrors.cloud.aliyuncs.com/debian stretch-proposed-updates InRelease [96.3 kB]
Get:4 http://mirrors.cloud.aliyuncs.com/debian stretch-backports InRelease [91.8 kB]
Get:5 http://mirrors.cloud.aliyuncs.com/debian-security stretch/updates InRelease [94.3 kB]
Get:6 http://mirrors.cloud.aliyuncs.com/debian stretch Release [118 kB]
Get:7 http://mirrors.cloud.aliyuncs.com/debian stretch Release.gpg [2,434 B]
Get:8 http://mirrors.cloud.aliyuncs.com/debian stretch-updates/main Sources [3,752 B]
Get:9 http://mirrors.cloud.aliyuncs.com/debian stretch-updates/main amd64 Packages [5,148 B]...Get:40 http://mirrors.cloud.aliyuncs.com/debian stretch/contrib amd64 Packages [50.9 kB]
Get:41 http://mirrors.cloud.aliyuncs.com/debian stretch/contrib Translation-en [45.9 kB]
Get:42 http://mirrors.cloud.aliyuncs.com/debian stretch/non-free amd64 Packages [78.7 kB]
Get:43 http://mirrors.cloud.aliyuncs.com/debian stretch/non-free Translation-en [80.6 kB]
Fetched 22.2 MB in 5s (4,344 kB/s)
Reading package lists... Done
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:
$ sudo apt-get install [package name 1] [package name 2] [package name 3] [package name n...]
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
$ sudo apt-get install apache2
Installing PHP Scripting Language via APT Command
$ sudo apt-get install php
Installing MySQL Database Server via APT Command
$ sudo apt-get install mysql-server
Installing Postfix Email Server via APT Command
$ sudo apt-get install postfix
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.
$ sudo apt-get upgrade
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.
$ sudo apt-get dist-upgrade
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:
$ sudo apt-get remove [package name]
$ sudo apt-get remove php
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.
$ sudo apt-get purge [package name]
$ sudo apt-get remove --purge [package name]
$ sudo apt-get purge php
$ sudo apt-get remove --purge php
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.
$ sudo apt-get autoremove
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:
$ sudo du /var/cache/apt/archives/ -h
Use the command below to reclaim the space and delete the temporary files:
$ sudo apt-get clean
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.