How to Use the Apt-Get Command in Linux?


The Advanced Packaging Tool, or APT, is a powerful command-line tool used in Debian based systems like Ubuntu, Linux Mint, and others. The apt−get command is one of the most common ways to interact with APT. It's used to handle packages, allowing you to install, upgrade, and remove software on your Linux system.

In this guide, we'll walk you through the basics of using the apt−get command, complete with examples and their outputs.

1. Updating Package Lists: apt−get update

The first command you should know is apt−get update. This command retrieves information about the newest versions of packages and their dependencies. It doesn't install or upgrade any packages, but it updates the package lists for upgrades and new package installations.

Example

sudo apt−get update

Output

Hit:1 http://archive.ubuntu.com/ubuntu bionic InRelease
Get:2 http://archive.ubuntu.com/ubuntu bionic−updates InRelease [88.7 kB]
...
Reading package lists... Done

2. Upgrading Packages: apt−get upgrade

Once you've updated your package lists, you can upgrade your installed packages with apt−get upgrade. This command installs the newest versions of all packages currently installed on the system.

Example

sudo apt−get upgrade

Output

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
   libssl1.1 openssl
2 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,374 kB of archives.
After this operation, 1,024 B of additional disk space will be used.
Do you want to continue? [Y/n]

3. Installing Packages: apt−get install

To install a new package, use the apt−get install command followed by the package name.

Example

sudo apt−get install firefox

Output

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
   firefox−locale−en
Suggested packages:
   fonts−lyx
The following NEW packages will be installed:
   firefox firefox−locale−en
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 49.4 MB of archives.
After this operation, 182 MB of additional disk space will be used.
Do you want to continue? [Y/n]

4. Removing Packages: apt−get remove

If you want to remove a package but keep its configuration files, use the apt−get remove command.

Example

sudo apt−get remove firefox

Output

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be removed:
   firefox firefox−locale−en
0 upgraded, 0 newly installed, 2 to remove and 0 not upgraded.
After this operation, 182 MB disk space will be freed.
Do you want to continue? [Y/n]

5. Purging Packages: apt−get purge

If you want to remove a package along with its configuration files, use the apt−get purge command.

Example

sudo apt−get purge firefox

Output

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be removed:
   firefox* firefox−locale−en*
0 upgraded, 0 newly installed, 2 to remove and 0 not upgraded.
After this operation, 182 MB disk space will be freed.
Do you want to continue? [Y/n]

6. Autoremove: apt−get autoremove

When you install a package, it may depend on other packages. When you remove the package, its dependencies might not be needed anymore. To remove these unnecessary packages, you can use the apt−get autoremove command.

Example

sudo apt−get autoremove

Output

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
   libfreetype6 libjpeg−turbo8 libjpeg8 libjbig0 libtiff5
0 upgraded, 0 newly installed, 5 to remove and 0 not upgraded.
After this operation, 1,525 kB disk space will be freed.
Do you want to continue? [Y/n]

7. Cleaning the Package Cache: apt−get clean

The apt−get clean command is used to free up space by cleaning retrieved .deb files from the local repository.

Example

sudo apt−get clean

Output

// No output, but .deb files are removed from /var/cache/apt/archives/

8. Installing Specific Version of a Package: apt−get install package=version

Sometimes, you might need to install a specific version of a package. You can do this by appending =version to the package name in the apt−get install command.

Example

sudo apt−get install apache2=2.4.29−1ubuntu4.14

Output

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
   apache2−bin apache2−data apache2−utils libapr1 libaprutil1 libaprutil1−dbd−sqlite3 libaprutil1−ldap liblua5.2−0
Suggested packages:
   www−browser apache2−doc apache2−suexec−pristine | apache2−suexec−custom
The following NEW packages will be installed:
   apache2 apache2−bin apache2−data apache2−utils libapr1 libaprutil1 libaprutil1−dbd−sqlite3 libaprutil1−ldap liblua5.2−0
0 upgraded, 9 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,358 kB of archives.
After this operation, 5,353 kB of additional disk space will be used.
Do you want to continue? [Y/n]

9. Download a Package Without Installing: apt−get download

If you want to download a .deb package without installing it, you can use the apt−get download command.

Example

apt−get download apache2

Output

Get:1 http://archive.ubuntu.com/ubuntu bionic−updates/main amd64 apache2 amd64 2.4.29−1ubuntu4.14 [95.1 kB]
Fetched 95.1 kB in 1s (67.8 kB/s)

10. Check if a Package is Installed: dpkg −l

While not an apt−get command, dpkg −l is often used in conjunction with apt−get to check if a package is installed.

Example

dpkg −l | grep apache2

Output

ii  apache2       2.4.29−1ubuntu4.14  amd64  Apache HTTP Server
ii  apache2−bin   2.4.29−1ubuntu4.14  amd64  Apache HTTP Server (modules and other binary files)
ii  apache2−data  2.4.29−1ubuntu4.14  all    Apache HTTP Server (common files)

11. List all Installed Packages: dpkg −−get−selections

Again, while not an apt−get command, dpkg −−get−selections is a useful command to list all installed packages.

Example

dpkg −−get−selections

Output

adduser                                         install
apache2                                         install
apache2−bin                                     install
apache2−data                                    install
apt                                             install
...

Remember, the apt−get command is a powerful tool in your Linux arsenal. Always use it with care, and you'll find it makes managing your system much easier.

Conclusion

The apt−get command is a powerful tool for managing packages on your Linux system. With it, you can easily install, upgrade, and remove software. Remember to use sudo before each command to ensure you have the necessary permissions. Always be careful when removing packages to avoid accidentally deleting something important. Happy Linux−ing!

Updated on: 13-Jul-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements