9 Most Useful Apt Commands On Debian, Ubuntu & Linux Mint
In this tutorial, I’m going to show you how to use apt-get to manage software packages on Linux from the terminal. apt-get is the default command line package manager on Debian-based Linux distributions such as Debian, Ubuntu, Linux Mint, elementary OS,etc.
Install Software Packages From Repository
apt-get update
Before you install any software, you always want to make sure your local package index is up to date. The local package index contains a list of all packages names, including their version numbers, that can be installed from your software repository.
sudo apt-get update
sudo
means switch user and do something. On every Linux distribution, you need root privilege to manage software package and sudo
allows you to temporarily obtain root privilege.
apt-get install
apt-get install is the command to install software packages. You need to add the package name after it. For example, you can install chromium browser on Ubuntu using the following command.
sudo apt-get install chromium-browser
Sometimes you will be asked if you really want to install the package. If you want to automatically answer yes to these question, you can add -y option like below.
sudo apt-get install -y chromium-browser
In the picture above you can see that chromium-browser is already installed on my system.
Before you install packages using apt-get install, you need to know the package name in your repository. This may sounds like a trouble at first, but once you are familiar with package names, it can be convinient to install software packages from the terminal especially when you are working on a remote Linux server.
apt-get will first download .deb
package installer into /var/cache/apt/archives directory, then it install package on your system.
Install Multiple Packages With One Command
You can use apt-get install to install multiple packages at once, for example,
sudo apt-get install wireshark nmap aircrack-ng
If a package installation failed for whatever reason, you can run sudo apt install
without any package name, then it will attempt to continue the previously failed installation.
apt-get upgrade
apt-get upgrade command is used to upgrade every upgradable package on your system.
sudo apt-get upgrade
It will tell you what packages will be upgraded and ask you if you really want to upgrade them. You can add -y option to this command to automatically answer yes to questions.
sudo apt-get -y upgrade
apt-get remove
This command will remove packages from your system, for example, to remove firefox:
sudo apt-get remove firefox
It will not remove the configuration file of that package.
apt-get purge
This command will remove packages and their configuration files.
sudo apt-get purge firefox
apt-get clean
When installing or upgrading packages, apt-get will download .deb package installers from repositories to /var/cache/apt/archives/ directory on your file system. apt-get clean command can help you remove all of those .deb files. Most of the time, you don’t need those .deb file any more.
You can use the following command to check how many spaces those .deb files occupy.
du -sh /var/cache/apt/archives
apt-get autoclean
apt-get autoclean can also be used to remove .deb file under /var/cache/apt/archives. However, it only remove those .deb files that can no longer be downloaded from repositories. In other words, these package are no longer maintained by Debian or Ubuntu. Or, those packages have a new name in repository.
apt-get autoremove
This command will remove dependencies that are no longer needed.
apt-get dist-upgrade
This is a command that may confuse people. On Debian you use this command to upgrade the version of your system, for example, upgrade Debian 7 to Debian 8. But on Ubuntu, this command will only upgrade Linux kernel and previously kept-back packages. You need to use do-release-upgrade command to upgrade your version of Ubuntu system.
Manually Install deb Packages
dpkg -i
If you downloaded a deb package which has .deb extension from the Internet, you will need dpkg tool to install it on your system.
For example, head over to Google Chrome download page and download the Chrome deb package. Once downloaded, change your current working directory to the download directory and issue the following command to install the deb package onto your system.
sudo dpkg -i google-chrome-stable*.deb
gdebi
A drawback of dpkg is that it can not automatically resolve dependency problem. If your downloaded deb package require another package which is not installed on your system, you have to manually install the other package and then install the deb package.
A great tool called gdebi can also be used to install deb package. The beauty of gdebi is that it will automatically install all dependencies for you. To install gdebi on your system:
sudo apt-get install gdebi
Its syntax is as follows:
sudo gdebi <package.deb>
How to Fix Broken Package Dependencies
If you install software from a third-party repository, there’s a chance that you will encounter broken package dependencies like below.
You can try fixing this problem with:
sudo apt --fix-broken install
If the above command doesn’t work, you can use aptitude
, which is another powerful package manager for Debian/Ubuntu.
sudo aptitude install
However, aptitude isn’t pre-installed. You need to install it beforehand with:
sudo apt install aptitude
Sometimes, you can install a specific version of the software to solve dependency problems.
sudo apt install libpcre2-8-0=10.39-3build1 libpcre2-16-0=10.39-3build1
Packages Kept back?
When you upgrade the system (sudo apt upgrade
), you might encounter the following problem.
The following packages have been kept back: python-attr python-funcsigs python-numpy python-pkg-resources python-pluggy python-py python-pytest python-setuptools python-six python-tk 0 upgraded, 0 newly installed, 0 to remove and 10 not upgraded.
You can install them by
sudo apt install package_name
If that doesn’t work, you can check if these packages are marked hold from upgrade.
sudo apt-mark showhold
Pacakges Not Configured
If a package fails to install because it’s not configured, then you need to run the following command to reconfigure.
sudo dpkg --configure -a
It will show you why a package isn’t configured. For example, when I upgrade the openmpi-bin
package on Ubuntu 22.04, it produces the following error.
update-alternatives: error: /var/lib/dpkg/alternatives/mpi corrupt: slave link same as main link /usr/bin/mpicc
To fix this error, I deleted the update-alternatives for openmpi.
sudo rm -f /etc/aternatives/mpi* /var/lib/dpkg/alternatives/mpi*
Then reinstall openmpi-bin
.
sudo apt install openmpi-bin
Package Force Removal
If you don’t want to use a package anymore, but there’s a problem uninstalling it with sudo apt remove <package_name>
, you can use the following command to force remove it.
sudo dpkg --remove --force-remove-reinstreq package_name
Hello is it way to install list local packages via gdebi