How to install a package from within my application? [duplicate]

Solution 1:

If you're using python, python-aptdaemon-gtk is probably the way to go. For a good example of how to do this, look at /usr/share/doc/python-aptdaemon.gtk3widgets/examples/gtk3-demo.py.gz

You get nice install prompts like:

install prompt

In the case of using C++, you could could use the AptDaemon D-Bus interface, but I don't know of any examples and you wouldn't get the widgets for free.

The D-Bus API of AptDaemon is documented here: http://packages.python.org/aptdaemon/dbus.html

I can't find much in the way of C++ DBus tutorials on the web, so your best bet is probably the documentation and examples in the libdbus-c++-doc Install libdbus-c++-doc package.

Solution 2:

You might take example on the totem media player when it prompts the user to install missing codecs, or when you open share options of a directory in nautilus via right click, it prompts to install samba.

I think they use the sessioninstaller package to do that. See man session-installer, and https://launchpad.net/sessioninstaller

You could also use the command xdg-open apt://package: it will open the default package manager with the possibility to install your package (ubuntu software center in recent releases).