Bird
Raised Fist0
Linux CLIscripting~5 mins

apt (Debian/Ubuntu) basics in Linux CLI - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you need to add or update software on your Debian or Ubuntu computer. The apt tool helps you find, install, and update software easily from the command line.
When you want to install a new program like a text editor or web browser.
When you need to update all the software on your computer to the latest versions.
When you want to remove software you no longer use to free up space.
When you want to check if a program is already installed on your system.
When you want to search for software by name or description before installing.
Commands
This command refreshes the list of available software and their versions from the online sources. It helps your system know about the latest software updates.
Terminal
sudo apt update
Expected OutputExpected
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB] Get:3 http://archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB] Fetched 215 kB in 1s (300 kB/s) Reading package lists... Done
This command installs the 'curl' program, which lets you transfer data from or to a server. It will download and set up curl if it is not already installed.
Terminal
sudo apt install curl
Expected OutputExpected
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, 500 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 (200 kB/s) Selecting previously unselected package curl. (Reading database ... 210000 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) ...
This command checks if 'curl' is installed by listing it among installed packages.
Terminal
apt list --installed curl
Expected OutputExpected
Listing... Done curl/focal,now 7.68.0-1ubuntu2.7 amd64 [installed]
→
--installed - Shows only installed packages
This command removes the 'curl' program from your system if you no longer need it.
Terminal
sudo apt remove curl
Expected OutputExpected
Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: curl 0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded. After this operation, 500 kB disk space will be freed. (Reading database ... 210000 files and directories currently installed.) Removing curl (7.68.0-1ubuntu2.7) ... Processing triggers for man-db (2.9.1-1) ...
Key Concept

If you remember nothing else from apt, remember: always run 'sudo apt update' before installing or upgrading software to get the latest package information.

Common Mistakes
Trying to install software without running 'sudo apt update' first
Your system may not know about the latest software versions, so it might install an old or missing package.
Always run 'sudo apt update' before 'sudo apt install' to refresh the package list.
Running apt commands without 'sudo'
Installing or removing software requires administrator rights, so the command will fail without 'sudo'.
Use 'sudo' before apt commands that change software, like install or remove.
Confusing 'apt remove' with 'apt purge'
'apt remove' deletes the program but leaves configuration files, which may clutter your system.
Use 'sudo apt purge' if you want to remove the program and its configuration files completely.
Summary
Use 'sudo apt update' to refresh your system's software list before installing or upgrading.
Use 'sudo apt install <package>' to add new software to your system.
Use 'apt list --installed <package>' to check if software is installed.
Use 'sudo apt remove <package>' to uninstall software you no longer need.

Practice

(1/5)
1. What does the apt update command do on a Debian/Ubuntu system?
easy
A. It upgrades all installed packages to the latest version.
B. It installs new software packages.
C. It removes installed software packages.
D. It refreshes the list of available packages and their versions.

Solution

  1. Step 1: Understand the purpose of apt update

    This command downloads the latest package lists from the repositories configured on the system.
  2. Step 2: Differentiate from other apt commands

    Unlike install or upgrade, it does not change installed software but only refreshes package info.
  3. Final Answer:

    It refreshes the list of available packages and their versions. -> Option D
  4. Quick Check:

    apt update refreshes package info [OK]
Hint: Remember: update refreshes info, install adds software [OK]
Common Mistakes:
  • Confusing update with install or upgrade
  • Thinking update removes packages
  • Running update without sudo and expecting changes
2. Which of the following is the correct syntax to install the package named curl using apt?
easy
A. apt install curl
B. apt get install curl
C. apt install -y curl
D. apt-install curl

Solution

  1. Step 1: Identify the correct apt install syntax

    The basic command is apt install package_name. Adding -y automatically confirms prompts.
  2. Step 2: Evaluate each option

    apt install curl is correct syntax but misses automatic confirmation. apt get install curl is incorrect syntax; it should be apt-get not apt get. apt-install curl uses invalid syntax. apt install -y curl is correct and includes -y for automation.
  3. Final Answer:

    apt install -y curl -> Option C
  4. Quick Check:

    Use apt install -y for automatic yes [OK]
Hint: Use -y to skip prompts during install [OK]
Common Mistakes:
  • Typing 'apt get' instead of 'apt-get'
  • Using hyphens incorrectly in command names
  • Forgetting sudo before apt commands
3. What will be the output of the command apt search nano if the package nano is available?
medium
A. Lists packages with 'nano' in their name or description.
B. Installs the nano package immediately.
C. Removes the nano package from the system.
D. Updates the nano package to the latest version.

Solution

  1. Step 1: Understand the purpose of apt search

    This command searches the package database for names or descriptions matching the search term.
  2. Step 2: Analyze the command effect

    It does not install, remove, or update packages; it only lists matching packages.
  3. Final Answer:

    Lists packages with 'nano' in their name or description. -> Option A
  4. Quick Check:

    apt search lists matching packages [OK]
Hint: Search shows matching packages, doesn't install or remove [OK]
Common Mistakes:
  • Thinking search installs packages
  • Confusing search with update or upgrade
  • Expecting no output if package is not installed
4. You run apt install git but get a permission denied error. What is the most likely fix?
medium
A. Run the command with sudo as sudo apt install git.
B. Use apt update before installing git.
C. Remove git first using apt remove git.
D. Restart the computer and try again.

Solution

  1. Step 1: Identify cause of permission denied

    Installing packages requires administrative rights, so running without sudo causes permission errors.
  2. Step 2: Apply correct command with elevated rights

    Prepend sudo to gain necessary permissions: sudo apt install git.
  3. Final Answer:

    Run the command with sudo as sudo apt install git. -> Option A
  4. Quick Check:

    Use sudo for system changes [OK]
Hint: Use sudo for install/remove commands [OK]
Common Mistakes:
  • Trying to install without sudo
  • Running update instead of fixing permissions
  • Restarting without fixing command permissions
5. You want to write a script that installs vim only if it is not already installed. Which command sequence correctly checks and installs using apt?
hard
A. apt search vim && sudo apt install vim
B. if ! dpkg -l | grep -q '^ii vim '; then sudo apt install -y vim; fi
C. sudo apt install vim && echo 'vim installed'
D. dpkg -s vim || apt install vim

Solution

  1. Step 1: Understand how to check if vim is installed

    Using dpkg -l lists installed packages; filtering with grep '^ii vim ' checks if vim is installed.
  2. Step 2: Use conditional to install only if not installed

    The if ! ...; then ...; fi structure runs install only if vim is missing.
  3. Step 3: Evaluate other options

    sudo apt install vim && echo 'vim installed' installs unconditionally. apt search vim && sudo apt install vim searches but does not check installation. dpkg -s vim || apt install vim misses sudo and may fail silently.
  4. Final Answer:

    if ! dpkg -l | grep -q '^ii vim '; then sudo apt install -y vim; fi -> Option B
  5. Quick Check:

    Check with dpkg, install if missing [OK]
Hint: Use dpkg -l and grep to check before install [OK]
Common Mistakes:
  • Installing without checking if package exists
  • Forgetting sudo in install command
  • Using apt search instead of dpkg for installed check
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard