Ubuntu package

From 太極
Jump to navigation Jump to search

Contents

Package maintenance

wajig: Simplyfying Debian Administration

Package repository and /etc/apt/sources.list

echo "new line of text" | sudo tee -a /etc/apt/sources.list
# OR
# use 'add-apt-repository' command 
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 886DDD89
sudo add-apt-repository "deb http://deb.torproject.org/torproject.org $(lsb_release -s -c) main"
sudo apt-get update
sudo sh -c 'echo "deb http://cran.rstudio.com/bin/linux/ubuntu trusty/" >> /etc/apt/sources.list'
# Or the following if the OS is Ubuntu 
# (other derived distributions like Linux Mint do not count)
# because $(lsb_release -s -c) returns the CodeName which diffs from each Linux distributions.
# sudo sh -c 'echo "deb http://cran.rstudio.com/bin/linux/ubuntu $(lsb_release -s -c)/" >> /etc/apt/sources.list' 

gpg --keyserver keyserver.ubuntu.com --recv-key E084DAB9
gpg -a --export E084DAB9 | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install r-base

sources.list.d

This directory has more contents than the /etc/apt/sources.list file. For example, on my Mint 17.2

$ cat /etc/apt/sources.list
#deb cdrom:[Linux Mint 17.2 _Rafaela_ - Release amd64 20150627]/ trusty contrib main non-free
deb http://download.virtualbox.org/virtualbox/debian trusty contrib
$ ls /etc/apt/sources.list.d
additional-repositories.list  google-chrome.list                   shutter-ppa-trusty.list
docker.list                   mc3man-mpv-tests-trusty.list         stebbins-handbrake-releases-trusty.list
ekozincew-ppa-trusty.list     official-package-repositories.list   webupd8team-java-trusty.list
getdeb.list                   openshot_developers-ppa-trusty.list  wine-wine-builds-trusty.list

See an example of how to use it: https://apt.syncthing.net/

# Add the release PGP keys:
curl -s https://syncthing.net/release-key.txt | sudo apt-key add -

# Add the "stable" channel to your APT sources:
echo "deb https://apt.syncthing.net/ syncthing stable" | sudo tee /etc/apt/sources.list.d/syncthing.list

# Update and install syncthing:
sudo apt-get update
sudo apt-get install syncthing

E: Could not get lock /var/lib/dpkg/lock

I get the above message when I run sudo apt-get update on a virtual machine that I have not used for a long time (on Ubuntu click 'Show Applications' and type Logs). Many people also reported this problem. The following shows the reason why I got the error. My case seems to be related to the process apt.systemd.daily. Read the comment by Kjetil S from the link in Solution 1 below. One easy solution I did is to reboot the machine.

[email protected]:~/Downloads$ sudo apt install mupdf
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?
[email protected]:~/Downloads$ ps aux | grep -i apt
root      1327  0.0  0.0   4628   772 ?        Ss   09:26   0:00 /bin/sh /usr/lib/apt/apt.systemd.daily update
root      1371  0.0  0.0   4628  1780 ?        S    09:26   0:00 /bin/sh /usr/lib/apt/apt.systemd.daily lock_is_held update
_apt      3902  3.0  0.2  88932  8636 ?        S    09:29   0:05 /usr/lib/apt/methods/http
brb       4516  0.0  0.0  21536  1032 pts/0    S+   09:32   0:00 grep --color=auto -i apt

Solution 1:

Unable to lock the administration directory (/var/lib/dpkg/) is another process using it?.

I can reproduce the error from a clean boot. The most possible explanation is the answer from poolie:

the command-line apt overlaps with update-manager automatically polling. So if you try again in a few minutes that should fix it.

From my experience, after I wait about 10 minutes, ps -A | grep apt won't show anything.

Solution 2:

Fix “Unable to lock the administration directory (/var/lib/dpkg/)” in Ubuntu

