How to install a package with apt or dnf
Topic: Servers linux
Summary
Install a single package on Debian/Ubuntu (apt install) or RHEL/Fedora (dnf install), resolve dependencies, and verify the binary and config location. Use this when you need a specific tool or service and the distro provides it.
Intent: How-to
Quick answer
- Debian/Ubuntu: sudo apt update && sudo apt install PACKAGE; RHEL/Fedora: sudo dnf install PACKAGE. Fix repo or key errors before retrying.
- Confirm the package name with apt search or dnf search; check which and dpkg -L or rpm -ql to verify install path and config.
- If install fails on dependencies, run apt -f install or dnf install with the suggested packages; avoid forcing unless you understand the impact.
Prerequisites
Steps
-
Find the correct package name
Run apt search keyword or dnf search keyword; pick the official package (e.g. nginx not nginx-full unless you need that flavor).
-
Install the package
sudo apt install pkg or sudo dnf install pkg; accept dependencies; note any post-install prompts (e.g. config).
-
Verify installation
which service_binary; dpkg -L pkg or rpm -ql pkg to list files; check /etc for config and systemctl status for services.
-
Enable and start if it is a service
systemctl enable --now unit; or enable only and start when ready; verify with systemctl status and a quick functional test.
Summary
You will install a single package using apt or dnf, confirm it is installed and where files and config live, and enable/start the service if applicable. Use this whenever you add software from the distro repos.
Prerequisites
- Root or sudo; network to package mirrors.
- Knowledge of the package or service name (e.g. nginx, postgresql).
Steps
Step 1: Find the correct package name
apt search nginx
dnf search nginx
Choose the main package (e.g. nginx); use -dev or -doc only if you need headers or docs.
Step 2: Install the package
sudo apt update
sudo apt install nginx
# or
sudo dnf install nginx
Resolve any dependency or repo errors; do not use —force unless you know the consequence.
Step 3: Verify installation
which nginx
dpkg -L nginx | head -20
rpm -ql nginx | head -20
ls /etc/nginx
Step 4: Enable and start if it is a service
sudo systemctl enable --now nginx
sudo systemctl status nginx
curl -s -o /dev/null -w "%{http_code}" http://localhost/
Verification
- Package is listed in dpkg -l or rpm -qa; binary in PATH or known path; config in /etc; service active if it is a daemon.
Troubleshooting
Package has no installation candidate / No match — Repo may be missing or package name wrong; add the right repo or use the exact name from search.
Unmet dependencies — Run sudo apt -f install or install the listed dependencies; if a conflict, consider an alternative package or repo.
Service fails to start — Check journalctl -u unit; often config error or missing dir; fix config and restart.