This error occurs when APT detects a syntax error in your sources.list file or files in sources.list.d/. Common causes include malformed URLs, incorrect line breaks, and mixed format styles. The fix involves identifying and correcting the problematic line.
The APT package manager reads repository configurations from /etc/apt/sources.list and files in /etc/apt/sources.list.d/ directory. When it encounters a line that doesn't follow the correct syntax, it reports this error with the line number and location. Each repository line must follow the Debian package repository format: `deb [options] uri distribution [component1] [component2]`. If a line has incorrect URL formatting, missing components, unescaped special characters, or spans multiple lines when it shouldn't, APT cannot parse it and raises this error.
The error message tells you which file and line number has the problem:
E: Malformed line 5 in source list /etc/apt/sources.listor
E: Malformed line 1 in source list /etc/apt/sources.list.d/docker.listNote the line number and file path from the error message.
Open the problematic sources file with a text editor:
sudo nano /etc/apt/sources.listOr if the error mentions a file in sources.list.d/:
sudo nano /etc/apt/sources.list.d/filename.listUse nano, vim, or your preferred text editor.
Look at the line number mentioned in the error. Check for these common issues:
Correct format:
deb http://archive.ubuntu.com/ubuntu focal main restricted universe multiverseCommon mistakes to fix:
1. Extra slashes or incorrect URLs:
# WRONG - extra slash
deb http://archive.ubuntu.com//ubuntu focal main
# CORRECT
deb http://archive.ubuntu.com/ubuntu focal main2. Missing components or spaces:
# WRONG - missing space between URL and distribution
deb http://archive.ubuntu.comainter focal main
# CORRECT
deb http://archive.ubuntu.com/ubuntu focal main3. Line wrapped incorrectly:
# WRONG - split across lines
deb http://archive.ubuntu.com/ubuntu
focal main restricted
# CORRECT - single line
deb http://archive.ubuntu.com/ubuntu focal main restricted4. Extra or stray characters:
# WRONG - extra text at end
deb http://archive.ubuntu.com/ubuntu focal main restricted] EXTRA
# CORRECT
deb http://archive.ubuntu.com/ubuntu focal main restrictedIf you can't determine what's wrong, you can safely comment out the problematic line by adding # at the beginning:
# deb http://archive.ubuntu.com/ubuntu focal mainSave your changes:
- In nano: Press Ctrl+X, then Y to confirm, then Enter to save
- In vim: Press Esc, type :wq, and press Enter
Then test that APT can read the file correctly:
sudo apt updateIf the error is gone and apt update completes successfully, the problem is fixed.
If the error is in the main sources.list file but you've fixed all lines, check for problematic files in sources.list.d/:
ls -la /etc/apt/sources.list.d/Look for files with incorrect extensions (like .backup, .bak, .old). APT only recognizes:
- .list files (old format)
- .sources files (new DEB822 format)
Delete or rename any backup files:
sudo rm /etc/apt/sources.list.d/old-repo.backupThen try sudo apt update again.
About the two APT source formats:
APT supports two different source list formats:
1. One-line format (.list files): The traditional format using a single line:
deb [options] uri distribution [components]2. DEB822 format (.sources files): The newer RFC 822 style format introduced in Debian 12 and Ubuntu 22.04+:
Types: deb
URIs: http://archive.ubuntu.com/ubuntu
Suites: focal
Components: main restricted universe multiverseDon't mix these formats in the same file. If you're migrating, create separate .sources files for new repositories.
Checking file syntax with apt-listchanges:
You can validate your sources without running a full update using:
sudo apt-get checkThis performs sanity checks on the repository configuration without downloading package lists.
dpkg: serious warning: files list file for package 'package-name' contains empty filename
How to fix "files list file contains empty filename" in APT
E: Sub-process /usr/bin/dpkg returned an error code (2)
How to fix "Sub-process /usr/bin/dpkg returned an error code (2)" in APT
dpkg-divert: error: rename involves overwriting 'path' with different file
How to fix dpkg-divert rename conflicts in APT
E: Sub-process /usr/bin/dpkg returned an error code (1) during kernel installation
How to fix "dpkg returned an error code (1)" in APT kernel installation
dpkg: dependency problems prevent configuration of triggers
dpkg: dependency problems prevent configuration of triggers in apt