# Method 1: Find and Kill all apt-get or apt Processes
$ ps -A | grep apt
1760 ?    00:00:00 apt.systemd.dai
3489 ?    00:00:00 aptd
$ sudo kill -9 1760
$ sudo kill -9 3489
$ sudo apt-get install keepass2
E: dpkg was interrupted, you must manually run 'sudo dpkg --configure -a' to correct the problem.

# Method 2: Delete the lock Files
sudo rm /var/lib/apt/lists/lock
# and 
sudo rm /var/lib/dpkg/lock

How To Force APT Package Manager To Use IPv4 In Ubuntu 16.04

https://www.ostechnix.com/how-to-force-apt-package-manager-to-use-ipv4-in-ubuntu-16-04/

dpkg error: Package is in a very bad inconsistent state

https://askubuntu.com/questions/148715/how-to-fix-package-is-in-a-very-bad-inconsistent-state-error

The following solution works for me (zramswap-enabler in this example).

# Remove the broken package via the following command:
sudo dpkg --remove --force-remove-reinstreq zramswap-enabler

# Install the package again:
sudo apt-get install zramswap-enabler

Difference between dpkg and aptitude/apt-get/gdebi

https://askubuntu.com/questions/309113/what-is-the-difference-between-dpkg-and-aptitude-apt-get

Some useful apt commands

19 Ubuntu / Debian Linux apt Command Examples

deb file

Show deb file information

dpkg-deb -I XXX.deb

dpkg-deb -c XXX.deb # list file content

Install a deb file

# dpkg gives more clear about the error message
sudo dpkg -i application.deb
# Or
sudo apt install application.deb

However, installing from a .deb file through dpkg does not resolve dependency issues the same way installation through apt will. See here.

So you should run sudo apt-get install -f to fix and complete the installation if any errors were encountered by dpkg.

sudo apt install -f dep1.deb dep2.deb dep3.deb

A better way to install a deb file is using the gdebi tool.

Remove deb packages

See the man page.

dpkg -l | grep 'tcl'
sudo dpkg -r tcl8.4 # -r means remove/uninstall
sudo dpkg -P tcl8.4 # -P means purge

dpkg -l | grep 'tcl'   (optional)

gdebi: an improvement over dpkg

How does gdebi automatically gets missing dependancies ? Can I use gdebi for all other installations of .deb packages?

sudo apt-get install gdebi-core

# wget newPackage.deb
# sudo gdebi -n newPackage.deb

deb files caches

See http://askubuntu.com/questions/444240/ubuntu-updates-blocked-apt-get

