The most useful apt commands you should know for managing packages in Ubuntu, Debian and Linux Mint

Endrit Qerreti

Endrit Qerreti

In this tutorial, you will learn the most useful apt commands that you should know.

apt is the default package manager, which allows you to manage packages on your Ubuntu system and Debian based systems.

In this tutorial, we'll go through the most useful commands that you should know when it comes to managing your Linux machine packages.

Update the system

To update the system with apt package manager, you can do so by using the update command.

sudo apt update

Upgrade packages

Upgrading packages is also quite simple, and it can be done easily by using the upgrade command.

Upgrade packages by running the command below.

sudo apt upgrade

Upgrade packages/Install new Kernel

When a new kernel is available to be downloaded and installed in your machine, in order to install the new kernel, you need to use the dist-upgrade option.

dist-upgrade is like apt-upgrade , the difference is that dist-upgrade upgrades packages but it can also install new packages or dependencies and also remove these packages if it's necessary.

sudo apt dist-upgrade

Install a package

When it comes to installing a package in your system by using the apt package manager, you need to use the install command.

Install command allows you to install any package that is available to be downloaded from Ubuntu repositories or any other repository that is available in your machine.

To install a package, simply run the following.

sudo apt install package

💡
Note: replace: package with the package you want to install.

Reinstall a package

In cases when a package doesn't get installed correctly, you can easily reinstall it by using the reinstall command.

To reinstall a package, simply run the command below.

sudo apt reinstall package

Install a .deb file

If you have a .deb file downloaded in your machine, and want to install it via terminal, you can do so by using apt.

All you have to do is use the deb option as below.

sudo apt deb file.deb

Download a .deb file

Did you know that apt allows you to download a .deb file of an app? Let's say you need the .deb binary of firefox, to be able to install it in another machine that is not connected to the internet.

The download will download the .deb binary that is available in the repository.

For example, to download Firefox .deb finary, simply run the command below.

sudo apt download firefox

And the Firefox .deb binary will be downloaded on the same directory where you ran the command above.

To verify that firefox .deb binary was downloaded, run the following.

ls *firefox

View installed packages

To view all the applications/packages that are currently installed in your system, you need to use the list command.

apt list

If you want to check if a specific package is installed in your system, simply run the command below.

apt list package

For example, to search for firefox, you can do so by replacing package with firefox in the command above.

apt list firefox

And it should list all packages that contain the keyword firefox, that are currently installed in your system.

Remove a package

The remove command allows you to remove packages that are currently installed in your system.

To remove a package, all you have to do is, use the uninstall command.

sudo apt remove package

💡
Note: make sure to replace package with the package that you want to remove.

Remove a package and its config files

There are times when you need to uninstall a package on your system and its config files, to do, you need to use the --purge command along with with the remove command.

To remove a package and its config files that were created by the package, simply run the command below.

sudo apt remove package --purge

💡
Note: package is the package that we are removing and its config files.

Search for a package

Search function in apt is a very useful feature that allows you to search for a package, to ensure that the package you are looking for is available and it can be downloaded.

To search for a package with apt package manager, you can do so by using the search function.

sudo apt search package

💡
Note: in the command above we are searching for a package called package, so make sure to replace package with the package you want to look for.

Clean your local cache repository

The autoclean option in apt allows you to remove packages that are no longer needed.

What the autoclean option does is that it removes the .deb files downloaded in the local cache repository /var/cache/apt/archives which no longer are required in your system.

sudo apt autoclean

Remove dependencies that are no longer needed

When you install a package, apt also installs its dependencies automatically, and when you remove this package, these dependencies that were automatically installed by apt, are still installed in your system.

So to remove these dependencies, run the command below.

sudo apt autoremove

Add/Remove repositories from sources.list

There are many ways to add or remove repositories in Ubuntu via terminal. For example, one way is by using nano text editor to manually edit sources.list

sudo nano /etc/apt/sources.list

However, you don't have to remember source.list path in order to add or remove repositories in your system, as you can do the same thing by using apt.

edit-sources in apt allows you edit sources.list without having to specify which text editor or path of the sources.list.

To do so, simply run the command below.

sudo apt edit-sources

Next, you need to choose which text editor you want to use for editing source.list. For example, we selected nano text editor.

Now you should be able to add a new repository or remove an existing one.

Once you are done, press CTRL + X to save changes.

Conclusion

In this tutorial, you learned the most useful commands that you should know when it comes to using apt to manage packages in your Linux machine.