How To Install A Package In Ubuntu

How To Install A Package In Ubuntu

Reading time1 min
#Linux#OpenSource#Ubuntu#PackageManagement#Apt#Debian

Mastering Package Installation in Ubuntu: Beyond Basic Apt Commands

Installing packages on Ubuntu may seem straightforward—just run a quick sudo apt install package-name, and you're done. But if you’ve ever faced broken dependencies, version conflicts, or slow installs, you know it’s often not that simple. Efficient and correct package installation is foundational for your system’s stability, security, and performance.

In this post, we’ll dive deeper than the basics of apt install. You'll learn advanced methods to install packages smarter, troubleshoot common issues, and optimize your workflow. Whether you’re a Linux newbie or an experienced user wanting to level up your package management skills, this guide has practical tips you don’t want to miss.


Why Go Beyond apt install?

The apt tool (short for Advanced Package Tool) is powerful but covers just the surface of what Ubuntu’s package management system can do. Here are some reasons to explore beyond the standard install command:

  • Dependency issues: Some packages require exact versions of libraries or dependent packages.
  • Version control: The official repositories often lag behind the latest software releases.
  • Multiple sources: Install software from PPAs (Personal Package Archives), .deb files, Snap, Flatpak, or even source code.
  • Package removal and cleanup: Properly removing or purging packages to avoid junk buildup.
  • Cache management: Handling cached files for quick reinstalls without wasting disk space.

1. Searching Packages Effectively with apt-cache and apt

Before installing, it helps to precisely find the package you need.

apt search <keyword>

Example:

apt search nginx

But often better is using:

apt-cache policy <package-name>

To see versions available and priority order:

apt-cache policy nginx

This tells you what version is installable from which repository—very handy if you're deciding between stable and testing packages.


2. Installing Specific Package Versions

Sometimes you need a specific version rather than the default.

Check available versions:

apt-cache madison <package-name>

Example:

apt-cache madison curl

Then install your desired version like this:

sudo apt install curl=7.68.0-1ubuntu2.7

If dependencies cause trouble while installing an older/newer version, try holding or pinning the version (covered next).


3. Holding (Pinning) Packages to Avoid Unintended Upgrades

If you want to keep a package at a certain version while upgrading others:

sudo apt-mark hold <package-name>

To unhold later:

sudo apt-mark unhold <package-name>

This prevents automatic upgrades and preserves system stability when crucial software must remain at a fixed version.


4. Using PPAs Safely

Ubuntu PPAs are third-party repositories that provide newer or alternative versions of software not in official repos.

Add a PPA with:

sudo add-apt-repository ppa:<ppa-name>
sudo apt update
sudo apt install <package>

Example—for installing latest Git:

sudo add-apt-repository ppa:git-core/ppa
sudo apt update
sudo apt install git

Important: Use PPAs from trusted sources only; adding unreliable ones can break your system or expose security risks.


5. Installing .deb Files Manually

Downloaded .deb files can be installed via:

sudo dpkg -i package.deb

If there are dependency errors afterward:

sudo apt-get -f install

This command fixes broken dependencies automatically — a lifesaver if dpkg complaints leave your system in limbo.


6. Using Other Package Formats: Snap and Flatpak

Ubuntu supports alternative packaging systems ideal for sandboxed software deployment.

  • Snap example installing Spotify:
sudo snap install spotify
  • Flatpak example (requires Flatpak installed):
flatpak install flathub com.spotify.Client

These are great for desktop apps or when official repos lag behind upstream.


7. Removing Packages Correctly

Avoid clutter with proper removal commands:

  • Remove package but keep config files:
sudo apt remove <package-name>
  • Remove package including config files (“purge”):
sudo apt purge <package-name>

After removal, clean unused dependencies with:

sudo apt autoremove

And clear the cache to save space with:

sudo apt clean 
# Or partial clean:
sudo apt autoclean 

8. Troubleshooting Common Installation Issues

Fix Broken Dependencies

If you get errors about broken dependencies after installation attempts:

sudo apt --fix-broken install

or

sudo dpkg --configure -a  

Clear and Rebuild Cache

Sometimes an outdated cache causes problems; refresh it with:

sudo apt update  

You can also clear local cache manually from /var/cache/apt/archives.

Check Held Packages Blocking Installs

List held packages with:

dpkg --get-selections | grep hold  

Unhold if necessary via apt-mark unhold.


Summary: Smarter Ubuntu Package Installation in Practice

ActionCommand ExampleNotes
Search packagesapt search nginxFind packages by keywords
Check policy/versionapt-cache policy nginxSee repo versions & priority
Install specific versionsudo apt install curl=7.xx.xxChoose exact package version
Hold packagesudo apt-mark hold gitAvoid unwanted upgrades
Add trusted PPAsudo add-apt-repository ppa:git-core/ppaGet newer software safely
Install .debsudo dpkg -i file.deb && sudo apt-get -f installManual installs + automatic dependency fix
Snap / Flatpaksnap install spotify / flatpak commandsAlternative sandboxed app installs
Remove/purge & cleanupapt purge, autoremove, cleanKeep system lean & tidy

By going beyond just typing sudo apt install, you gain more control — reducing downtime due to conflicts or errors — and optimize your daily Linux usage significantly.

Master these techniques to become confident in managing Ubuntu systems like a pro!


Got questions or tips about Ubuntu package installation? Drop them in the comments—I love sharing ideas!