Ubuntu – How to (de)activate a PPA from the command-line with a single command

command lineppasoftware-sources

Ubuntu offers an easy GUI interface that allows you to edit, (de-)activate and remove PPAs.

But going through all the GUI dialogs and menus can sometimes be tedious, especially if you have a lot of PPAs added to your system.

That's why I was wondering if there was a way to disable (and enable) a PPA from the command-line.


Note: I am not talking about adding/removing the PPA (that's quite easy to do: sudo add-apt-repository ppa / sudo add-apt-repository --remove ppa). What I am looking for is a way to temporarily disable a PPA and reenable it at will – all from within the CLI.


Edit:

Sushantp606 and Davidson Chua's answers were a good starting point and made me change the scope of my question. It's certainly good to know that repositories can be managed with the sources.list files but this still looks like a very tedious task to me. I would love to know if there is a way to automate this in the same manner the Software Properties window does.

Ideally I would like to find a command that will make it possible to quickly enable and disable a PPA by its PPA address, e.g.:

ppa_activate ppa:synapse-core/ppa

and

ppa_deactivate ppa:synapse-core/ppa

Best Answer

Even a simpler script to toggle between activating or deactivating a particular ppa. Save the code given below in a file, for instance toggle_ppa.sh.

#!/bin/bash
#
# toggle_ppa.sh
#
# created by souravc (https://askubuntu.com/users/127327/)
# modified by Glutanimate (https://askubuntu.com/users/81372/)
#
# originally released at https://askubuntu.com/q/383605/81372
#
# DESCRIPTION:  Detects if a PPA is active/inactive and deactivates/activates it
#               on user confirmation.
#
# USAGE:        toggle_ppa.sh ppa:launchpaduser/ppaname

### VARIABLES

SOURCEDIRECTORY=/etc/apt/sources.list.d
PPA="$1"

### USAGE CHECKS

## Arguments

if [ -z "$PPA" ]
then
    echo "Error: Please provide a PPA name to toggle between activation/deactivation"
    echo "The PPA name should be formatted as it appears on launchpad, e.g.:
"$0" ppa:webupd8team/y-ppa-manager"
    exit 1
fi

## Root privileges

if [ "$(whoami)" != "root" ]; then
  echo "Error: This script needs root privileges. Restarting..."
  sudo "$0" "$1"
  exit
fi

### MAIN

SOURCELIST_NOPFX="${PPA#*:}" #remove 'ppa:' prefix
SOURCELIST="${SOURCELIST_NOPFX////-}"-$(lsb_release -cs) #replace all slashes with dashes, include release
SOURCEFILE="$SOURCEDIRECTORY"/"$SOURCELIST".list #compose sources list path

if [ -e "$SOURCEFILE" ]
then
    echo "Processing $SOURCEFILE..."
    SOURCE_COMMENTED=$(grep "^\#deb\ " "$SOURCEFILE") #check if sources line is commented
    if [ -z "$SOURCE_COMMENTED" ]
    then
        echo "$PPA is active. Going to deactivate. Proceed? [ y/n ]"
        read ANSWER
        if [ $ANSWER == "y" ]
        then
            sed -i "s/^deb\-src/\#deb\-src/" $SOURCEFILE
            sed -i "s/^deb\ http/\#deb\ http/" $SOURCEFILE
            echo "Updating package index files..."
            sudo apt-get update
            echo "Done."
        else
            echo "Aborted."
            exit 0
        fi
    else
        echo "$PPA is inactive. Going to activate. Proceed? [ y/n ]"
        read ANSWER
        if [ $ANSWER == "y" ]
        then
            sed -i "s/^\#deb\-src/deb\-src/" $SOURCEFILE
            sed -i "s/^\#deb\ http/deb\ http/" $SOURCEFILE
            echo "Updating package index files..."
            sudo apt-get update
            echo "Done."
        else
            echo "Aborted."
            exit 0
        fi
    fi
else
    echo "Error: Source file at $SOURCEFILE for $PPA does not exist. Please check PPA name."
    exit 0
fi

Follow the procedure given at the other answer to keep file in PATH and make it executable.

Usage

sudo toggle_ppa.sh <full-ppa-name>

Example

sudo toggle_ppa.sh ppa:webupd8team/java

How it works

The working principle of this code is the same as in my other answer. The code acts in a very interactive manner. When someone runs this along with ppa name as its argument, it will display the PPA's current status and what the code is going to do on successful execution. Then it will ask permission of the user. Only if the user inputs 'y' to confirm the code will change the status of the PPA and activate/deactivate it. It will immediately abort if the user puts an 'n' for no.