Installing software or making system changes from the command line can be a great time saver. Editing the repositories list though has always been something that I have done manually – either opening /etc/apt/sources.list with nano or making the necessary changes via the GUI.
Today, for example, I wanted to install Skype in Ubuntu 11.4 which meant enabling the “partner” repository. In /etc/apt/sources.list this would mean manually removing the # from the following line:
# deb http://archive.canonical.com/ubuntu natty partner
With a bit of searching though I found a solution that can be adapted to enable any of the existing entries in sources.list.
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak STOP="deb " REPO="partner" sudo sed -i "/^# $STOP.*$REPO/ s/^# //" /etc/apt/sources.list
The first command simply makes a backup copy of your sources.list file.
In the second line we define the parameter STOP as “deb ” so that we will only edit deb repositories (and not deb-src repositories which are not needed to install Skype).
The third line defines the REPO parameter which in this case contains a character string that is unique to the ubuntu natty partner repository.
The last line uses the sed command to remove the # according to the parameters that we have set.
This script gives a great and easily customizable framework to work from as STOP will either be “deb ” or “deb-src” and REPO will always be something unique to the repository to be enabled.