Logo

dev-resources.site

for different kinds of informations.

Streamlining .deb Package Installation on Ubuntu: A Better Way to Manage Downloaded Packages

Published at
11/13/2024
Categories
linux
ubuntu
debian
tutorial
Author
dev_michael
Categories
4 categories in total
linux
open
ubuntu
open
debian
open
tutorial
open
Author
11 person written this
dev_michael
open
Streamlining .deb Package Installation on Ubuntu: A Better Way to Manage Downloaded Packages

As an Ubuntu user, I frequently find myself downloading and installing .deb packages from various sources. While Ubuntu's Software Center and apt handle most package installations, there are times when you need to install software directly from .deb files. The standard approach using dpkg -i works, but it could be more user-friendly. Here's how I've streamlined this process with a simple yet powerful bash script.

The Problem with Traditional .deb Installation

The traditional way to install a .deb package involves:

  1. Opening a terminal

  2. Navigating to the download directory

  3. Running sudo dpkg -i package.deb

  4. Manually cleaning up the .deb file afterward

This process lacks safeguards, confirmation prompts, and automatic cleanup. It's not terrible, but it could be better.

The Solution: A Smart Installation Script

I've created a bash script that makes this process more intuitive and safer. Here are its key features:

1. User-Friendly Confirmation

Instead of immediately executing with sudo privileges, the script asks for confirmation first. This prevents accidental installations and provides a moment to double-check.

2. Smart Cleanup

After successful installation, the script asks if you want to delete the .deb file. This helps keep your downloads folder clean without forcing automatic deletion.

3. Non-Interactive Mode

For automation scenarios, the script includes a -y flag that skips the installation confirmation prompt.

Using the Script

Basic Usage

install firefox.deb
Enter fullscreen mode Exit fullscreen mode

This will:

  • Ask for confirmation before installation

  • Install the package with sudo privileges

  • Ask if you want to delete the installer file

Non-Interactive Usage

install -y firefox.deb
Enter fullscreen mode Exit fullscreen mode

This will:

  • Install immediately (no confirmation)

  • Still ask about file cleanup

The Script Explained

The core of the script is quite straightforward:

install () {
  if [ $# -eq 0 ]; then
    echo "No package file specified."
    return 1
  fi

  local package_file="$1"
  local skip_prompt=false

  if [[ "$1" == "-y" ]]; then
    shift
    package_file="$1"
    skip_prompt=true
  fi

  if $skip_prompt; then
    sudo dpkg -i "$package_file"
    install_status=$?
  else
    echo "Are you sure you want to install $package_file (requires sudo)?"
    read -p "Enter (y)es, (n)o, or (Y)es to skip this prompt in the future: " answer
    case $answer in
      [yY] ) 
        sudo dpkg -i "$package_file"
        install_status=$?
        ;;
      n|N ) 
        echo "Installation cancelled."
        return 1
        ;;
      * ) 
        echo "Invalid input. Please enter y, n, or Y."
        return 1
        ;;
    esac
  fi

  if [ $install_status -eq 0 ]; then
    echo "Installation completed successfully."
    read -p "Do you want to delete the package file $package_file? (y/n): " delete_answer
    case $delete_answer in
      [yY] ) 
        rm "$package_file"
        echo "Package file deleted."
        ;;
      * ) 
        echo "Package file retained."
        ;;
    esac
  fi
}
Enter fullscreen mode Exit fullscreen mode

Key components:

  1. Input validation ensures a package file is specified.

  2. Confirmation prompt offers yes/no options to install and yes-always to skip confirmation in future.

  3. Success checking verifies installation before offering file cleanup.

  4. File deletion prompts to help keep the folder tidy.

Bonus: Package Removal

The script also includes a remove function for uninstalling packages:

remove () {
  if [ $# -eq 0 ]; then
    echo "No package name specified."
    return 1
  fi

  local search_name="$1"
  local skip_prompt=false

  if [[ "$1" == "-y" ]]; then
    shift
    search_name="$1"
    skip_prompt=true
  fi

  local packages
  packages=$(dpkg -l | grep "$search_name" | awk '{print $2}')

  if [ -z "$packages" ]; then
    echo "No installed packages found matching '$search_name'."
    return 1
  fi

  echo "Packages found:"
  select package in $packages; do
    if [ -n "$package" ]; then
      break
    else
      echo "Invalid selection. Please select a valid package number."
    fi
  done

  if $skip_prompt; then
    sudo dpkg -r "$package"
    remove_status=$?
  else
    echo "Are you sure you want to remove $package (requires sudo)?"
    read -p "Enter (y)es, (n)o, or (Y)es to skip this prompt in the future: " answer
    case $answer in
      [yY] )
        sudo dpkg -r "$package"
        remove_status=$?
        ;;
      n|N )
        echo "Removal cancelled."
        return 1
        ;;
      * )
        echo "Invalid input. Please enter y, n, or Y."
        return 1
        ;;
    esac
  fi

  if [ $remove_status -eq 0 ]; then
    echo "Package '$package' removed successfully."
    read -p "Do you want to remove configuration files for $package as well? (y/n): " config_answer
    case $config_answer in
      [yY] )
        sudo dpkg -P "$package"
        echo "Configuration files removed."
        ;;
      * )
        echo "Configuration files retained."
        ;;
    esac
  else
    echo "Failed to remove package '$package'."
    return 1
  fi
}
Enter fullscreen mode Exit fullscreen mode

