# Linux Installation and Package Management

> Chapter 2 of the Courseiva LPI-LPIC1 curriculum — https://courseiva.com/learn/lpi-lpic1/linux-installation-and-package-management

**Official objective:** 102.1 — Design hard disk layout, install a Linux system, and manage packages using Debian and RPM package managers.

## Introduction

What’s the difference between a well-organised kitchen and a chaotic junk drawer? It’s all about how you arrange and maintain your tools. For the LPIC-1 exam, the topic of Linux installation and package management is the difference between a server that runs smoothly for years and one that crashes the day after you set it up. This matters because your job as a Linux professional will involve installing operating systems and managing software — if you mess this up, nothing else works.

## The Flatpack Furniture Analogy

Ever tried to assemble a flatpack bookshelf from a certain Swedish furniture giant? You open the box and find a messy pile of screws, wooden panels, dowels, and an Allen key. Your job is to turn this chaos into a stable, usable bookshelf. First, you must decide where in the room it goes — do you want it next to the desk or against the far wall? That’s your hard drive layout, deciding which partitions (sections of the hard drive) go where. Then, you follow the instruction manual to screw the sides to the shelves. Each screw and panel is like a software package — a program or library your system needs. But what if you lose a screw or snap a dowel? You need a reliable system to get a replacement. That’s where the package manager comes in: it’s your toolkit for finding the right ‘screw’ (software), checking it’s the correct size (dependency), and installing it without breaking the rest of the furniture (the system). Without a package manager, you’d be searching for the right screw in a giant hardware shop with no help — chaotic and prone to mistakes. This analogy maps directly to Linux: the ‘box’ is your installation media, the ‘room layout’ is your partition scheme, and the ‘toolkit’ is your package manager (like APT for Debian or YUM/DNF for Red Hat). Just as you’d never build furniture blind, you should never install or manage Linux software without understanding its package management system.

## Core explanation

Let’s start at the very beginning. When you install Linux, you are essentially copying a set of files from an installation medium (like a USB stick or a DVD) onto your computer’s hard drive. But you cannot just dump everything in one big pile. Hard drives are divided into sections called partitions. Think of a partition like a room in a house: the ‘root’ partition (‘/’) is the main living room where the operating system lives, ‘/home’ is a bedroom for user files, and ‘swap’ is like a utility closet used as overflow memory. Partitioning is crucial because if one partition fills up (say, with personal documents), it won’t crash the entire system — it just affects that room.

Now, once Linux is installed, you will need to install more software. On Linux, software is distributed in packages. A package is a compressed archive that contains the program files, configuration files, and a manifest (a list of what’s inside). There are two major package formats you must know for LPIC-1: .deb (used by Debian and Ubuntu) and .rpm (used by Red Hat, Fedora, and CentOS). Each format needs its own package manager to handle installation, updates, and removal.

Package managers solve a huge headache: dependency resolution. A dependency is a piece of software that another program needs to run. For example, a web browser might depend on a library called libfoo. When you install the browser, the package manager automatically finds and installs libfoo too. Without a package manager, you would have to track down libfoo yourself, install it, and hope it doesn’t depend on something else — this was famously called ‘dependency hell’.

For Debian-based systems, the primary package manager is apt (Advanced Package Tool). You use commands like:
- `apt update` to refresh the list of available packages from online repositories (software sources).
- `apt install <package-name>` to install a package.
- `apt remove <package-name>` to uninstall.
- `dpkg -i <file.deb>` to install a .deb file manually.

For Red Hat-based systems, you use dnf (or yum on older versions). Commands include:
- `dnf check-update` to see what can be updated.
- `dnf install <package-name>` to install.
- `dnf remove <package-name>` to remove.
- `rpm -ivh <file.rpm>` to install an .rpm file manually.

Both package managers rely on repositories — centralised servers that hold thousands of packages. When you run `apt update`, your system downloads a list of what’s available in those repositories. This replaces the old, painful method of downloading software from websites manually and hoping it works.

Why does this replace older methods? Before package managers, installing software on Linux meant compiling from source code. You would download the source code of a program, run a compiler (a program that turns human-readable code into machine instructions), and then handle dependencies yourself. It was slow, error-prone, and required deep knowledge. Package managers automate this process, making installation as simple as a single command.

In summary: installation is about setting up the foundation (partitioning) and then adding software in a structured, safe way using package managers. The exam tests your ability to understand these processes, the commands, and the differences between Debian and Red Hat package management.

## Real-world context

Imagine you are a junior systems administrator at a small e-commerce company. The company runs its website on a Debian-based Linux server. One day, the development team says they need a new version of PHP (a programming language) to support a new feature. Here’s what you actually do:

First, you log into the server via SSH (a secure remote connection). You check what version of PHP is currently installed using `dpkg -l | grep php`. You see it’s version 7.4, but you need version 8.0. Before installation, you must make sure the correct repository is available. Debian’s default repositories might not have PHP 8.0, so you add a third-party repository (a specific server maintained by the PHP team). You do this by editing the file `/etc/apt/sources.list` or adding a new file in `/etc/apt/sources.list.d/`. Then you run `apt update` to fetch the latest package lists.

Now you install PHP 8.0 using `apt install php8.0`. The package manager automatically picks up dependencies — it installs new libraries and modules PHP needs. But here’s a real-world trap: the old PHP 7.4 might still be present, causing conflicts. You might need to purge the old version with `apt purge php7.4`. You then restart the web server (like Apache or Nginx) for the changes to take effect.

Next, imagine you need to install a proprietary tool that only comes as an .rpm file, but your server is Debian-based. You cannot use apt for that. You must use a tool called alien to convert the .rpm to a .deb, or you install the .rpm manually with `rpm -ivh` (though this can cause dependency issues). In practice, you search for a .deb version first.

Finally, you test the new PHP by running a simple script to ensure it works. You document the changes in a change log for your team. This whole process — from identifying the need to testing — takes about 15 minutes, but if you hadn’t understood package management, you could have broken the server for hours.

Key tools used in this scenario:
- apt and dpkg for Debian systems.
- yum and rpm for Red Hat systems.
- repositories, both official and third-party.
- commands like `apt list --upgradable` to check for updates.

The lesson: real-world package management is about solving problems quickly without breaking production systems. You repair broken packages with `dpkg --configure -a`, you pin package versions to prevent unwanted upgrades, and you use virtual packages when dependencies are missing.

## Exam focus

The LPIC-1 exam loves to test your knowledge of specific commands and their options. For objective 102.1, you will face questions that ask you to identify the correct command to install a package, or to recover from a broken installation. The exam is not about theory — it’s about knowing which flag does what.

Here are the exact concepts they test:
- Partitioning schemes: They will ask about the standard directories like /boot, /, /home, /var, and swap. They expect you to know that /boot is typically small (around 500 MB) and contains the kernel and bootloader files. They also test the concept of LVM (Logical Volume Manager) though it’s a separate objective.
- Package formats: You must know that .deb is for Debian and .rpm is for Red Hat. They will ask which command installs a local .deb file (`dpkg -i`), and which installs a local .rpm file (`rpm -ivh`).
- Frontend vs backend tools: For Debian, `apt` is the frontend (handles dependencies and fetches from repositories), while `dpkg` is the backend (directly manipulates packages). For Red Hat, `dnf` or `yum` is the frontend, `rpm` is the backend. The exam tests that you use the frontend for dependency resolution and the backend for direct manipulation.
- Key options: Memorise these:
  - `dpkg -l` (list installed packages)
  - `dpkg -s <package>` (status of a package)
  - `dpkg -L <package>` (list files installed by a package)
  - `rpm -qa` (query all installed packages)
  - `rpm -qi <package>` (info about a package)
  - `rpm -ql <package>` (list files)
- Traps: The exam often presents a scenario where a package installation fails due to a missing dependency. The correct answer is to use the frontend tool (apt or dnf) with the ‘install’ command, not the backend. Another trap: they might ask which command installs a package from a remote repository — the correct answer is `apt install` or `dnf install`, not `dpkg -i` (which only works with a local file).
- Verification: They test `rpm -V` (verify a package) and `dpkg --audit` (check for broken packages).

Finally, they love to test the order of operations: when adding a new repository, you must run `apt update` before `apt install`. They will present a scenario where the user forgot to update and ask why the package isn’t found. The answer: the package list is outdated.

To pass this section, drill the commands. Use flashcards for each tool: apt, dpkg, dnf, rpm. Know when to use the frontend vs backend. And remember: the exam is multiple choice, so look for the most specific command that fits the scenario.

## Step by step

1. **Partition the hard drive** — During installation, you decide where to put the operating system. You create partitions like / (root), /boot (for boot files), and swap (virtual memory). This step is like deciding which rooms go where in a house. It matters because poor partitioning can lead to a full root partition, crashing the system.
2. **Install the base system** — The installer copies the core Linux files (kernel, shell, utilities) to the root partition. This step replaces the older method of manually copying files from a floppy disk. It creates the essential structure needed to boot and run commands.
3. **Configure package repositories** — After installation, you must point your system to online software repositories (servers that host packages). You do this by editing /etc/apt/sources.list (Debian) or /etc/yum.repos.d/ (Red Hat). Without this step, your package manager has no source for new software.
4. **Install your first package** — You run 'apt install <package>' or 'dnf install <package>'. The package manager downloads the package and all its dependencies from the repository and installs them. This automates the once-manual task of compiling source code and tracking dependencies.
5. **Manage package updates** — Regularly run 'apt upgrade' or 'dnf upgrade' to apply security patches and bug fixes. This step is critical in production environments to protect against vulnerabilities. It replaces the old approach of downloading new versions from websites individually.
6. **Remove or purge unwanted packages** — Use 'apt remove' to delete program files but keep configs, or 'apt purge' to remove everything. This keeps your system clean and prevents unused packages from occupying disk space or causing conflicts.

## Comparisons

### apt (Debian) vs dnf (Red Hat)

**apt (Debian):**
- Frontend package manager for Debian-based systems
- Uses .deb package format
- Commands: apt update, apt install, apt upgrade

**dnf (Red Hat):**
- Frontend package manager for Red Hat-based systems
- Uses .rpm package format
- Commands: dnf check-update, dnf install, dnf upgrade

### dpkg (backend) vs rpm (backend)

**dpkg (backend):**
- Directly installs .deb files
- Does not resolve dependencies
- Use for local package installation

**rpm (backend):**
- Directly installs .rpm files
- Does not resolve dependencies
- Use for local package installation

### Partition / vs Partition /home

**Partition /:**
- Root partition: holds system files and programs
- Must have enough space for OS and core utilities
- If full, system can become unstable

**Partition /home:**
- Home partition: holds user files and settings
- Separating /home protects user data during reinstall
- If full, only user operations are affected

### apt remove vs apt purge

**apt remove:**
- Removes the executable files of a package
- Leaves configuration files in /etc
- Useful if you might reinstall later and want settings

**apt purge:**
- Removes executable files and configuration files
- Completely cleans the package
- Useful to get rid of software permanently

### .deb package vs .rpm package

**.deb package:**
- Package format for Debian, Ubuntu, and derivatives
- Managed by dpkg and apt
- Example file: myapp_1.2-3_amd64.deb

**.rpm package:**
- Package format for Red Hat, Fedora, CentOS
- Managed by rpm and dnf
- Example file: myapp-1.2-3.el9.x86_64.rpm

## Diagram

_Flowchart showing the two main branches of Linux installation: hard drive partitioning and package management, with Debian and Red Hat paths._

```mermaid
flowchart TD
    A[Linux Installation] --> B[Partition Hard Drive]
    B --> C[/ (root)]
    B --> D[/home]
    B --> E[swap]
    A --> F[Package Management]
    F --> G[Debian (apt/dpkg)]
    F --> H[Red Hat (dnf/rpm)]
    G --> I[.deb packages]
    H --> J[.rpm packages]
    I --> K[Install from repo]
    J --> L[Install from repo]
    I --> M[Install local file via dpkg]
    J --> N[Install local file via rpm]
```

## Common misconceptions

- **Misconception:** When I install a package with 'apt install', it only installs that one package. **Reality:** Apt automatically installs all required dependencies as well, often installing multiple packages in a chain. (Beginners think the package manager works like a simple copy operation, not understanding the dependency graph.)
- **Misconception:** All Linux distributions use the same package manager. **Reality:** Different distributions use different package managers: Debian-based use apt/dpkg (for .deb), Red Hat-based use dnf/rpm (for .rpm). (New users often start with one distribution (like Ubuntu) and assume everything works the same way on other distros.)
- **Misconception:** RPM is a safer or better format than DEB, or vice versa, in terms of quality. **Reality:** Both package formats are equally capable. The choice depends on the distribution, not technical superiority. (Users often engage in debates online, but the exam treats them as equivalent tools for different ecosystems.)
- **Misconception:** Removing a package with 'apt remove' also removes its configuration files. **Reality:** 'apt remove' deletes the program files but leaves configuration files in /etc. To purge them, use 'apt purge'. (The names 'remove' and 'purge' are not intuitive; beginners expect remove to be thorough.)
- **Misconception:** You need an internet connection to install any package on Linux. **Reality:** You can install packages from local .deb or .rpm files using dpkg or rpm, even without internet access. (Modern distributions default to online repositories, so beginners forget that offline installation is possible.)

## Key takeaways

- Linux hard drives are divided into partitions like /, /home, /boot, and swap to isolate system files from user data.
- Package managers like apt and dnf automate software installation by resolving dependencies and fetching from repositories.
- Debian-based systems use .deb packages managed by dpkg (backend) and apt (frontend).
- Red Hat-based systems use .rpm packages managed by rpm (backend) and dnf or yum (frontend).
- Always run 'apt update' or 'dnf check-update' before installing a package to ensure your package list is current.
- To remove a package and its configuration files, use 'apt purge' (not just 'apt remove').

## FAQ

**What is the difference between 'apt install' and 'dpkg -i'?**

'apt install' automatically resolves dependencies and fetches the package from a repository. 'dpkg -i' installs a local .deb file directly but will fail if dependencies are missing.

**Why do I need to run 'apt update' before installing a package?**

'apt update' refreshes your local copy of the package list from repositories. Without it, your system might not know the latest version or even that the package exists.

**Can I use a .rpm package on a Debian-based system?**

Not directly. You can use the 'alien' tool to convert an .rpm to a .deb, but it's not guaranteed to work perfectly. It's better to find a .deb version for the package.

**What does 'swap' mean in Linux partitioning?**

Swap is a special partition used as virtual memory. When your physical RAM is full, the system moves less-used data to swap. It can be a partition or a file.

**How do I list all packages installed on my system?**

For Debian systems, use 'dpkg -l'. For Red Hat systems, use 'rpm -qa'. Both commands output a list of all installed packages.

**What is 'dependency hell' in Linux?**

It's the situation where installing one program requires installing another, which requires another, and so on, often with version conflicts. Package managers like apt and dnf solve this by automatically handling the chain.

## Check your understanding

1. **Which command would you use to install a local .deb file called 'hello.deb'?**

   Answer: Use 'dpkg -i hello.deb' (or 'sudo dpkg -i hello.deb').

2. **What is the main purpose of running 'apt update' before installing a package?**

   Answer: To refresh the local package index from the repositories, so your system knows about the latest available versions.

3. **Name the two main package formats used in Linux distributions.**

   Answer: .deb (used by Debian-based) and .rpm (used by Red Hat-based).

4. **What is the difference between 'remove' and 'purge' in apt?**

   Answer: 'Remove' deletes the program files but retains configuration files; 'purge' removes both program and config files.

5. **Why is partitioning important during Linux installation?**

   Answer: Partitioning isolates different types of data (system files, user files, swap) so that a problem in one partition does not affect the whole system.

---

Interactive version with quiz and diagrams: https://courseiva.com/learn/lpi-lpic1/linux-installation-and-package-management
