How do I use PackageKit?

Back to the main page

How do I use PackageKit?

If you are writing an application that wants to install packages on demand, and don't care about the low level details, there's a session helper API that you should use. It's much simpler than using PackageKit directly.

Using the command line

The pkcon text-mode program allows you to interact with PackageKit on the command line. For example:

[hughsie@laptop ~]$ pkcon get-updates
[hughsie@hughsie-work PackageKit]$ pkcon get-updates
security    	bluez-utils-3.35-3.fc9                  	Bluetooth utilities
bugfix      	xterm-236-1.fc9                         	Terminal emulator for the X Window System
bugfix      	kernel-devel-2.6.25.14-108.fc9          	Development package for building kernel modules to match the kernel
enhancement 	kde-filesystem-4-17.fc9                 	KDE filesystem layout
enhancement 	subversion-1.5.1-1.fc9                  	Modern Version Control System designed to replace CVS

or

[hughsie@hughsie-work PackageKit]$ pkcon --filter=~devel search name power
installed   	DeviceKit-power-001-0.8.20080811git.fc9 	Power Management Service
installed   	gnome-power-manager-2.23.4-1.118.20080801svn.fc9.hughsie	GNOME Power Manager
installed   	powerman-2.1-1.fc9                      	PowerMan - Power to the Cluster
installed   	powertop-1.9-3.fc9                      	Power consumption monitor
available   	gnome-power-manager-2.22.1-1.fc9        	GNOME Power Manager
available   	kadu-powerkadu-0.6.0-3.fc9              	PowerKadu
available   	kadu-powerkadu-0.6.0.1-1.fc9            	PowerKadu
available   	kpowersave-0.7.3-3.fc9                  	KPowersave is the KDE frontend for powermanagement
available   	powerman-1.0.32-5.fc9                   	PowerMan - Power to the Cluster
available   	powermanga-0.90-3                       	Arcade 2D shoot-them-up game

The pkmon program allows you to monitor what PackageKit is doing on the command line and is mainly used for debugging.

The pkgenpack program allows you to generate Service Packs with a package and its dependencies.

Using graphical tools:

gnome-software and gnome-packagekit provide a rich set of GTK tools for automatically updating your computer and installing software.

Apper is the name of the KDE graphical tools designed for PackageKit.

Using libpackagekit:

The libpackagekit gobject library wraps the DBus interface in a nice glib-style API. This makes designing programs that use libpackagekit can concentrate on core functionality rather that the DBus and PackageKit internals. PkTask in libpackagekit can be used as easily as:

GError *error = NULL;
PkError *error_code = NULL;
PkResults *results = NULL;
GPtrArray *array = NULL;
PkPackage *item;
gchar **values = NULL;
gchar **package_ids = NULL;
uint i;
PkTask *task;

task = pk_task_new ();

/* resolve the package name */
values = g_new0 (gchar*, 1 + 1);
values[0] = g_strdup ("openoffice-clipart");
values[1] = NULL;
results = pk_task_resolve_sync (task, PK_FILTER_ENUM_NOT_INSTALLED, values, NULL, NULL, NULL, &error);

/* check error code */
error_code = pk_results_get_error_code (results);
if (error_code != NULL) {
	g_printerr ("%s: %s, %s\n", "Resolving of packages failed",
		    pk_error_enum_to_string (pk_error_get_code (error_code)),
		    pk_error_get_details (error_code));
	goto out;
}

/* get the packages returned */
array = pk_results_get_package_array (results);
package_ids = g_new0 (gchar *, array->len+1);
for (i = 0; i < array->len; i++) {
	item = g_ptr_array_index (array, i);
	package_ids[i] = g_strdup (pk_package_get_id (item));
}

/* install the packages */
results = pk_task_install_packages_sync (task, package_ids , NULL, NULL, NULL, &error);

/* check error code */
error_code = pk_results_get_error_code (results);
if (error_code != NULL) {
	g_printerr ("%s: %s, %s\n", _("Error installing package(s)!"),
		pk_error_enum_to_string (pk_error_get_code (error_code)),
		pk_error_get_details (error_code));
	goto out;
}

out:
	g_strfreev (values);
	g_object_unref (task);
	if (error_code != NULL)
		g_object_unref (error_code);
	if (array != NULL)
		g_ptr_array_unref (array);
	if (package_ids != NULL)
		g_strfreev (package_ids);
	if (results != NULL)
		g_object_unref (results);

Using the raw DBus API:

Using the PackageKit DBus methods and signals directly means that no glib or gobject dependency is needed, although this means you will have to manage the transaction_id multiplexing in any client program. This is not difficult, although does require more code than just using libpackagekit.

Back to the main page