apt-get

Updated on 28 Dec 2018

Can’t release lock

Sometimes you might get an error from apt-get that it could not get the lock

E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

In this situation you can run the following command to find out what process is currently using the lock file, and more importantly what the pid is.

sudo lsof /var/lib/dpkg/lock

You can now use the pid to kill the process.

kill 4564

Update package database with apt-get

apt-get basically works on a database of available packages. If you don’t update this database, the system won’t know if there are newer packages available or not. This is usually the first command you run in the apt-get library.

sudo apt-get update

Upgrade installed packages with apt-get

Once you have updated the package database, you can upgrade the installed packages. The most convenient way is to upgrade all the packages that have updates available. You can use the command below for this purpose:

sudo apt-get upgrade

To upgrade only a specific program, use the command below:

sudo apt-get upgrade <package_name>

Using apt-cache commands to search for packages

Searching for packages with the following apt-cache commands.

apt-cache search <search term>

showpkg

Once you know the exact package name, you can get more information about it such as version, dependencies etc by using the command below:

apt-cache showpkg <package_name>

apt-get command install packages

If you know the name of the package, you can easily install it using the command below:

sudo apt-get install <package_name>

Example: Install Bless hex editor.

How to remove installed packages with apt-get

You can also remove packages with apt-get.

sudo apt-get remove <package_name>

Example: Remove Bless hex editor.

Another way of uninstalling packages is to use purge.

sudo apt-get purge <package_name>

Difference between apt-get remove and apt-get purge?

  • apt-get remove just removes the binaries of a package. It doesn’t touch the configuration files
  • apt-get purge removes everything related to a package including the configuration files

So if you have to remove a particular software and install it again, your system will have the same configuration files. Of course, you will be asked to override the existing configuration files when you install it again.

Purge is particularly useful when you have messed up with the configuration of a program. You want to completely erase its traces from the system and perhaps start afresh.

How to clean your system with apt-get

You can also clean your system with apt-get and free up some disk space.

You can use the command below to clean the local repository of retrieved package files:

sudo apt-get clean

Another way is to use autoclean. Unlike the above clean command, autoclean only removes those retrieved package files that have a newer version now and they won’t be used anymore.

sudo apt-get autoclean

Another way to free up disk space is to use autoremove. It removes libs and packages that were installed automatically to satisfy the dependencies of an installed package. If the package is removed, these automatically installed packages are useless in the system. This command removes such packages.

sudo apt-get autoremove

In this case the command is removing the dependencies that were installed as part of Bless, in the earlier examples.