How to install a deb file, by dpkg -i or by apt?

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 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.


Install your foo.deb file with dpkg -i foo.deb. If there are some errors with unresolved dependencies, run apt-get install -f afterwards.


Here's the best way to install a .deb file on Ubuntu on the command-line:

sudo gdebi skype.deb

If you don't have gdebi installed already, install it using sudo apt install gdebi-core.

Why gdebi?

gdebi will look for all the dependencies of the .deb file, and will install them before attempting to install the .deb file. I find this much preferable than sudo dpkg -i skype.deb && sudo apt install -f. The latter is much too eager to remove dependencies in certain situations. For instance, when I tried to install Skype, it attempted to remove 96 (!) packages, including packages like compiz and unity! gdebi gave a much clearer error message:

 $ sudo gdebi skype.deb
 Cannot install 'libqtgui:i386'

(Here is the solution to that particular issue, by the way.)