You can use
sudo apt-rdepends apache2
This will show the packages and the version needed.
Question
When installing softwares like nginx on an Ubuntu 12.04 server, apache2
appears to be installed as one of the dependency which is not what we need.
Question: How do you find out which packages installed has apache2
as its dependency?
Solution
You can use
sudo apt-rdepends apache2
This will show the packages and the version needed.
OTHER TIPS
You can use apt-rdepends to traverse dependencies forwards or backwards
http://pkgs.org/ubuntu-12.04/ubuntu-universe-i386/apt-rdepends_1.3.0-2_all.deb.html
alternatively, there is
apt-cache rdepends packagename
from here https://askubuntu.com/questions/128524/how-to-list-package-dependees-reverse-dependencies
RPM-based distributions allow this by a simple call of the basic rpm utility:
rpm -q --whatrequires apache2
I am certain the apt package manager used in Ubuntu offers something similar. I suggest you read its man page...
/var/lib/dpkg/status
this has the deb dependency list. each package in this file has a dependencies section if they have deps. and you can just pull a list of those and look for the apache2 depends.
here is some terrible code for pulling that status file into a searchable sqlitedb in python.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import os
import re
import sqlite3
import sys
import time
import traceback
from dpkg import parse
from dpkg import poll
def db_init(status_file):
# poll status file
status_obj = poll.poll(status_file)
# initialize sqlite3
conn = sqlite3.connect(":memory:")
db_create(conn)
# parse status fileobject, load into database
db_status_load(conn, status_obj)
# kill that big string obj
del status_obj
# return ready database handle
return conn
def db_query(conn, query):
db_resp = conn.execute(query).fetchall()
return db_resp
def db_create(conn):
c = conn.cursor()
c.execute('PRAGMA temp_store=MEMORY;')
c.execute('PRAGMA journal_mode=MEMORY;')
# two tables packages, and a dependency index
c.execute('create table if not exists packages (packageid integer primary key autoincrement, packagename text, description blob);')
c.execute('create table if not exists depends (packageid int, dependencyid int, alternate int);')
c.execute('create table if not exists alternates (packageid int, dependencyid int, alternateid int);')
conn.commit()
def db_listPackages(conn):
c = conn.cursor()
c.execute('select * from packages')
rows = c.fetchall()
return rows
def db_descPackage(conn, package_name):
c = conn.cursor()
c.execute('select * from packages where packagename=?', (package_name,))
rows = c.fetchall()
return rows
def db_namePackage(conn, package_id):
c = conn.cursor()
c.execute('select * from packages where packageid=?', (package_id,))
rows = c.fetchall()
if len(rows) < 1:
# No package name for that ID
package_name = "NULL"
else:
package_name = str(rows[0][1])
return package_name
def db_idPackage(conn, package_name):
c = conn.cursor()
c.execute('select * from packages where packagename=?', (str(package_name),))
rows = c.fetchall()
if len(rows) < 1:
# No corresponding package in our status db by that name
package_id = 0
else:
package_id = int(rows[0][0])
return package_id
def db_depsPackage(conn, package_id):
c = conn.cursor()
c.execute('select * from depends where packageid=?', (package_id,))
rows = c.fetchall()
return rows
def db_indepsPackage(conn, package_id):
c = conn.cursor()
c.execute('select * from depends where dependencyid=?', (package_id,))
rows = c.fetchall()
return rows
def db_getAlternates(conn, package_id, dependency_id):
c = conn.cursor()
c.execute('select * from alternates where dependencyid=? AND packageid=?', (dependency_id, package_id))
rows = c.fetchall()
return rows
def db_status_load(conn, status_obj):
c = conn.cursor()
# render json datastruct from string object
status_dict = parse.parse(status_obj)
package_name = package_description = ""
package_depends = []
# stage packages info from status_dict
# populate packages table
for packages in status_dict['Packages']:
# define package name
package_name = packages
# print "inserting \033[34m %s \033[0m :\n" % package_name
for idx,stuff in enumerate(status_dict['Packages'][packages]):
if stuff.has_key("Description"):
# define package description
package_description = stuff['Description']
# execute insertion query
b = sqlite3.Binary(package_description)
c.execute('insert into packages (packagename, description) values (?,?)', (package_name, b) )
conn.commit()
# populate dependency table
for packages in status_dict['Packages']:
package_name = packages
# print "inserting depends and alternates for \033[31m %s \033[0m :\n" % package_name
for stuff in status_dict['Packages'][packages]:
if stuff.has_key("Depends"):
dep_string = stuff['Depends']
dep_string = dep_string.replace(" ", "")
dep_array = dep_string.split(',')
package_id = db_idPackage(conn, package_name)
for dep in dep_array:
dep = re.sub(r'\([^)]*\)', '', dep)
if "|" in dep:
alternates = dep.split('|')
deporig = alternates[0]
for idx, alternate in enumerate(alternates):
if idx > 0:
alternate_id = db_idPackage(conn, alternate)
dependency_id = db_idPackage(conn, deporig)
# print "\033[33m ALTERNATE: %s, %s, %s \033[0m \n" % (package_id, dependency_id, alternate_id)
c.execute('insert into alternates (packageid, dependencyid, alternateid) values (?,?,?)',
(package_id, dependency_id, alternate_id) )
else:
dependency_id = db_idPackage(conn, deporig)
c.execute('insert into depends (packageid, dependencyid, alternate) values (?, ?, ?)',
(package_id, dependency_id, 1) )
else:
dependency_id = db_idPackage(conn, dep)
c.execute('insert into depends (packageid, dependencyid, alternate) values (?, ?, ?)',
(package_id, dependency_id, 0) )
# commit changes to database
conn.commit()
return 0