How to fix E: Malformed line in preferences file /etc/apt/preferences in APT
This error occurs when APT encounters invalid syntax in the /etc/apt/preferences file. The preferences file must follow strict formatting rules with required Package, Pin, and Pin-Priority headers separated by blank lines.
Quick fix
Fixes APT_PREFS_MALFORMED
sudo nano /etc/apt/preferences- Check the /etc/apt/preferences file syntax
- Fix common syntax errors
- Validate the file format
- Test the preferences file
sudo nano /etc/apt/preferencesE: Malformed line in preferences file /etc/apt/preferences
What this error means
The /etc/apt/preferences file controls package pinning and version prioritization in Debian-based systems. APT parses this file to determine which package versions to prefer during installation and upgrades. When APT detects a line that doesn't conform to the required format—such as missing required headers, incorrect field names, or malformed entries—it reports this error and refuses to continue. Common issues include using 'Priority:' instead of 'Pin-Priority:', using '#' for comments instead of the 'Explanation:' field, leaving the file empty except for comments, or mixing in unrecognized configuration syntax.
Typical symptoms
- apt update, apt upgrade, or apt install commands fail with the error message
- System cannot perform package management operations
- Error message includes the line number or specific malformed content
- Previously working package management suddenly stops working after editing /etc/apt/preferences
- apt-cache policy command may also fail to parse preferences
Common causes
- Using 'Priority:' field instead of 'Pin-Priority:' in a preferences record
- Using '#' for comments instead of the 'Explanation:' field
- Missing required 'Package:' header in a preferences block
- Missing 'Pin:' or 'Pin-Priority:' fields in a preferences record
- Incorrect blank line separation between multiple preference records
- File contains only comments or is empty, which is not permitted
- Whitespace or indentation issues in field definitions
- Typos in field names (e.g., 'Packge:' instead of 'Package:')
How to fix "E: Malformed line in preferences file /etc/apt/preferences"
Open the file and review its contents carefully:
sudo nano /etc/apt/preferencesEnsure the file follows the required structure:
- Each record starts with 'Package: <package-name>' or 'Package: *'
- Followed by 'Pin: <pin-specification>'
- Followed by 'Pin-Priority: <priority-number>'
- Multiple records are separated by blank lines
- No other content should appear between these fields
Correct these frequent mistakes:
Incorrect Priority field:
# WRONG
Priority: 900
# CORRECT
Pin-Priority: 900Using # for comments:
# WRONG
# This is a comment
# CORRECT
Explanation: This is a commentMissing Package header:
# WRONG
Pin: release a=stable
Pin-Priority: 700
# CORRECT
Package: *
Pin: release a=stable
Pin-Priority: 700A valid preferences file should look like:
Package: nginx
Pin: release a=stable
Pin-Priority: 900
Package: *
Pin: release a=testing
Pin-Priority: 700Key points:
- Each record starts on a new line with 'Package:'
- All three fields (Package, Pin, Pin-Priority) are required
- Records are separated by exactly one blank line
- No spaces or special formatting between lines
After fixing syntax errors, test the file with:
sudo apt-cache policyIf the error persists, check for hidden characters or whitespace issues:
cat -A /etc/apt/preferencesThis shows all special characters, including tabs and carriage returns that might cause parsing issues.
If the file is not needed or corruption is severe, remove it entirely:
sudo rm /etc/apt/preferencesThen verify package management works:
sudo apt updateYou can also check /etc/apt/preferences.d/ directory for additional preference files:
ls -la /etc/apt/preferences.d/Fix or remove any corrupted files in that directory as well.
Advanced notes
The /etc/apt/preferences file uses a strict record-based format. Each record must be complete with all required fields. APT also supports a preferences.d directory (/etc/apt/preferences.d/) where you can place multiple preference files with .pref extension. These files are processed in alphanumeric order.
For pin specifications, use 'apt-cache policy <package>' to verify that your pinning rules are correctly applied. The output shows the available versions and their priorities.
When dealing with distribution-based pins, use 'apt-cache policy' output to identify the correct release attribute names (release codenames like 'focal', 'jammy', 'bookworm', or archive names like 'stable', 'testing').
Note that APT reads these files sequentially, so more specific package entries should come before general wildcard entries to ensure proper precedence.
Frequently asked questions
- What does "APT_PREFS_MALFORMED" mean?
- The /etc/apt/preferences file controls package pinning and version prioritization in Debian-based systems. APT parses this file to determine which package versions to prefer during installation and upgrades. When APT detects a line that doesn't conform to the required format—such as missing required headers, incorrect field names, or malformed entries—it reports this error and refuses to continue.
- How do I fix "APT_PREFS_MALFORMED"?
- This error occurs when APT encounters invalid syntax in the /etc/apt/preferences file. The preferences file must follow strict formatting rules with required Package, Pin, and Pin-Priority headers separated by blank lines.
- What causes "APT_PREFS_MALFORMED"?
- Using 'Priority:' field instead of 'Pin-Priority:' in a preferences record Using '#' for comments instead of the 'Explanation:' field Missing required 'Package:' header in a preferences block
- Is the "APT_PREFS_MALFORMED" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚APT Official Documentation
- 📖Debian APT Preferences Manual(Official Docs)
- 📖Debian Wiki - APT Configuration(Official Docs)