0
0
Linux-cliHow-ToBeginner · 3 min read

How to Use apt-get Command in Linux: Syntax and Examples

The apt-get command in Linux is used to manage software packages by installing, updating, or removing them. You run it in the terminal with options like install, update, or upgrade followed by package names or flags.
📐

Syntax

The basic syntax of the apt-get command is:

  • sudo apt-get [option] [package_name]

Where:

  • sudo: Runs the command with administrator rights, needed for package management.
  • apt-get: The command to manage packages.
  • [option]: The action you want to perform, like install, update, or remove.
  • [package_name]: The name of the software package you want to manage.
bash
sudo apt-get install package_name
sudo apt-get update
sudo apt-get upgrade
sudo apt-get remove package_name
💻

Example

This example shows how to update the package list and install the curl package.

bash
sudo apt-get update
sudo apt-get 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, 563 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-get include:

  • Not using sudo, which causes permission errors.
  • Trying to install a package without updating the package list first, which may cause "package not found" errors.
  • Typing package names incorrectly.
  • Not running apt-get update regularly, leading to outdated package information.
bash
apt-get install curl
sudo apt-get update
sudo apt-get 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, 563 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-get commands:

CommandDescription
sudo apt-get updateRefresh package list from repositories
sudo apt-get upgradeUpgrade all installed packages to latest versions
sudo apt-get install package_nameInstall a new package
sudo apt-get remove package_nameRemove an installed package
sudo apt-get autoremoveRemove unused packages
sudo apt-get cleanClear downloaded package files

Key Takeaways

Always use sudo with apt-get to have the necessary permissions.
Run sudo apt-get update before installing or upgrading packages to get the latest package info.
Use apt-get install to add new software and apt-get remove to uninstall it.
Keep your system updated regularly with apt-get upgrade.
Check package names carefully to avoid errors.