How to Use the apt Command on an Ubuntu Cloud Server?

15-01-2024 02:44:30

This article explains how to use the apt-get and apt-cache commands in the Ubuntu environment for installing, uninstalling, and finding software packages. These commands are applicable for systems running Ubuntu or Debian.

  • apt-get is used for installing, uninstalling, and updating software packages.
  • apt-cache is used for searching software packages and displaying package information.

Common Commands

Retrieve update list:

sudo apt-get update

Install a software package:

sudo apt-get install package name -y

Search for a software package:

sudo apt-cache search package name

Display information about a software package:

sudo apt-cache show package-name

Uninstall a software package (retain configuration files):

sudo apt-get remove package-name

Remove a software package (do not retain configuration files):

sudo apt-get purge package-name

Remove unneeded dependencies:

sudo apt-get autoremove

UsingPPA(Personal Package Archives)

PPAs (Personal Package Archives) are provided by the Ubuntu community for installing the latest software, but they come with certain risks. For instance, a new version of PHP might be released that is not included in the current Ubuntu distribution. If someone in the community has developed a package for this new version, it can be installed via a PPA. Below are the commands for installation and uninstallation, where 'ppa:author/ppa-name' needs to be replaced with the respective package name.

# add a PPA
sudo add-apt-repository ppa:author/ppa-name

# remove a PPA
sudo add-apt-repository --remove ppa:author/ppa-name

Setting Aliases

Aliases are shortcuts in the Linux command line designed to simplify input. The system file where aliases are stored is ~/.bash_profile. The following aliases can be added to this file.

alias apti="sudo apt-get install"
alias aptr="sudo apt-get remove"
alias aptar="sudo apt-get autoremove"
alias aptp="sudo apt-get purge"
alias apts="sudo apt-cache search"
alias aptinfo="sudo apt-cache show"
alias addppa="sudo add-apt-repository"
alias removeppa="sudo add-apt-repository --remove"

Once the aliases are set, if we want to install nginx, we can use the shortcut command below for installation.

apti nginx