Apt Linux Command with Examples


Introduction

If you are using Linux as your operating system, then you might be familiar with apt command. Apt stands for "Advanced Package Tool" and it is a package manager used in Linux distributions like Ubuntu, Debian, and others. Apt is a command-line tool that allows users to search, install, remove, and manage software packages on their Linux system. In this article, we will discuss various apt commands with examples that will help you to understand how to use apt in Linux.

Updating Package Lists

Before installing any package on your Linux system, it's important to update package lists. Apt uses package lists to know which packages are available for installation. To update package lists, use following command −

sudo apt update

This command will download latest package lists from repository. After executing this command, you will be able to install latest packages available for your Linux distribution.

Installing Packages

To install a package, you need to use apt install command followed by name of package you want to install. For example, to install Apache web server, use following command −

sudo apt install apache2

This command will download and install Apache web server on your Linux system.

Removing Packages

If you want to remove a package from your Linux system, use apt remove command followed by name of package you want to remove. For example, to remove Apache web server, use following command −

sudo apt remove apache2

This command will remove Apache web server from your Linux system.

Upgrading Packages

To upgrade installed packages on your Linux system, use apt upgrade command. This command will download and install latest version of packages that are already installed on your Linux system. To upgrade installed packages, use following command −

sudo apt upgrade

This command will download and install latest version of installed packages on your Linux system.

Listing Installed Packages

To list all packages that are installed on your Linux system, use apt list command. This command will display list of all installed packages along with their version numbers. To list all installed packages, use following command −

apt list --installed

This command will display list of all installed packages on your Linux system.

Searching for Packages

To search for a package, use apt search command followed by name of package you want to search for. This command will search repository for package and display results. To search for a package, use following command −

apt search apache2

This command will search for Apache web server in repository and display results.

Cleaning UP

When you install or remove a package on your Linux system, apt will keep downloaded package files in cache. This can take up a lot of disk space over time. To clean up cache, use apt clean command. This command will remove all downloaded package files from cache. To clean up cache, use following command −

sudo apt clean

This command will remove all downloaded package files from cache.

Autoremove Packages

Sometimes, when you remove a package from your Linux system, it may leave behind some dependencies. These dependencies are no longer needed and can be removed using apt autoremove command. To remove unnecessary dependencies, use following command −

sudo apt autoremove

This command will remove all unnecessary dependencies that are no longer required by any other package on your Linux system.

Checking Package Information

If you want to get more information about a package, use apt show command. This command will display detailed information about package, including its version, size, dependencies, and a brief description. To check package information, use following command −

apt show apache2

This command will display detailed information about Apache web server package.

Adding Repositories

If you want to install a package that is not available in default repository, you need to add a new repository. To add a new repository, use apt-add-repository command followed by URL of repository. For example, to add a repository for PHP packages, use following command −

sudo apt-add-repository ppa:ondrej/php

This command will add PPA (Personal Package Archive) repository for PHP packages.

Updating Package Cache

When you add a new repository, you need to update package cache to download package lists from new repository. To update package cache, use apt update command. For example, to update package cache after adding a new PHP repository, use following command −

sudo apt update

This command will download package lists from newly added PHP repository.

Purging Packages

If you want to completely remove a package from your Linux system, including its configuration files, use apt purge command followed by name of package. For example, to completely remove Apache web server package and its configuration files, use following command −

sudo apt purge apache2

This command will completely remove Apache web server package and its configuration files from your Linux system.

Installing Packages From a Specific Version

In some cases, you may want to install a specific version of a package that is not available in default repositories. To do this, you can download package from internet and install it using dpkg command. For example, to install version 3.3.1 of VLC media player, you can download package file (vlc_3.0.12-2_amd64.deb) and install it using following command −

sudo dpkg -i vlc_3.0.12-2_amd64.deb

This command will install VLC media player version 3.3.1 on your Linux system.

Creating a Local Repository

If you have a collection of packages that you want to install on multiple Linux systems, you can create a local repository. To create a local repository, you need to create a directory that contains packages and create a Packages.gz file that lists all packages in directory. You can then add local repository to your apt sources list and install packages using apt-get. For example, to create a local repository for packages in /var/myrepo directory, you can use following commands −

cd /var/myrepo
dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz
sudo echo "deb file:/var/myrepo /" | sudo tee -a /etc/apt/sources.list
sudo apt update
sudo apt install mypackage

These commands will create a local repository for packages in /var/myrepo directory, add it to your apt sources list, update package list, and install mypackage package from local repository.

Installing Packages From a Specific Repository

If you have multiple repositories on your Linux system, you can install a package from a specific repository by specifying repository in apt-get install command. For example, to install Apache web server package from universe repository, use following command −

sudo apt-get install apache2/universe

This command will install Apache web server package from universe repository.

Installing Packages Without Dependency

Sometimes, you may want to install a package without installing its dependencies. To do this, you can use --nodeps option with dpkg command. For example, to install MySQL server package without installing its dependencies, use following command −

sudo dpkg --install --nodeps mysql-server.deb

This command will install MySQL server package without installing its dependencies.

Installing Packages From a .deb File

If you have a package in a .deb file, you can install it using dpkg command. For example, to install Google Chrome browser from a .deb file, download file from internet and use following command −

sudo dpkg -i google-chrome-stable_current_amd64.deb

This command will install Google Chrome browser from .deb file.

Reinstalling Packages

If a package on your Linux system is corrupted or has some other issue, you can reinstall it using apt-get command with --reinstall option. For example, to reinstall Apache web server package, use following command −

sudo apt-get --reinstall install apache2

This command will reinstall Apache web server package on your Linux system.

Downgrading Packages

If a newer version of a package causes issues on your Linux system, you can downgrade to an earlier version using apt-get command with = option. For example, to downgrade Apache web server package to version 2.2.31-1ubuntu2, use following command −

sudo apt-get install apache2=2.2.31-1ubuntu2

This command will downgrade Apache web server package to version 2.2.31-1ubuntu2.

Conclusion

In this article, we have discussed various apt commands with examples. Apt is a powerful package manager that allows users to search, install, remove, and manage software packages on their Linux system. With help of these apt commands, you can easily manage packages on your Linux system. So, start using apt and make your Linux experience even better.

Updated on: 02-May-2023

591 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements