How to install packages from Ubuntu installation media offline?

Ubuntu 18.04 installation media contains dkms and its dependencies in pool directory. But apt doesn't recognize USB flash as a package source.

How can I tell apt to pull packages with dependencies from an installation media connected to a USB port without copying files to cache or manually installing them using dpkg?


Offline install of dkms. Not network, install media on USB Stick

With Bionic Desktop install media, usb stick on /dev/sdb1

  1. Backup /etc/apt/sources.list Mount USB key and create new /etc/apt/sources.list
  2. Install dkms
  3. Revert changes on /etc/apt/sources.list

Backup sources.list and prepare new source

sudo cp /etc/apt/sources.list /etc/apt/sources.list.BCK
sudo touch /etc/apt/sources.list
if [ ! -d /media/apt ]; then sudo mkdir /media/apt; fi
sudo mount /dev/sdb1 /media/apt
sudo apt-cdrom add --cdrom=/media/apt
sudo apt-get update

Install

sudo apt-get install dkms

Revert changes on /etc/apt/sources.list

sudo umount /dev/sdb2
sudo rm /etc/apt/sources.list
sudo mv /etc/apt/sources.list.BCK /etc/apt/sources.list

I wanted to resolve the same issue after installing Ubuntu 18.04 desktop version from a live USB drive, but the other answer didn't quite work because my USB drive was automatically mounted after boot, and the apt command was expecting the USB drive to be at /media/cdrom.

After booting the newly installed Ubuntu, the USB drive was automatically mounted at /media/username/UBUNTU 18_0, where username will be your user name.

To use that as a source in apt I did the following:

sudo apt-cdrom --no-auto-detect --cdrom=/media/username/UBUNTU\ 18_0 -m add
sudo rmdir -f /media/cdrom
sudo ln -snf /media/username/UBUNTU\ 18_0 /media/cdrom
sudo apt-get update
sudo apt-get install package_name

The ln was necessary because apt was expecting the source to be in /media/cdrom in my case.

The apt-cdrom options are different from other answers. -m prevents it from being unmounted.

You can still do the backup and restore of the sources.list as written in the other answers.