Ugacomp

How to use the apt command in Linux | Syntaxes & Examples

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

apt‘ stands for Advanced Package Tool, and it’s a command-line utility tool we use to update, install, upgrade, and remove packages from Linux systems.

The apt command-line tool provides the mechanism to manage various packages on a Linux system.

Executing apt commands based on user privileges

In Linux, the root user is the system administrator who has complete control over the operating system and its settings. The root user is also known as the “superuser” and has access to all files and directories on the system, as well as the ability to execute any command or program.

On the other hand, a sudo user is a regular user who has been granted the ability to use the sudo command to execute privileged commands on a Linux system.

RECOMMENDED READING: How to Install WordPress on a VPS Server using Cloudron?

apt commands as root and sudo users

When you’re a root user, and you’re running apt commands on Linux, you don’t have to tell the system your superuser status. By default, the root user has privileged and administrative access to the system without facing limitations. For example, if you need to update your Linux packages using the terminal, all you have to do is just run the following command using the apt tool;

apt update

But for the case of sudo users, by default, they have to always tell the system of their superuser privileges when running certain commands using the apt command-line utility. For example, if a sudo user needed to update Linux packages on the system, they will always be required to prefix the apt update command with sudo as seen below;

sudo apt update

Also, sudo users may be required to type in their password whenever they run the apt command to install or update a certain package on the system. This is not the case with the root user as they’re not required to put their password credentials whenever they need to install a certain script or package on Linux.

Furthermore, a root user can also choose to prefix the apt commands with sudo but they don’t have to do it. In other words, a root user can optionally choose to prefix every apt command they run with sudo, but in a real sense, it’s not necessary.

RECOMMENDED READING: Step-by-Step Guide to Installing FTP on Ubuntu Server

apt command syntaxes and examples

You’re going to use apt every time you need to execute privileged commands on a Linux system. Basically, the apt command will always be part of your life whenever you’re using a Linux platform. The following are some of the command apt syntaxes and arguments you’re likely to use when interacting with any Linux platform:

‘apt update’ syntax

If you needed to update your Linux packages using the terminal, you would have to run the apt update command. For root users, you would just type the following command without having to prefix it with sudo:

apt update

For sudo users as we said earlier, you would need to prefix sudo to your apt commands every time. And if you want to update packages as a sudo user, the command syntax would be as follows:

sudo apt update

‘apt upgrade’ syntax

The apt upgrade command is used in Linux-based operating systems that use the Advanced Package Tool (apt) package management system to update installed packages to their latest versions.

To use the apt upgrade command, you need to first run the apt update command as we explained earlier. Below is the syntax we can use:

sudo apt update
sudo apt upgrade

The above two commands will first make a general update of all installed packages on the system and then upgrade them to their respective latest versions.

On the other hand, you can run the upgrade command by targeting a specific package. This means that the upgrade version will only affect the package you’ve defined or specified in the command.

RECOMMENDED READING: How can I simulate traffic on a Linux server using Apache Bench?

For example. let’s say you want to upgrade apache2 package to the latest version, what we need to do is first run the update command and then run the upgrade command targeting this specific package as seen below;

sudo apt update
sudo apt --only-upgrade install apache2

So, the syntax for upgrading a specific package using the apt upgrade command is seen as follows;

sudo apt update
sudo apt upgrade package-name

‘apt install’ syntax

If you needed to install a certain package on your Linux system, you would need to run the apt install command and below is the syntax:

apt install package-name

And for sudo users, you have to add the sudo prefix to the apt install command as seen below

sudo apt install package-name

For example, let’s say you need to install apache server on your Linux, the installation command for this would be as follows:

sudo apt install apache2

In the above command, apache2 is the name of the package we need to install Apache server on our system.

RECOMMENDED READING: How to use Alibaba Cloud Free credit to Set up an Ubuntu server

‘apt remove’ syntax

The apt remove is a command that allows you to remove a package from your system. When you use this command, apt will remove the package and any dependencies that are no longer required by other packages. Here is how to use apt remove command:

sudo apt remove package-name

For example, if you needed to remove apache2 package from your system, the apt remove command would be as follows;

sudo apt remove apache2

You need to be careful when using the apt remove command as any unintended action to remove packages can break your system. To run this command, you must know what you’re doing.

‘apt autoremove’ syntax

apt autoremove is a command used in Linux-based operating systems to remove packages that were automatically installed as dependencies of other packages and are no longer needed.

RECOMMENDED READING: Solved: Ant Media stream refusing to play after embedding

When you install a package on your system, other packages may be installed as dependencies to ensure that the original package runs correctly. However, if you later remove the original package, the dependencies may still remain on your system. This is where apt autoremove comes in handy – it removes any packages that were installed as dependencies but are no longer needed by any installed package.

So, to remove all of them, and since your system no longer needs them, you can apply the apt autoremove command as seen below;

sudo apt autoremove

It’s a good idea to run apt autoremove regularly to keep your system clean and free up disk space

However, be careful when using this command as it may remove packages that you still need. Always double-check the list of packages that will be removed before confirming the command.

‘apt search’ syntax

apt search is a command in Linux-based operating systems that allows you to search for packages in the distribution’s software repository. To search for a package on the system, here is the command syntax:

sudo apt search search_term

For example, if you want to search for a text editor package, you can type the following command;

sudo apt search text editor 

RECOMMENDED READING: How can I update Ant Media Server on Ubuntu?

The above command will return a list of text editor packages available in the repository.

‘apt show’ syntax

This apt show command shows information about packages installed on your Linux system, and the syntax is as follows:

sudo apt show package-name

For example, to show information about the nginx package installed on your system, here is the command you would use;

sudo apt show nginx

apt command parameters

The apt command has several parameters that can be used to modify its behavior. Sure, here’s a table that summarizes the commonly used apt parameters:

ParameterDescription
-y or --yesAutomatically answers “yes” to prompts
-q or --quietRuns the command in quiet mode, suppressing most output
-qq or --really-quietRuns the command in really quiet mode, suppressing all output except for errors
-h or --helpDisplays a help message and exits
-v or --versionDisplays version information and exits
-c or --config-fileSpecifies an alternate configuration file to use
-o or --optionSpecifies a configuration option to override the default value

Some of these parameters can be combined with each other, for example, -yq can be used to run the command in quiet mode and automatically answer “yes” to prompts. Also, there are many other parameters that can be used with the apt command, so make sure to check the man apt page for a complete list.

RECOMMENDED READING: How to install and configure UFW firewall on Ubuntu Linux

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.