Kokil Thapa - Professional Web Developer in Nepal
Freelancer Web Developer in Nepal with 15+ Years of Experience

Kokil Thapa is an experienced full-stack web developer focused on building fast, secure, and scalable web applications. He helps businesses and individuals create SEO-friendly, user-focused digital platforms designed for long-term growth.

Install Packages with apt on Ubuntu

By Kokil Thapa | Last reviewed: September 2026

You need to install packages with apt on Ubuntu whenever you set up a web server, add PHP, or patch a production VPS. Apt is the default package manager on Ubuntu 22.04 and 24.04 LTS. It reads signed repositories, resolves dependencies, and installs software as root-managed `.deb` units. This guide walks through the commands I use daily on client servers—from a first nginx install to fixing the dreaded lock-file error. If you are new to the terminal, start with our essential Ubuntu terminal commands reference first.

How do you install packages with apt on Ubuntu?

Apt (Advanced Package Tool) sits between you and Ubuntu's package index. You never download random `.deb` files from forums unless you have no other choice. The normal path is: refresh the index, find the exact package name, install, then confirm.

On a fresh Ubuntu Server install, log in as a user with sudo rights. Never run routine apt work as root without sudo—you want audit trails and fewer accidental system-wide edits.

Step 1: Refresh the package index

Before any install, sync your local index with remote repositories. Skipping this step is the most common reason apt reports "Unable to locate package".

sudo apt update

That command does not upgrade installed software. It only downloads new package lists. For a deeper explanation of what happens under the hood, see apt update explained on Ubuntu.

Step 2: Install one or more packages

Use apt install with the exact binary or meta-package name from the repository.

sudo apt install nginx
sudo apt install php8.3-fpm php8.3-cli php8.3-mysql
sudo apt install git curl unzip

Apt shows a summary: new packages, upgraded packages, disk space required. Press Y to confirm, or pass -y in scripts to auto-accept.

sudo apt install -y nginx

Step 3: Verify the installation

Check that binaries exist and services are active.

nginx -v
systemctl status nginx
dpkg -l | grep nginx

dpkg -l lists installed Debian packages. systemctl confirms systemd units—critical on any Ubuntu server setup.

Install Packages with apt on Ubuntuapt updateRefresh indexapt searchFind packageapt installPull depsVerifydpkg / systemctlWhat apt install does on disk• Downloads .deb from mirrors• Resolves dependency tree• Runs maintainer scripts (pre/post)• Registers with dpkg database• Enables systemd units• Logs in /var/log/dpkg.log
Standard apt workflow to install packages with apt on Ubuntu: update the index, search, install with dependency resolution, then verify.
  1. Run sudo apt update to refresh repository metadata.
  2. Search with apt search keyword or browse packages.ubuntu.com for the exact name.
  3. Install with sudo apt install package-name.
  4. Verify with dpkg -l, version flags, and systemctl status where applicable.

What is the difference between apt and apt-get on Ubuntu?

Both commands talk to the same backend: dpkg and the APT library. On modern Ubuntu, apt is the user-facing wrapper Ubuntu recommends. apt-get remains for scripts and older tutorials.

Featureaptapt-get
Progress barYes, coloured outputPlain text
installYesYes
upgradeYes (full-upgrade alias)Yes (dist-upgrade)
searchBuilt inUse apt-cache search
Script stabilityInterface not guaranteed frozenStable for automation
Default on Ubuntu 24.04Recommended for humansStill installed, widely used in CI

For interactive server work, I use apt. For Deployer hooks and GitLab CI jobs on production boxes, I often keep apt-get -y because behaviour has been predictable for years. After installs, run sudo apt upgrade on a schedule—see apt upgrade safely on Ubuntu for the full distinction between upgrade and dist-upgrade.

Official reference: the Ubuntu apt manual page documents subcommands and options for Noble (24.04).

How do you search and find packages before installing with apt?

Package names rarely match application marketing names. PHP-FPM is php8.3-fpm, not "php". MySQL server packages may be mysql-server-8.0 or MariaDB variants depending on the release.

Search from the terminal

apt search nginx
apt search php | grep fpm
apt show nginx

apt show prints version, dependencies, installed size, and repository origin. That last field matters when you audit third-party PPAs.

List installed and available versions

apt list --installed | grep php
apt policy nginx

apt policy reveals which repository version apt will pick. Pinning and extra repos belong in a dedicated Ubuntu repository management workflow—do not add random PPAs on production without reviewing signing keys.

apt Dependency Resolutionnginxnginx-commonlibssl3t64libc6Conflicts and breaks block installapt proposes removals only after explicit confirmSimulate first: apt install --simulate pkg
When you install packages with apt on Ubuntu, apt builds a dependency tree and refuses conflicting combinations unless you confirm changes.

Simulate before risky installs on live servers:

sudo apt install --simulate php8.3-fpm

The dry run prints planned installs and removals without touching the system. Use it before adding Ondřej Surý's PHP PPA on a box that already runs distro PHP.

How do you install a full web stack with apt on Ubuntu?

Most of my Laravel and WordPress deployments on Ubuntu start with the same apt sequence. Versions shift by LTS release; always check apt search on the target server rather than copying stale package names from a blog.

Base tools

sudo apt update
sudo apt install -y curl git unzip software-properties-common

Nginx and PHP

Ubuntu 24.04 ships PHP 8.3 in default repos. PHP 8.5 may require a PPA or newer release—verify with apt search php before scripting.

sudo apt install -y nginx
sudo apt install -y php8.3-fpm php8.3-cli php8.3-mysql php8.3-xml php8.3-mbstring php8.3-curl php8.3-zip

Detailed walkthroughs: install Nginx on Ubuntu and install PHP on Ubuntu.

MySQL or MariaDB

sudo apt install -y mysql-server

MySQL 8.4 LTS remains common on managed hosts. Ubuntu 24.04 may package MySQL 8.0 or 8.4 depending on archive updates—confirm with apt show mysql-server. See install MySQL on Ubuntu for secure mysql_secure_installation steps.

Redis, Node.js, and containers

sudo apt install -y redis-server
sudo apt install -y nodejs npm

For Node.js 26 LTS, many teams prefer NodeSource or nvm over distro packages. Redis 8.x may not be in default Ubuntu repos—check version with redis-server --version after install. Container workflows often skip host Node entirely; see install Docker on Ubuntu instead.

Firewall after exposing services

sudo apt install -y ufw
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable

Pair this with configure a firewall with UFW on Ubuntu and broader server hardening for Ubuntu web servers.

Production Stack via aptnginxweb serverphp-fpmapp runtimemysqldatabaserediscacheLaravel / WordPress / Symfony appSites on shared EC2 use this apt baselineDeployer 7 + GitLab CI deploys app code after stack install
Typical packages installed with apt on Ubuntu for Laravel and WordPress production hosts.

I have used this exact apt baseline on sister legal-tech sites that share a Deployer 7 pipeline—application code ships separately, but the stack always starts with apt. For Symfony-specific tuning after packages land, read Symfony deployment on Ubuntu VPS.

How do you remove, hold, and upgrade packages installed with apt?

Installing is half the lifecycle. You also need clean removal, controlled upgrades, and occasional version holds.

Remove packages

sudo apt remove nginx
sudo apt purge nginx
sudo apt autoremove

remove uninstalls the package but may leave config files. purge deletes configs under /etc. autoremove clears orphaned dependencies pulled in earlier.

Upgrade installed packages

sudo apt update
sudo apt upgrade
sudo apt full-upgrade

Schedule security updates on every public VPS. Unattended upgrades or weekly maintenance windows beat emergency Sunday patches. See Ubuntu security updates guide for automation patterns.

Hold a package at a fixed version

sudo apt-mark hold php8.3-fpm
apt-mark showhold

Holds prevent accidental PHP bumps during a dist-upgrade. Release the hold before deliberate upgrades:

sudo apt-mark unhold php8.3-fpm

