Ubuntu 16.04: Install package from repository in local machine

This article will describe creating repository in local machine and installing package from it.

1 Download deb packages

Download deb packages of ubuntu-desktop and dependencies with this script.

$ sudo apt install -y apt-rdepends
$ mkdir deb
$ cd deb
$ ../download-deb-package.sh ubuntu-desktop
$ cd ..

2 Create GPG key

When creating GPG key, the following message will be output and console will be hung.

Not enough random bytes available. Please do some other work to give

For avoid this hung, update /dev/urandom with rng-tools.

$ sudo apt install -y rng-tools
$ sudo rngd -r /dev/urandom

Create gpg config file.

$ cat <<EOF > gpg.txt
Key-Type: RSA
Subkey-Type: RSA
Name-Real: hiroom2
Expire-Date: 0
%pubring public.key
%secring signing.key
%commit
EOF

Create GPG key with gpg batch mode, and import GPG key.

$ gpg --batch --gen-key gpg.txt
$ gpg --import public.key signing.key

3 Create repository

Install reprepro which creates repository with apt.

$ sudo apt install -y reprepro

Get subkey value for GPG key of repository.

$ dir=`pwd`
$ subkey=`gpg --list-keys --keyring ${dir}/public.key --no-default-keyring | \
grep sub | awk '{ print $2 }' | awk -F'/' '{ print $2 }'`

Create repo directory and store reprepro config file to there.

$ mkdir repo
$ cd repo
$ mkdir conf
$ cat <<EOF > conf/distributions
Codename: xenial
Architectures: amd64
Components: main
SignWith: ${subkey}
EOF

Create repository with reprepro.

$ reprepro includedeb xenial ~/deb/*.deb

Copy GPG public key to repo directory.

$ cp ../public.key .

4 Add repo to repository list

Add repo to repository list with apt-add-repository.

$ sudo apt-add-repository "deb file:/home/hiroom2/repo xenial main"

apt-add-repository changes /etc/apt/sources.list as below.

$ diff -uprN /etc/apt/sources.list{.org,}
--- /etc/apt/sources.list.org   2016-08-13 07:43:32.618218136 +0900
+++ /etc/apt/sources.list       2016-08-13 07:49:19.777011499 +0900
@@ -48,4 +48,6 @@ deb-src http://security.ubuntu.com/ubunt
 deb http://security.ubuntu.com/ubuntu xenial-security universe
 deb-src http://security.ubuntu.com/ubuntu xenial-security universe
 deb http://security.ubuntu.com/ubuntu xenial-security multiverse
+deb file:/home/hiroom2/repo xenial main
+# deb-src file:/home/hiroom2/repo xenial main
 deb-src http://security.ubuntu.com/ubuntu xenial-security multiverse

Add GPG key with apt-key.

$ sudo apt-key add /home/hiroom2/repo/public.key

Now you can install deb packages from repository in local machine.

$ sudo apt update -y
$ sudo apt install -y ubuntu-desktop

5 Export via HTTP

Export repository in local machine with apache2.

$ sudo apt install -y apache2
$ sudo cp -a repo /var/www/html/

Add repo to repository list with apt-add-repository on client.

$ sudo apt-add-repository "deb http://ubuntu-16.04-apt.hiroom2.com/repo xenial main"

Add GPG key with apt-key on client.

$ wget -O - http://ubuntu-16.04-apt.hiroom2.com/repo/public.key | \
sudo apt-key add -

Now client can install deb package from this repository.

$ sudo apt update -y
$ sudo apt install -y ubuntu-desktop