DEV Community

Michael Amachree
Michael Amachree

Posted on • Originally published at michaelish.hashnode.dev

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!

Top comments (0)