cache = apt.cache.Cache()
cache.update()
pkg = cache[pkg_name]
if pkg.is_installed:
    print('{pkg_name} already installed'.format(pkg_name=pkg_name))
else:
    pkg.mark_install()
try:
    cache.commit()
except Exception as arg:
    print('Sorry, package installation failed [{err}]'.format(
        err=str(arg)), file=sys.stderr)
Как установить пакет с помощью API python-apt