Common Issues and Solutions when Using `apt install` on Ubuntu

Common Issues and Solutions When Installing Software with apt install on Ubuntu

When using Ubuntu, apt install is one of the most common commands for installing software. However, beginners may encounter various issues such as “package not found,” “permission denied,” or “dependency errors.” This article will discuss the causes and solutions to these common problems, helping you quickly resolve installation difficulties.

Issue 1: “Could not locate package” (E: Could not find package xxx)

Phenomenon: When executing sudo apt install 软件名, the terminal displays E: Could not find package xxx or “package not found.”
Causes:
1. The package name is misspelled (e.g., writing vlc instead of vcl).
2. The local software source information is not updated, so APT is unaware of the package.
3. Incorrect software source configuration (e.g., adding incompatible third-party sources or failing to enable official sources).

Solutions:
- Check the package name: Verify the correct package name using apt search 关键词 (e.g., apt search vlc).
- Update software sources: Run sudo apt update to refresh the local software source list, then reinstall.
- Fix software sources: If using third-party sources (e.g., Wine, Docker), check source files in /etc/apt/sources.list or /etc/apt/sources.list.d/. Replace with a domestic mirror source if errors occur (refer to Issue 7).

Issue 2: “Unable to acquire lock” (Resource temporarily unavailable)

Phenomenon: Installation fails with errors like Unable to acquire lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable).
Causes:
- Another apt-related process (e.g., apt install or apt update in another terminal) did not exit properly, locking the file.

Solutions:
1. Terminate occupying processes: Run ps aux | grep apt to find all apt processes (e.g., apt, apt-get), note their PID (process ID), and kill them with sudo kill PID (e.g., sudo kill 1234).
2. Delete lock files: If processes cannot be found, directly delete the lock files:

   sudo rm /var/lib/dpkg/lock
   sudo rm /var/lib/apt/lists/lock
   sudo rm /var/cache/apt/archives/lock
  1. Re-run the installation command: sudo apt install 软件名.

Issue 3: “Temporary failure in name resolution”

Phenomenon: sudo apt update or installation fails with “Temporary failure in name resolution” due to connection issues.
Causes:
- The network is not connected, or DNS server configuration is incorrect (Ubuntu uses system-assigned DNS; instability or DNS failures cause resolution issues).

Solutions:
1. Check network connectivity: Verify with ping www.baidu.com. If unreachable, check the router or restart the network.
2. Replace DNS servers: Edit /etc/resolv.conf (requires root privileges) and add public DNS:

   sudo nano /etc/resolv.conf

Add:

   nameserver 8.8.8.8  # Google DNS
   nameserver 8.8.4.4

Save and re-run sudo apt update.
3. Temporarily switch to HTTP source: Replace https:// with http:// in the source list (e.g., http://mirrors.aliyun.com/ubuntu/).

Issue 4: “Dependency problem” (xxx is not installed)

Phenomenon: Installation fails with dependency errors like “dependency is not installed” (e.g., “requires libxxx but it is not installed”).
Causes:
- The target software depends on other packages (e.g., teamviewer requires libssl), which are either missing or incompatible.

Solutions:
1. Fix dependencies: Run sudo apt install -f (the -f flag fixes broken dependencies automatically).
2. Force install dependencies: If the missing package is known, install it directly (e.g., sudo apt install libssl1.1), then reinstall the target software.
3. Reinstall the software: If dependencies are corrupted, try sudo apt install --reinstall 软件名.

Issue 5: “Permission denied” (No sudo)

Phenomenon: Running apt install 软件名 (without sudo) results in E: Unable to acquire lock /var/lib/dpkg/lock - open (13: Permission denied).
Causes:
- Ubuntu requires root privileges for software installation; ordinary users lack permissions without sudo.

Solution:
Add sudo to the command:

sudo apt install 软件名

Enter your user password (input will not be displayed; press Enter after typing).

Issue 6: Software not found or cannot start after installation

Phenomenon: The installed software (e.g., vlc) is missing from the application menu or fails to launch when clicked.
Causes:
- Incomplete installation (e.g., interrupted download) or failure to register with the desktop environment.

Solutions:
1. Verify installation: Run sudo dpkg -l | grep 软件名 (e.g., sudo dpkg -l | grep vlc). An ii status indicates successful installation.
2. Reinstall: If incomplete, use sudo apt install --reinstall 软件名.
3. Launch manually: Try running the software directly in the terminal (e.g., vlc). If it starts, restart the system or reinstall the desktop shortcut if the icon issue persists.

Issue 7: Slow installation or hanging

Phenomenon: apt update or installation freezes at “Fetching xxx package” (especially with official sources).
Causes:
- Official sources are hosted overseas, leading to slow downloads in China; incorrect source configuration (e.g., enabling deb-src sources).

Solutions:
1. Switch to a domestic mirror source:
- Backup the original sources: sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
- Edit the source file: sudo nano /etc/apt/sources.list
- Replace with a domestic mirror (e.g., Tsinghua Mirror):

     deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
     deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
     deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
     deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
 (Replace `jammy` with your Ubuntu version; check with `lsb_release -a`.)
  • Run sudo apt update to refresh sources, then install.

Summary

When encountering apt install issues, first analyze the error message (e.g., “package not found” or “dependency error”) and apply the corresponding solution:
- Misspelled name → Check the package name.
- Lock file issues → End processes or delete lock files.
- Network issues → Replace DNS or switch to a domestic source.
- Dependency errors → Use apt install -f to fix.

If problems persist, search for the official package name with apt search 关键词 or refer to the Ubuntu Chinese Community for further assistance. Mastering these skills will help you resolve most installation issues efficiently!

Xiaoye