Ubuntu: Using add-apt-repository with a proxy

In a production environment where a proxy such as squid is necessary to reach the public internet, you may need use apt-add-repository to install a package from a ppa.

Using the Ansible ppa as an example, the command from the documentation is:

sudo apt-add-repository --yes --update ppa:ansible/ansible

You might be tempted to believe that if you have configured “apt-get” with a proxy, then apt-add-repository would look at those settings – it will not!

Instead, you need to set the standard http_proxy/https_proxy variables, and use “sudo -E” to preserve the environment if using sudo.

export http_proxy=http://mysquid:3128
export https_proxy=http://mysquid:3128
# preserve environment sent to sudo
sudo -E apt-add-repository --yes --update ppa:ansible/ansible

The same type of advice goes toward apt-key, it does not automatically use the proxy configured for apt-get, you need to use the ‘http-proxy’ parameter parameter similar to below.

sudo apt-key adv --keyserver-options http-proxy=http://mysquid:3128/ --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 6125e2a8c77f2818fb7bd15b93c4a3fd7bb9c367

The key id used is the one published on the ansible ppa page.

 

REFERENCES

stackoverflow, using add-apt-repository and apt-key with proxy

ppa launchpad for ansible, shows key

blog.zackad.dev, determining key from ppa page

butwt.wordpress.com, ansible and apt with proxy