LWM-Linux/08 - Advanced Linux Administration/Advanced Package Management (APT).md

148 lines
3.3 KiB
Markdown
Raw Normal View History

# Advanced Package Management with apt
## 1. Understanding apt
APT (Advanced Package Tool) is a powerful package management system used in Debian, Ubuntu, and other Debian-based Linux distributions. It simplifies the process of installing, upgrading, configuring, and removing software packages.
## 2. Key components
- apt: The command-line tool for package management
- apt-get: The older command-line tool (still widely used)
- apt-cache: Used for querying package information
- /etc/apt/sources.list: The main configuration file for package repositories
## 3. Basic apt commands
- Update package lists: `sudo apt update`
- Upgrade installed packages: `sudo apt upgrade`
- Install a package: `sudo apt install package_name`
- Remove a package: `sudo apt remove package_name`
- Search for a package: `apt search keyword`
- Show package information: `apt show package_name`
## 4. Advanced apt commands
### a. Install specific version:
```
sudo apt install package_name=version_number
```
### b. Downgrade a package:
```
sudo apt install package_name=older_version_number
```
### c. Hold a package at its current version:
```
sudo apt-mark hold package_name
```
### d. Remove a package and its configuration files:
```
sudo apt purge package_name
```
### e. Remove unused dependencies:
```
sudo apt autoremove
```
### f. Clean up the local repository:
```
sudo apt clean
```
### g. Download a package without installing:
```
sudo apt download package_name
```
## 5. Working with repositories
### a. Add a repository:
```
sudo add-apt-repository ppa:user/ppa-name
```
### b. Remove a repository:
```
sudo add-apt-repository --remove ppa:user/ppa-name
```
### c. Update package lists after adding/removing repositories:
```
sudo apt update
```
## 6. Managing package priorities
APT uses priorities to determine which version of a package to install when multiple versions are available. You can modify priorities using the `/etc/apt/preferences` file.
Example:
```
Package: firefox
Pin: release o=Ubuntu
Pin-Priority: 1001
```
This gives Firefox from the Ubuntu repositories a higher priority than other sources.
## 7. Apt configuration
The main configuration file is `/etc/apt/apt.conf`. You can create custom configuration files in `/etc/apt/apt.conf.d/`.
Example configuration options:
```
APT::Get::Show-Versions "true";
APT::Get::Show-Upgraded "true";
```
## 8. Troubleshooting
### a. Fix broken dependencies:
```
sudo apt --fix-broken install
```
### b. Reconfigure a package:
```
sudo dpkg-reconfigure package_name
```
### c. Verify package integrity:
```
sudo apt-get check
```
## 9. Advanced features
### a. Simulate installations:
```
sudo apt install -s package_name
```
### b. Download source code:
```
sudo apt source package_name
```
### c. Build a package from source:
```
sudo apt build-dep package_name
sudo apt source --compile package_name
```
### d. Create a package download script:
```
sudo apt-get --print-uris --yes install package_name > download_script.sh
```
## 10. Best practices
- Regularly update and upgrade your system
- Use `apt` instead of `apt-get` for interactive use
- Be cautious when adding third-party repositories
- Always verify package names and versions before installation
- Use `apt-mark` to manage package states (hold, unhold, etc.)
- Regularly clean up unused packages and local repository cache