How do you fix common apt install errors on Ubuntu?

Production servers throw repetitive apt errors. Most trace back to stale indexes, concurrent apt processes, or broken dependencies—not corrupt hardware.

Unable to locate package

Run sudo apt update. Confirm the package name with apt search. If the software needs a PPA or third-party repo, add the repo first—then update again. Jumping straight to add-apt-repository without understanding signing keys is a common security mistake on client VPS instances.

Could not get lock /var/lib/dpkg/lock

Another apt process—or an unattended upgrade—is running. Wait, or identify the holder:

ps aux | grep -E 'apt|dpkg'
sudo lsof /var/lib/dpkg/lock-frontend

Never delete lock files while dpkg is active. If a process died mid-run:

sudo dpkg --configure -a
sudo apt install -f

dpkg --configure -a finishes interrupted configuration. apt install -f fixes broken dependency states.

Unmet dependencies or broken packages

sudo apt --fix-broken install
sudo dpkg --remove --force-remove-reinstreq problematic-package

Use force removal only when you understand what pulled the package in. Check logs:

less /var/log/dpkg.log
less /var/log/apt/term.log

Hash sum mismatch or 404 from mirror

Switch mirrors or retry after sudo apt update. On Nepal-hosted VPS boxes, I sometimes see transient mirror lag during upstream sync windows. Waiting thirty minutes or changing to the main archive often clears it.

apt Install Error Fixesapt install failedLock file?Wait or kill stale aptNot found?apt update + searchBroken deps?apt install -fRecovery command sequence1. dpkg --configure -a2. apt --fix-broken install3. apt update && apt upgrade
Decision paths when install packages with apt on Ubuntu fails—lock files, missing packages, and broken dependencies.

Wrong file permissions after manual edits under /var/lib/apt also break apt. Restore ownership if you touched those paths during a rushed debug session:

sudo chown -R root:root /var/lib/apt/lists
sudo chmod -R 644 /var/lib/apt/lists/*
sudo chown root:root /var/lib/apt/lists/lock
sudo chown root:root /var/lib/dpkg/lock

File permission fundamentals live in Ubuntu file permissions explained.

Install a local .deb file

Prefer repositories. When a vendor ships a `.deb` directly:

sudo apt install ./teamviewer_amd64.deb

Using apt install ./file.deb—not raw dpkg -i—lets apt pull missing dependencies from repos automatically.

What apt practices matter on production Ubuntu servers?

Treat apt changes like code deploys: predictable, logged, and reversible where possible.

  • Always run apt update immediately before apt install or upgrade.
  • Use --simulate before installing meta-packages that might remove existing tools.
  • Document non-default repos in your runbook—future you will forget why a PPA exists.
  • Snapshot the VPS or verify backups before full-upgrade on a live database server.
  • Reload PHP-FPM and web servers after library upgrades: sudo systemctl reload php8.3-fpm nginx.
  • Keep kernel and microcode upgrades on a separate maintenance window—they often need reboots.

On budget Nepal VPS plans (Rs 800–2,500/month, roughly USD 6–19), a broken apt state can take a site offline longer than application bugs. That is why many clients route server work through Linux system administration in Nepal or ongoing support and maintenance instead of experimenting on production.

Application-layer work—Laravel queues, WooCommerce checkout, API integrations—still depends on this foundation. Browse the Adventure Third Pole Trek portfolio case for an example where Ubuntu packages, PHP-FPM, and deployment automation sit underneath a Livewire booking system.

When you need quick JSON config checks while editing nginx or app env files, the free JSON formatter tool on this site saves a round trip to another tab. For broader context on who maintains these guides, see about me or the main services overview.

Performance tuning after packages are installed belongs in a separate pass—optimize Ubuntu server performance covers sysctl, PHP-FPM pools, and MySQL buffers once apt work is done.

Key Takeaways

  • Run sudo apt update before every install so the index matches live repositories.
  • Install with sudo apt install package-name; verify using dpkg -l and systemctl status.
  • Use apt search and apt show to confirm exact package names before scripting.
  • Fix lock errors by waiting or running dpkg --configure -a, not by deleting locks blindly.
  • Remove cleanly with apt purge plus autoremove when uninstalling services.
  • Simulate risky installs with apt install --simulate on production hosts.

People Also Ask

Do you need sudo to install packages with apt on Ubuntu?

Yes. Installing system packages writes to /usr, /etc, and the dpkg database—all root-owned. Standard users run sudo apt install. Ubuntu disables direct root login by default, which is the safer pattern for SSH-managed servers.

What is the difference between apt install and apt upgrade?

apt install package adds new software. apt upgrade updates already-installed packages to newer versions within the same Ubuntu release. Neither installs a new distribution version—that requires do-release-upgrade.

Can you install multiple packages in one apt command?

Yes. Chain names in one line: sudo apt install nginx php8.3-fpm mysql-server. Apt resolves the combined dependency tree once, which is faster and easier to log than three separate installs.

How do you list all packages installed with apt on Ubuntu?

Run apt list --installed or dpkg-query -l. Filter with grep when hunting a specific stack component. Export the list before major upgrades so you can diff if something disappears.

Build on a clean apt foundation

Every production site I maintain on Ubuntu still starts the same way: update the index, install packages with apt on Ubuntu deliberately, verify services, then harden and deploy application code. Apt is boring technology—and that is exactly why it belongs at the base of your stack. When you want the full server built, secured, and handed over with runbooks, contact us or explore web development services in Nepal for end-to-end delivery.

Frequently Asked Questions

Yes. Installing system packages writes to root-owned paths like /usr, /etc, and the dpkg database. Standard users run sudo apt install. Ubuntu disables direct root login by default, which is the safer pattern for SSH-managed servers.

apt install adds new software. apt upgrade updates already-installed packages within the same Ubuntu release. Neither switches to a new distribution version—that requires do-release-upgrade.

Yes. Chain names in one line, for example sudo apt install nginx php8.3-fpm mysql-server. Apt resolves the combined dependency tree once, which is faster and easier to log than separate installs.

Log in as a user with sudo rights—never run routine apt work as root without sudo. Run sudo apt update to refresh the package index; skipping this is the most common cause of "Unable to locate package" errors. Install with sudo apt install package-name using the exact name from apt search or apt show. Press Y to confirm, or pass -y in scripts. Verify with dpkg -l, version flags like nginx -v, and systemctl status for services. Prefer signed repositories over random .deb files from forums.

Both commands use the same dpkg and APT backend. On Ubuntu 22.04 and 24.04 LTS, apt is the recommended user-facing wrapper with coloured progress output and built-in search. apt-get remains widely used in scripts and CI because its behaviour has been predictable for years. For interactive server work I use apt; for Deployer hooks and GitLab CI jobs on production boxes I often keep apt-get -y. After installs, schedule sudo apt upgrade separately and understand the distinction between upgrade and full-upgrade before dist-upgrade on live servers.

Package names rarely match application marketing names—PHP-FPM is php8.3-fpm, not "php". Run apt search keyword, then apt show package-name for version, dependencies, installed size, and repository origin. That origin field matters when auditing third-party PPAs. Use apt list --installed and apt policy package-name to see which repository version apt will pick. On live servers, run sudo apt install --simulate before risky installs such as adding Ondřej Surý's PHP PPA on a box that already runs distro PHP. Do not add random PPAs on production without reviewing signing keys.

My typical Laravel and WordPress baseline starts with sudo apt update, then curl, git, unzip, and software-properties-common. Install nginx, then php8.3-fpm with php8.3-cli, php8.3-mysql, php8.3-xml, php8.3-mbstring, php8.3-curl, and php8.3-zip—Ubuntu 24.04 ships PHP 8.3 in default repos. Add mysql-server, optionally redis-server and nodejs npm, then ufw with OpenSSH and Nginx Full allowed. Always confirm names with apt search on the target server. PHP 8.5 or Redis 8.x may need extra repos; Node.js 26 LTS is often better from NodeSource or nvm than distro packages.

sudo apt remove uninstalls a package but may leave config files; sudo apt purge also deletes configs under /etc. Run sudo apt autoremove to clear orphaned dependencies pulled in earlier. Upgrade with sudo apt update, then sudo apt upgrade or apt full-upgrade on a schedule—snapshot the VPS or verify backups before full-upgrade on a live database server. Hold a version with sudo apt-mark hold package-name to prevent accidental PHP bumps during dist-upgrade; release with apt-mark unhold before deliberate upgrades. Schedule security updates via unattended upgrades or weekly maintenance windows.

Another apt process or unattended-upgrade is usually running. Wait for it to finish, or identify the holder with ps aux checking apt or dpkg processes and sudo lsof on /var/lib/dpkg/lock-frontend. Never delete lock files while dpkg is active. If a process died mid-run, run sudo dpkg --configure -a to finish interrupted configuration, then sudo apt install -f to fix broken dependency states. Wrong file permissions under /var/lib/apt after manual edits also break apt—restore root ownership on lists and lock files if you touched those paths during a rushed debug session.

Run sudo apt update first—stale indexes cause this constantly on fresh or neglected servers. Confirm the exact package name with apt search or browse packages.ubuntu.com. If the software needs a PPA or third-party repository, add the repo with proper signing key review, then update again before installing. Jumping straight to add-apt-repository without understanding signing keys is a common security mistake on client VPS instances. If you recently added a repo, verify the package appears with apt policy before scripting the install across multiple servers.

Start with sudo apt --fix-broken install, which attempts to resolve dependency conflicts automatically. Run sudo dpkg --configure -a if a previous install was interrupted mid-configuration. For a specific problematic package, sudo dpkg --remove --force-remove-reinstreq is a last resort—only when you understand what pulled the package in and what services depend on it. Check less /var/log/dpkg.log and less /var/log/apt/term.log for the failure point. After recovery, verify affected services with systemctl before returning the server to production traffic.

The --simulate flag runs a dry run that prints planned installs and removals without touching the system. Use it before adding a PHP PPA on a server that already runs distro PHP, or before installing meta-packages that might remove existing tools. On budget Nepal VPS plans costing Rs 800–2,500 per month, roughly USD 6–19, a broken apt state can take a site offline longer than application bugs—simulating first is cheap insurance. Pair it with apt show to audit dependencies and disk space before you confirm a live install.

Prefer signed repositories whenever possible. When a vendor ships a .deb directly, use sudo apt install ./filename_amd64.deb—not raw dpkg -i. Installing a local file through apt still pulls missing dependencies from configured repositories automatically, which dpkg alone will not do. After installation, verify with dpkg -l and check whether the vendor expects a dedicated repository for future upgrades. Document one-off .deb installs in your server runbook so the next maintainer knows why a package is not tracked from main Ubuntu archives.

Treat apt changes like code deploys: predictable, logged, and reversible where possible. Always run apt update immediately before install or upgrade. Use --simulate before risky meta-packages. Snapshot the VPS or verify backups before full-upgrade on live database servers. Reload PHP-FPM and web servers after library upgrades—sudo systemctl reload php8.3-fpm and nginx. Keep kernel and microcode upgrades on separate maintenance windows since they often need reboots. Document non-default repos in your runbook; future you will forget why a PPA exists on a two-year-old VPS.

Run apt list --installed or dpkg-query -l to see everything apt and dpkg track on the system. Filter with grep when hunting a specific stack component, for example piping apt list --installed through grep php to audit PHP extensions before a framework upgrade. Export the list before major upgrades so you can diff if something disappears unexpectedly. On servers I maintain with Deployer 7 and GitLab CI, I keep a baseline package export in the runbook alongside application deploy steps—it saves time when rebuilding a VPS from scratch.

Share this article

0 Comments

Leave a comment

Your email is not published. Comments appear once they have been read. Sign in to have your details filled in.

Quick Contact Options
Choose how you want to connect me: