How can I fix apt error "W: Target Packages ... is configured multiple times"?
Recent, up-to-date solution
I wrote a Python script to automate this task. You can find the most recent version here.
Installation:
Install the prerequisites:
sudo apt install python3-apt
Download the PYZ bundle (
aptsources-cleanup.pyz
) from the latest release.Mark the PYZ bundle as executable:
chmod a+x aptsources-cleanup.pyz
Usage:
From the download location of the PYZ bundle (see step 2 above) run:
sudo ./aptsources-cleanup.pyz
Follow the instructions appearing on the screen.
If you wish to report an issue, leave a comment, or fork the source code, please do so on GitHub.
Historical, outdated solution
This is an older, shorter and simpler version more suitable to inline quotation:
#!/usr/bin/env python3
"""
Detects and interactively deactivates duplicate Apt source entries.
Usage: sudo python3 apt-remove-duplicate-source-entries.py
"""
from __future__ import print_function
import aptsources.sourceslist
EMPTY_COMPONENT_LIST = (None,)
def get_duplicates(sourceslist):
"""
Detects and returns duplicate Apt source entries.
"""
sentry_map = dict()
duplicates = list()
for se in sourceslist.list:
if not se.invalid and not se.disabled:
for c in (se.comps or EMPTY_COMPONENT_LIST):
key = (se.type, se.uri, se.dist, c)
previous_se = sentry_map.setdefault(key, se)
if previous_se is not se:
duplicates.append((se, previous_se))
break
return duplicates
if __name__ == '__main__':
try:
input = raw_input
except NameError:
pass
sourceslist = aptsources.sourceslist.SourcesList(False)
duplicates = get_duplicates(sourceslist)
if duplicates:
for dupe, orig in duplicates:
print(
'Overlapping source entries:\n'
' 1. {0}: {1}\n'
' 2. {2}: {3}\n'
'I disabled the latter entry.'.format(
orig.file, orig, dupe.file, dupe),
end='\n\n')
dupe.disabled = True
print('\n{0} source entries were disabled:'.format(len(duplicates)),
*[dupe for dupe, orig in duplicates], sep='\n ', end='\n\n')
if input('Do you want to save these changes? (y/N) ').upper() == 'Y':
sourceslist.save()
else:
print('No duplicated entries were found.')
It takes no parameter and works on all /etc/apt/sources.list*
with the help of the python3-apt
package. Lines don't have to be exact duplicates (even after normalization) but its enough if type, URI, suite and at least one component overlap.
I'm not sure the equality test is correct in all corner cases (e. g. various “option” strings).
For me (16.04 LTS) going to
System settings >> Software and Updates >> Other Software and removing each duplicate entry from the list worked fine.
Reset your repositories like this:
- IMPORTANT: First verify that you have
software-properties-gtk
installed (you can runsoftware-properties-gtk -h
to verify that you get a help-message) sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak
sudo software-properties-gtk
- Pick your options
- Save
Once everything is working, you can then delete your backup with sudo rm -rf /etc/apt/sources.list.bak
.