1191

I have a deb package for installation.

Shall I install by dpkg -i my.deb, or by apt?

Will both handle the software dependency problem well?

If by apt, how can I install from the deb by apt?

g_p
  • 15,676
Tim
  • 101,790

9 Answers9

1545

When you use apt to install a package, under the hood it uses dpkg. When you install a package using apt, it first creates a list of all the dependencies and downloads it from the repository.

Once the download is finished it calls dpkg to install all those files, satisfying all the dependencies.

So if you have a .deb file, you can install it by:

  1. Using:

    sudo dpkg -i /path/to/deb/file
    sudo apt-get install -f
    
  2. Using:

    sudo apt install ./name.deb
    

    Or

    sudo apt install /path/to/package/name.deb
    

    With old apt-get versions you must first move your deb file to /var/cache/apt/archives/ directory. For both, after executing this command, it will automatically download its dependencies.

  3. First installing gdebi and then opening your .deb file using it (Right-click -> Open with). It will install your .deb package with all its dependencies.

Note: APT maintains the package index which is a database (/var/cache/apt/*.bin) of available packages available in repo defined in /etc/apt/sources.list file and in the /etc/apt/sources.list.d directory. All these methods will fail to satisfy the software dependency if the dependencies required by the deb is not present in the package index.


Why use sudo apt-get install -f after sudo dpkg -i /path/to/deb/file (as mentioned in method 1)?

From man apt-get:

 -f, --fix-broken
           Fix; attempt to correct a system with broken dependencies in place.

When dpkg installs a package and a package dependency is not satisfied, it leaves the package in an "unconfigured" state and that package is considered broken.

The sudo apt-get install -f command tries to fix this broken package by installing the missing dependency.

g_p
  • 15,676
  • Thanks. How can I make "the .deb package present in the apt package list"? – Tim May 06 '15 at 13:26
  • Sorry @Tim, I didn't understand what you are asking. Pls add bit more detail in your comment. – g_p May 06 '15 at 15:17
  • http://askubuntu.com/questions/620056/install-a-package-moved-to-var-cache-apt-archives-manually/620059#comment876271_620059 – Tim May 06 '15 at 15:24
  • 2
    @Tim, apt-get is designed to install packages from repository. It maintains packages list as well as their respective repository address. So just moving a .dev file, which is not present in the apt package list, in /var/cache/apt/archives directory will not work. Because whenever you try to install this package using apt-get, it searches for its repository, which is not present. For this you have to create local repository which I think is useless. – g_p May 06 '15 at 15:41
  • Hello thank you for your answer - "You can install it using sudo dpkg -i /path/to/deb/file followed by sudo apt-get install -f" --> is this with two separate commands, or is it all on the same line? I was a litttle confused. – BenKoshy Jan 28 '16 at 22:04
  • 2
    @g_p thank you! im a little confused in your answer above. you write: "(it will work if the .deb package is present in the apt package list)" - I do not understand what is meant here. How can one ensure that the .deb package is present in the apt package list? – BenKoshy Jan 28 '16 at 22:16
  • 2
    @BKSpurgeon, I have added bit information in my answer. To find-out if a package is present in the index or not run apt-cache search <package name>. You can also use apt-cache policy <package name>, which will give some additional information as well. – g_p Jan 29 '16 at 15:10
  • Well, nowadays, you can right click and open the .deb file with Ubuntu Software Center too. That installs it for you. Dunno about the advantages and disadvantages of doing this though. – John Red Sep 21 '16 at 04:32
  • 1
    Will these methods still perform the signature checking usually performed by apt? Or is it possible for a tampered-with .deb file to be installed this way? – Boann Oct 11 '16 at 04:07
  • 1
    I'd like to point out as well for gamers in here that this is how you install discord. – Callat Apr 13 '17 at 22:39
  • Why sudo dpkg -i /path/to/deb/file is followed by sudo apt-get install -f? – Tim Apr 24 '17 at 03:19
  • @Tim,updated the answer. – g_p May 31 '17 at 17:20
  • 26
    Modern apt-get can be uset to install a package simply with apt-get install /path/to/package/name.deb. It would be neat if the author of this answer decided to integrate that. If not, I'll go add that answer sometime when I have enough rep here – Sam Hartman May 31 '17 at 19:10
  • Anyone able to get the /var/cache/apt/archives/ method to work. I like it however it doesn't work for me when I move the deb in update and then try to install the package. – michael.schuett Jun 20 '17 at 00:45
  • @mschuett please post your query as new question and give the relevant information. – g_p Jun 24 '17 at 09:00
  • 1
    Thanks for adding the comment about what apt-get install -f does. Thorough answer! – Mathias Sep 15 '17 at 17:44
  • I come to this answer once a month. For some reason I can not remember this ... :\ – Mathias Nov 09 '17 at 18:51
  • 1
    I came to this again because I got Unable to locate package. I used as in Windows just name of package after switching to archive folder. Forgot ./ for Unix... – Alex Martian May 28 '18 at 04:14
  • 1
    FYI, the ability to run apt install foo.deb was added in apt 1.1. – Sam Morris Sep 24 '18 at 13:30
  • Doesn't work for me: sudo dpkg -i libstdc++6_8-20180414-1ubuntu2_amd64.deb dpkg: warning: downgrading libstdc++6:amd64 from 8.2.0-1ubuntu2~18.04 to 8-20180414-1ubuntu2 dpkg: unrecoverable fatal error, aborting: files list file for package 'linux-modules-4.15.0-1027-aws' is missing final newline

    sudo apt install libstdc++6_8-20180414-1ubuntu2_amd64.deb

    apt: error while loading shared libraries: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: invalid ELF header

    – mvladk Jan 31 '19 at 08:20
  • Everyone above is writing /path/to/package/name.deb . But what is that path? How to find it? – Agent Zebra Mar 11 '19 at 18:26
  • 1
    @AgentZebra if you download the .deb to your ~/Downloads folder, just cd ~/Downloads then the path is just "." (here); e.g, sudo apt install ./install-me.deb , or if you prefer, sudo apt install ~/Downloads/install-me.deb – michael May 13 '19 at 03:19
  • sudo apt-get install -f /path/to/.deb worked for me – Tinkaal Gogoi Jul 05 '19 at 10:03
  • worked for me to install teamviewer on Ubuntu 16.04. Appreciate it
  • – Galapagos Jan 06 '20 at 05:51
  • 2
    You might need to use sudo dpkg --force-depends -i /path/to/package.deb – billyjmc Mar 05 '20 at 14:38
  • 1
    The sudo apt install ./foo.deb way I always get Note, selecting 'on_repo_foo' instead of './foo.deb'. dpkg -i seems the only way to force the installation of the .deb. – Pablo A Jan 15 '21 at 07:27
  • "Note: APT maintains the package index which is a database of available packages"
    Where dir. is it ?
    –  Jul 06 '21 at 16:56
  • @cantiknonokmu /var/cache/apt/*.bin – g_p Jul 08 '21 at 06:42
  • 1
    On modern distros you can also use apt-get install ./package.rpm. The key thing is to use an absolute path, or start it with a ./ – Spacen Jasset Sep 20 '21 at 15:12
  • When trying sudo apt install ./package.deb on my Ubuntu 22.04, I get an error as follows (the original german and my translation into english)
    N: Der Download wird als root und nicht Sandbox-geschützt durchgeführt, da auf die Datei »/home/user/Downloads/package.deb« durch den Benutzer »_apt« nicht zugegriffen werden kann. - pkgAcquire::Run (13: Keine Berechtigung)
    N: The download is conducted as root and is not Sandbox-protected, since the file »/home/user/Downloads/package.deb« can not be opened by the user »_apt«. - pkgAcquire::Run (13: no permission)
    – Dohn Joe Mar 16 '23 at 10:19
  • 1
    @DohnJoe please check this https://askubuntu.com/questions/1403337/download-is-performed-unsandboxed-as-root-as-file – g_p Mar 16 '23 at 16:00