How can I (de)activate a PPA from the command-line with a single command?

Solution 1:

ppa info is actually saved in separate files in /etc/apt/sources.list. Here is the code which will do the desired action to de-activate or activate desired repo. Save the code given below in a file, say mod-ppa

#!/bin/bash
mydir=/etc/apt/sources.list.d
function getlist(){
    echo -e "\n\tchose one of the following lists"
    echo -e "\t================================"
    for itm in `seq 1 $1`
    do
        echo -e "\t`echo $2 | awk -v x=$itm '{print $x}' | awk -F/ '{print $NF}'`"
    done
}

if [ $# -lt 1 ]
then
    echo "E: mod_ppa: missing operand"
    echo "Try 'mod_ppa --help' for more information."
    exit 0
elif [ $# -lt 2 ]
then
    echo -e "E: syntax error,\nTry 'mod_ppa --help' for more information."
    exit 0
fi

case "$1" in
    -d )
        ppa=`ls $mydir/$2*.list`
        num=`echo $ppa | wc -w`
        if [ `echo $num` -gt 1 ]
        then
            getlist "$num" "$ppa"
        else
            if [ -e $mydir/$2*.list ]
            then
                sed -i "s/^deb\-src/\#deb\-src/" $ppa
                sed -i "s/^deb\ http/\#deb\ http/" $ppa
            else
                echo "E: ppa does not exist/check ppa name"
            fi
        fi
        ;;
    -a )
        ppa=`ls $mydir/$2*.list`
        num=`echo $ppa | wc -w`
        if [ `echo $num` -gt 1 ]
        then
            getlist "$num" "$ppa"
        else
            if [ -e $mydir/$2*.list ]
            then
                sed -i "s/^\#deb\-src/deb\-src/" $ppa
                sed -i "s/^\#deb\ http/deb\ http/" $ppa
            else
                echo "E: ppa does not exist/check ppa name"
            fi
        fi
        ;;
    --help)
        echo "Usage: mod_ppa [OPTION] [PPA NAME'S INITIAL PART]"
        echo "Mandatory argument options."
        echo "-a       Activate certain ppa"
        echo "-d       Deactivate certain ppa"
        ;;
    * ) 
        echo "mod_ppa: invalid option '$1'"
        echo "Try 'mod_ppa --help' for more information."
        ;;
esac

save the script in /home/<username>/bin/, which is in PATH usually. To check write in terminal

echo $PATH

If not, add /home/<username>/bin/ to PATH by adding the following line to .bash_profile or .bashrc whichever is convenient,

export PATH=$PATH:$HOME/bin

Then source the file, write in terminal

source .bash_profile [or source .bashrc]

Give mod_ppa execution permission, write in terminal

chmod +x mod_ppa

Usage

To de-activate a ppa use

sudo mod_ppa -d <ppa_name's_initial_part>

For example, to deactivate ppa:webupd8team/java use

sudo mod_ppa -d webupd8team

To activate a ppa use

sudo mod_ppa -a <ppa_name's_initial_part>

For example, to activate ppa:synapse-core use

sudo mod_ppa -a synapse-core

help

mod_ppa --help

I have assumed you will not be interested in deactivating the basic and default ppa(s) listed in /etc/apt/sources.list. Anyway there is only few, you can deactivate them by hand if you wish to. Sushantp606's answer was quite helpful.

How it works

When any ppa repo is deactivated using Software center (GUI), it actually puts a # in front of all the lines inside corresponding ppa .list file at /etc/apt/sources.list.d/. Usually most of the .list files contain two lines that begins with deb http and deb-src, a few have only one. The above script is doing the same thing using sed. First it checks the ppa .list (input by user) is present or not. Then put or remove a # in front the lines accordingly.

As the script is supposed to deal with some sensitive part of OS, I tried to code it defensively. Kept checks for what arguments are being passed to the script and set some error messages accordingly. Also a small help is added. These part increases the length of code.

Solution 2:

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.

Solution 3:

By editing

/etc/apt/sources.list

file from the command line, we can add, remove, or temporarily disable software repositories.just comment out unwanted repo via terminal .

I cant make the command ,its just an idea . let me know if you could connect it via terminal command like-

The command to comment out the source repositories in /etc/apt/sources.list is:

sudo sed -i 's/^deb\-src/\#deb\-src/' /etc/apt/sources.list

and to un comment it use

sudo sed -i 's/^\#deb\-src/deb\-src/' /etc/apt/sources.list

use repository you want to disable/comment out instead of s/^\#deb\-src/deb\-src/ and before doing any change make a backup source list file as below-

sudo cp -v /etc/apt/sources.list /etc/apt/sources.list.backup

Solution 4:

Take a look at How to disable a particular PPA?. Although the first answer by qeoh uses the GTK frontend, the second answer by minimec suggests commenting out the ppa in /etc/apt/sources.list. I'm not sure if this is what you're looking for, but this is a possible method.