Setting Up the Script

  1. Open your .bashrc file:

    nano ~/.bashrc
    
  2. Add the script functions to the end of the file.

  3. Save and reload your terminal:

    source ~/.bashrc
    

Benefits Over Traditional Methods

  1. Safety: Confirmation prompts prevent accidental installations.

  2. Cleanliness: Optional cleanup of installer files.

  3. Flexibility: Support for both interactive and non-interactive usage.

  4. Consistency: Unified interface for both installation and removal.

Real-World Usage Examples

I frequently use this when installing:

  • Third-party applications not in the Ubuntu repositories.

  • Specific versions of software not available via apt.

  • Custom packages from development teams.

Conclusion

This script has significantly improved my workflow when dealing with .deb packages. It adds safety features and automation while remaining simple and easy to use. Feel free to adapt it to your needs or suggest improvements!

Pro Tips

  1. Keep the script in your .bashrc for easy access.

  2. Consider adding an alias if you prefer a different command name.

  3. Use the -y flag in your automation scripts.


Note: This script assumes you're using Ubuntu or a Debian-based system with dpkg available.

FYI: Most of this article was made/modified by an LLM; Why!? Cause it’s 2024, and we all have better things to do with our time!

debian Article's
30 articles in total
Favicon
Fixes for a critical rsync vulnerability (CVE-2024-12084) have been released for Stable/Bookworm, Testing and Unstable....
Favicon
Debian and KDE 6 - WSL - How to install KDE 6 via Debian - Windows 11 - X410 - Linux - 2024 https://www.youtube.com/watch?v=yrtgmwsptVc
Favicon
Comprehensive Guide: Setting Up Gestures on Linux (Debian-Based Distributions)
Favicon
让安卓手机不再吃灰:在安卓手机上搭建 Rust 开发环境
Favicon
The Importance of Reading Documentation: A Lesson from Nvidia Drivers
Favicon
I changed the arm on my android with Debian no root Linux to the Debian arm my android does wonders now.
Favicon
"Why is it, when something happens, it is always you TWO?"- troubleshooting Bluetooth and Wi-Fi devices on Debian 12
Favicon
Virtualization on Debian with virsh&QEMU&KVM — Installation of virtualization tools and first VM creation
Favicon
The Debian LTS Team is actively working towards ensuring that security fixes made in LTS are also propogated to more recent...
Favicon
OKMX6UL Development Board Debian Filesystem Creation Process (Including Tool Installation, Configuration, and Burning)
Favicon
From Debian to Devuan
Favicon
Debian Outreachy interns selected for the December 2024 round
Favicon
Abilitare SSH root login su Debian Linux Server
Favicon
Calls for bids for DebConf26 have started, please see: https://lists.debian.org/debconf-announce/2024/11/msg00001.html
Favicon
LINUX ÜZERINDE OPERA BROWSER VIDEO OYNATMAMA SORUNU
Favicon
Debian 12 … is amazing! How to: Create your custom codehouse #6 [Giving Voice to Debian: Wireless Audio Devices configuration]
Favicon
Kicksecure: Hardening Your Linux System’s Security (Debian Morph)
Favicon
Managing Large Debian Repositories with Pulp
Favicon
Debian 12 … is amazing! How to: Create your custom codehouse #5 [From Console only to Custom Graphical User Interface]
Favicon
Reviving the Remix Mini PC: A Guide to Running ARM-based OS Images
Favicon
Automating Debian Package Update Summaries with Python and Gemini (gemini-1.5-flash)
Favicon
DebConf26 bids: Please get your information in shape soon!
Favicon
Debian 12: NVIDIA Drivers Installation
Favicon
Debian Secure Boot: To be, or not to be, that is the question!
Favicon
Bulk Linux Users Creation
Favicon
Using Timeshift for System's Snapshots and Recovery on Debian 12 via Command Line
Favicon
Streamlining .deb Package Installation on Ubuntu: A Better Way to Manage Downloaded Packages
Favicon
Debian 12 … is amazing! How to: Create your custom codehouse #4 [Security mechanisms against Network-Based attacks]
Favicon
Debian 12 … is amazing! How to: Create your custom codehouse #3 [Security mechanisms against malware]
Favicon
Debian 12 … is amazing! How to: Create your custom codehouse #2 [Installation & Manual Disk Partitioning with LVM]

Featured ones: