How to Use apt Command in Linux: Basic Syntax and Examples
The
apt command in Linux is used to manage software packages, including installing, updating, and removing them. You run it with commands like apt update to refresh package lists or apt install package-name to install software.Syntax
The basic syntax of the apt command is:
apt [options] command [package_name]
Here, command is the action you want to perform, such as update, install, or remove. The package_name is the name of the software package you want to manage. options are optional flags to modify the command behavior.
bash
apt [options] command [package_name]
Example
This example shows how to update the package list and install the curl package using apt. Updating ensures you get the latest package info, and installing adds the software to your system.
bash
sudo apt update sudo apt install curl
Output
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
curl
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 159 kB of archives.
After this operation, 520 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 curl amd64 7.68.0-1ubuntu2.7 [159 kB]
Fetched 159 kB in 1s (234 kB/s)
Selecting previously unselected package curl.
(Reading database ... 123456 files and directories currently installed.)
Preparing to unpack .../curl_7.68.0-1ubuntu2.7_amd64.deb ...
Unpacking curl (7.68.0-1ubuntu2.7) ...
Setting up curl (7.68.0-1ubuntu2.7) ...
Processing triggers for man-db (2.9.1-1) ...
Common Pitfalls
Common mistakes when using apt include:
- Not running
sudobeforeaptcommands, which causes permission errors. - Skipping
apt updatebefore installing, leading to outdated package info. - Typing wrong package names, causing errors that the package cannot be found.
bash
apt install curl sudo apt update sudo apt install curl
Output
E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
curl
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 159 kB of archives.
After this operation, 520 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 curl amd64 7.68.0-1ubuntu2.7 [159 kB]
Fetched 159 kB in 1s (234 kB/s)
Selecting previously unselected package curl.
(Reading database ... 123456 files and directories currently installed.)
Preparing to unpack .../curl_7.68.0-1ubuntu2.7_amd64.deb ...
Unpacking curl (7.68.0-1ubuntu2.7) ...
Setting up curl (7.68.0-1ubuntu2.7) ...
Processing triggers for man-db (2.9.1-1) ...
Quick Reference
Here is a quick reference for common apt commands:
| Command | Description |
|---|---|
| apt update | Refresh package list from repositories |
| apt upgrade | Upgrade all installed packages to latest versions |
| apt install package_name | Install a new package |
| apt remove package_name | Remove an installed package |
| apt search keyword | Search for packages matching keyword |
| apt show package_name | Show details about a package |
Key Takeaways
Always run apt commands with sudo to have the necessary permissions.
Use apt update before installing or upgrading packages to get the latest info.
The apt command manages software packages with simple commands like install, remove, and update.
Check package names carefully to avoid errors during installation.
Use apt search to find packages if you are unsure of the exact name.