sudo rm -f /etc/apt/sources.list.d/*    # PPA list

sudo dpkg -i /var/cache/apt/archives/*.deb

sudo dpkg --configure -a

apt-cache

/var/lib/apt/lists

https://unix.stackexchange.com/questions/217369/clear-apt-get-list

List all available packages (from repositories)

apt-cache pkgnames # pkgnames is part of the command

Show package information

apt-cache show <package_name>
apt-cache show <package_name> | grep Version

How can I see all versions of a package that are available

We can use the show or madison parameter in apt-cache. For example,

apt-cache show <package_name> | grep Version
apt-cache madison <package_name> # wide output, include repository URLs

Show the candidcate version and whether any version is installed

apt-cache policy <package_name>

Check dependencies for specific packages

$ apt depends r-base
r-base
  Depends: r-base-core (>= 3.6.3-1bionic)
  Depends: r-recommended (= 3.6.3-1bionic)
  Recommends: r-base-html
  Recommends: r-doc-html
  Suggests: elpa-ess
 |Suggests: r-doc-info
  Suggests: r-doc-pdf

$ apt-cache showpkg <package_name>

List racing games package (kind of search packages by key words)

apt-cache search racing game
apt-cache search vsftpd

depends, recommends and suggests

How to install suggested packages in apt-get

By default, apt-get installs recommended packages.

Your option --install-suggests adds in the suggested packages and their suggested dependencies.

Note that --install-suggests will be recursive. It may install a lot of packages.

Upgrade a package

apt-get install <package_name>

Install specific version of the package

Use the "=" with the package-name and append desired version.

sudo apt-get install vsftpd=2.3.5-3ubuntu1

Remove a package

Linux uninstall package / software using the CLI

sudo apt-get remove <package_name>

To completely remove a package including their configuration files

sudo apt-get purge <package_name>
sudo apt-get remove --purge <package_name>

Rollback an apt-get upgrade

http://www.cyberciti.biz/howto/debian-linux/ubuntu-linux-rollback-an-apt-get-upgrade/

Clean up disk space

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

sudo apt-get clean

Auto clean up apt-get cache

sudo apt-get autoclean

The 'autoclean' command deletes all .deb files from /var/cache/apt/archives to free up disk space.

What is difference between the options "autoclean", "autoremove" and "clean"? From the apt-get man page: autoclean: Like clean, autoclean clears out the local repository of retrieved package files. The difference is that it only removes package files that can no longer be downloaded, and are largely useless. This allows a cache to be maintained over a long period without it growing out of control. The configuration option APT::Clean-Installed will prevent installed packages from being erased if it is set to off.

Download only source code of package

sudo apt-get --download-only source <package_name>

To download and unpack source code of a package

sudo apt-get source <package_name>

To download, unpack and compile a package

sudo apt-get --compile source <package_name>

Download without installing

sudo apt-get download <package_name>

Check change log of package

Note that the change log may not be found.

sudo apt-get changelog <package_name>

Simulate installing packages

$ sudo apt-get -s install packagename

$ sudo apt-get -s build-dep packagename
$ man apt-get

Generally, it is good to build-dep then install package.

Check broken dependencies

sudo apt-get check

Search missing package's full name

Use the apt-file command. See this post

At first, install apt-file command and prepare it.

$ sudo apt-get install apt-file
$ sudo apt-file update

To find zlib.h,

$ apt-file search zlib.h

It reports too many result. Let’s narrow down.

$ apt-file search /usr/include/zlib.h
zlib1g-dev: /usr/include/zlib.h

Now you know zlib.h is in zlib1g-dev package.

$ sudo apt-get install zlib1g-dev

Find package information before installing it

Use "-s" option for simulation. No sudo is necessary.

apt-get -s install PACKAGENAME

The output is too much (include other dependences)

A better way is to use aptitude (which is not installed by default in Ubuntu)

aptitude search <package> -F "%c %p %d %V"

For example,

[email protected]:~/qt-4.8.5/bin$ aptitude search  qtcreator -F "%c %p %d %V"
p qtcreator                         lightweight integrated development environme 2.5.0-2       
p qtcreator:armel                   lightweight integrated development environme 2.5.0-2       
p qtcreator-dbg                     debugging symbols for Qt Creator IDE         2.5.0-2       
p qtcreator-dbg:armel               debugging symbols for Qt Creator IDE         2.5.0-2       
p qtcreator-doc                     documentation for Qt Creator IDE             2.5.0-2   

If a package is already installed, we can use the following way to check version number.

apt-show-versions <package>

dpkg Command : View Package Information On Debian/Ubuntu/Mint Systems

dpkg -s r-base

aptitude Command : View Package Information On Debian/Ubuntu/Mint Systems

aptitude show htop

List files in a package

dpkg -L <package_name>

Search installed packages

dpkg -l libgtk* | grep -e '^i'
dpkg -l libpng* | grep -e '^i'
dpkg -l libjpeg* | grep -e '^i'

To search x-org related packages

[email protected]:~$ dpkg -l | grep xserver-xorg | awk '{$1=$3=$4=""; print $0}'  # Skip columns 1,3,4
 imx-xserver-xorg-extension-viv-9t6-hdmi   Freescale Xorg server driver extension for HDMI performance
 imx-xserver-xorg-video-viv-9t6   Xorg server driver for imx6, vivante
 xserver-xorg   X.Org X server
 xserver-xorg-core   Xorg X server - core server
 xserver-xorg-dev   Xorg X server - development files
 xserver-xorg-input-all   X.Org X server -- input driver metapackage
 xserver-xorg-input-evdev   X.Org X server -- evdev input driver
 xserver-xorg-input-synaptics   Synaptics TouchPad driver for X.Org server
 xserver-xorg-input-wacom   X.Org X server -- Wacom input driver
 xserver-xorg-video-all   X.Org X server -- output driver metapackage
 xserver-xorg-video-fbdev   X.Org X server -- fbdev display driver
 xserver-xorg-video-modesetting   X.Org X server -- Generic modesetting driver
 xserver-xorg-video-omap   X.Org X server -- OMAP display driver
 xserver-xorg-video-vesa   X.Org X server -- VESA display driver

List of installed packages

dpkg --get-selections 
# Or using [https://wiki.debian.org/ListInstalledPackages dpkg-query] utility.
dpkg-query -l

Show A History Of Installed, Upgraded Or Removed Packages

https://www.linuxuprising.com/2019/01/how-to-show-history-of-installed.html?m=1

# recently installed packages, their version number
grep "install " /var/log/dpkg.log

# recently upgraded packages, as well as the old and new package version
grep "upgrade " /var/log/dpkg.log

# recently removed packages
grep "remove " /var/log/dpkg.log

/var/log/dpkg.log contains the package install, update and remove history for the current month. For the previous month, read the /var/log/dpkg.log.1 log file. Want to go back even more in the dpkg history? Use zgrep instead of grep, and read /var/log/dpkg.log.2.gz, /var/log/dpkg.log.3.gz, /var/log/dpkg.log.4.gz and so on (up to 12 months),

grep "install " /var/log/dpkg.log.1

zgrep "upgrade " /var/log/dpkg.log.2.gz

Install a list of packages

How to get list of installed packages on Ubuntu / Debian Linux

Suppose we want to install all packages currently installed on server 1 onto server 2, we can do that by

# server 1
dpkg --get-selections | grep -v deinstall > mylist.txt
# server 2
sudo dpkg --set-selections < mylist.txt

Show (sort) package size

dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n

Check if a library is installed or not (ldconfig)

Use ldconfig -p | grep LIBNAME ('-p' is used to print the list stored in the current cache). For example, to check if libxml is installed or not, then following code will output the number of matches if it was found.

EXIST=`ldconfig -p | grep libxml | wc -l`
if [ $EXIST -ne 0 ]; then echo $EXIST; fi

The following code will search "libQt" and the library version will be shown in the file name.

ldconfig -p | grep libQt
# On Ubuntu 14.04, 16.04:
# libQtGui.so.4 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libQtGui.so.4
# On Ubuntu 17.10, 18.04:
# libQt5Gui.so.5 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5

To install libQtGui.so.4 in Ubuntu 18.04, run the command

sudo apt-get install libqtgui4

See also the ldd command to print all dynamic dependencies of an executable program.

How to uninstall software

http://www.howtogeek.com/229699/how-to-uninstall-software-using-the-command-line-in-linux/

Upgrade software packages

Upgrade all the currently installed software packages on the system

sudo apt-get upgrade

If you want to upgrade, unconcerned of whether software packages will be added or removed to fulfill dependencies, use

sudo apt-get dist-upgrade

List of available (uninstalled) packages

aptitude -F "%p" search "?not(?installed)"

See http://superuser.com/questions/408082/how-do-i-get-a-list-of-available-i-e-uninstalled-packages-in-debian

Check Change Log of Package

sudo apt-get changelog PKGNAME

Clean up/remove packages

ubuntugeek.com

PPA

A Guide to Using PPA Repositiories in Ubuntu

unmet dependencies after adding a PPA & apt --fix-broken

http://askubuntu.com/questions/140246/how-do-i-resolve-unmet-dependencies-after-adding-a-ppa

$ sudo apt-get install libgl1-mesa.dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Note, selecting 'libgl1-mesa-dev-lts-quantal' for regex 'libgl1-mesa.dev'
Note, selecting 'libgl1-mesa-dev' for regex 'libgl1-mesa.dev'
Note, selecting 'libgl1-mesa-dev-lts-saucy' for regex 'libgl1-mesa.dev'
Note, selecting 'libgl1-mesa-dev-lts-trusty' for regex 'libgl1-mesa.dev'
Note, selecting 'libgl1-mesa-dev-lts-utopic' for regex 'libgl1-mesa.dev'
Note, selecting 'libgl1-mesa-dev-lts-vivid' for regex 'libgl1-mesa.dev'
Note, selecting 'libgl1-mesa-dev-lts-raring' for regex 'libgl1-mesa.dev'
libgl1-mesa-dev is already the newest version.
libgl1-mesa-dev set to manually installed.
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 libgl1-mesa-dev : Conflicts: libgl-dev
                   Conflicts: libgl1-mesa-dri-dev
 libgl1-mesa-dev-lts-utopic : Depends: mesa-common-dev-lts-utopic (= 10.3.2-0ubuntu1~trusty2) but it is not going to be installed
                              Depends: libgl1-mesa-glx-lts-utopic (= 10.3.2-0ubuntu1~trusty2) but it is not going to be installed
                              Conflicts: libgl-dev
                              Conflicts: libgl1-mesa-dev
                              Conflicts: libgl1-mesa-dri-dev
 libgl1-mesa-dev-lts-vivid : Depends: mesa-common-dev-lts-vivid (= 10.5.9-2ubuntu1~trusty2) but it is not going to be installed
                             Depends: libgl1-mesa-glx-lts-vivid (= 10.5.9-2ubuntu1~trusty2) but it is not going to be installed
                             Conflicts: libgl-dev
                             Conflicts: libgl1-mesa-dev
                             Conflicts: libgl1-mesa-dri-dev
                             Conflicts: xorg-renamed-package-lts-utopic
E: Unable to correct problems, you have held broken packages.

Unmet dependencies when trying to install r-base (Works on Ubuntu 16.04).

sudo apt --fix-broken install
sudo apt-get update
sudo apt-get upgrade

Another case: I installed R 4.0.0 using sudo add-apt-repository --yes ”ppa:edd/r-4.0” by following this, then I remove and install the official one. The above 3 commands does not help. (Ubuntu 18.04).

The following packages have unmet dependencies:
 r-base : Depends: r-recommended (= 4.0.0-1.1804.0) but 4.0.0-1.1804.1 is to be installed
E: Unable to correct problems, you have held broken packages.

The solution is to use Aptitude command. See How to Fix Unmet Dependencies Error on Ubuntu

$ sudo aptitude install r-base                                   9:55:45
The following NEW packages will be installed:
  r-base{b} 
0 packages upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 43.3 kB of archives. After unpacking 62.5 kB will be used.
The following packages have unmet dependencies:
 r-base : Depends: r-recommended (= 4.0.0-1.1804.0) but 4.0.0-1.1804.1 is installed
The following actions will resolve these dependencies:

     Keep the following packages at their current version:
1)     r-base [Not Installed]                             



Accept this solution? [Y/n/q/?] n
The following actions will resolve these dependencies:

     Downgrade the following packages:                                       
1)     r-recommended [4.0.0-1.1804.1 (now) -> 4.0.0-1.1804.0 (bionic-cran40)]



Accept this solution? [Y/n/q/?] 
The following packages will be DOWNGRADED:
  r-recommended 
The following NEW packages will be installed:
  r-base 
0 packages upgraded, 1 newly installed, 1 downgraded, 0 to remove and 0 not upgraded.

Fix broken Ubuntu

How To Fix Broken Ubuntu OS Without Reinstalling It

$ sudo rm /var/lib/apt/lists/lock
$ sudo rm /var/lib/dpkg/lock
$ sudo rm /var/lib/dpkg/lock-frontend
$ sudo dpkg --configure -a
$ sudo apt clean
$ sudo apt update --fix-missing
$ sudo apt install -f
$ sudo dpkg --configure -a
$ sudo apt upgrade
$ sudo apt dist-upgrade
$ sudo reboot