Manage packages from the commandline
The more common way to manage packages or softwares is from Synaptic Package Manager. Its the easiest gui tool to install and remove software from your debian/ubuntu system.
However for those who prefer the console, there are plenty of tools to do the job as easily. In this tutorial we are going to look into apt, the package management tool used on ubuntu. Even I used to use the synaptic gui earlier, when I started with the commandline tools, I found the commandline tools easier and faster.
On ubuntu there are 3 main commands to manage packages. These are dpkg, apt-* and aptitude. So lets start experimenting with these one by one.
List all installed packages
This command will list all the installed packages.
$ dpkg -l
Search installed packages
Search the installed packages for 'apache'
$ dpkg --get-selections | grep 'apache' apache2 install apache2-mpm-prefork install apache2-suexec install apache2-utils install apache2.2-bin install apache2.2-common install libapache-pom-java install libapache2-mod-fcgid install libapache2-mod-php5 install
Or use the same l option to search for installed packages. This is actually easier.
$ dpkg -l php* Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) ||/ Name Version Architecture Description +++-===============================-====================-====================-==================================================================== un php-doc <none> (no description available) un php-imlib <none> (no description available) un php-kolab-filter <none> (no description available) un php-openid <none> (no description available) un php-pear <none> (no description available) un php-radius-legacy <none> (no description available) ii php5 5.4.6-1ubuntu1.2 all server-side, HTML-embedded scripting language (metapackage) ii php5-cgi 5.4.6-1ubuntu1.2 amd64 server-side, HTML-embedded scripting language (CGI binary) ii php5-cli 5.4.6-1ubuntu1.2 amd64 command-line interpreter for the php5 scripting language ii php5-common 5.4.6-1ubuntu1.2 amd64 Common files for packages built from the php5 source
List files installed by a package
To find the path of all files that are installed by a certain package use "L" option with dpkg
$ dpkg -L php5-cli /. /etc /etc/php5 /etc/php5/cli /usr /usr/share /usr/share/man /usr/share/man/man1 /usr/share/man/man1/php5.1.gz /usr/share/doc /usr/share/lintian /usr/share/lintian/overrides /usr/share/lintian/overrides/php5-cli /usr/lib /usr/lib/php5 /usr/lib/php5/20100525 /usr/bin /usr/bin/php5 /etc/php5/cli/conf.d /usr/share/doc/php5-cli
dpkg -i <package.deb> Installs a Debian package file; one that you downloaded manually, for example. dpkg -c <package.deb> Lists the contents of <package.deb>, a .deb file. dpkg -I <package.deb> Extracts package information from <package.deb>, a .deb file. dpkg -r <package> Removes an installed package named <package> dpkg -P <package> Purges an installed package named <package>. The difference between remove and purge is that while remove only deletes data and executables, purge also deletes all configuration files in addition. dpkg -L <package> Gives a listing of all the files installed by <package>. See also dpkg -c for checking the contents of a .deb file. dpkg -s <package> Shows information on the installed package <package>. See also apt-cache show for viewing package information in the Debian archive and dpkg -I for viewing package information extracted from a .deb file. dpkg-reconfigure <package> Reconfigures an installed package, if it uses debconf (debconf provides that consistent configuration interface for package installation). You can reconfigure debconf itself if you want to change the front-end or priority of questions asked. For example, to reconfigure debconf with the dialog front-end, you simply run: dpkg-reconfigure --frontend=dialog debconf echo ``<package> hold'' | dpkg --set-selections Put <package> on hold (command line method) dpkg --get-selections ``<package>'' Get the current status of <package> (command line method) dpkg -S <file> Searches for <file> in package database, telling you which packages have that file in them.
Add new repository
The software sources are stored in the file called /etc/apt/sources.list. So if you need to add a new repository
$ sudo apt-add-repository ppa:lubuntu-desktop/ppa
Or if its a full url then
add-apt-repository 'deb uri distribution [component1] [component2] [...]'
$ add-apt-repository 'deb http://extras.ubuntu.com/ubuntu quantal main' $ add-apt-repository 'deb-src http://extras.ubuntu.com/ubuntu quantal main'
Next command is apt-get.
Say Moo with apt-get
This is the most important thing to do with apt-get. That is, to say Moo....
$ apt-get moo (__) (oo) /------\/ / | || * /\---/\ ~~ ~~ ...."Have you mooed today?"... $
Install a new package
This is the most common command. The apt-get install command will install the package and pull in all necessary dependencies, that is other packages that are needed to run the current required package. Usage is again, very simple
$ sudo apt-get install apache2
Upgrade selected package
To upgrade a selected package just do install again
$ sudo apt-get install nginx
This will upgrade if updates are available.
Getting the source code of a package
The source code of any package can be downloaded using the following command
$ apt-get source gbrainy
Remove an installed package
Use the remove option with apt-get to remove a package
$ apt-get remove apache2
Find dependencies of a package
To find the dependencies of a certain package, use the apt-cache command
$ apt-cache depends apache2 apache2 |Depends: apache2-mpm-worker |Depends: apache2-mpm-prefork |Depends: apache2-mpm-event Depends: apache2-mpm-itk Depends: apache2.2-common Conflicts: apache2:i386
It will tell what extra packages does a certain package depends on and what packages conflict with it. It will also list the packages that shall be removed on installing this package and all recommended packages to install with this package.
The apt-cache command can be used to search the packages from the console. This is useful when working on a remote server where there is no gui available. But moreover, the console is more powerful and faster.
$ apt-cache search nginx
The apt-cache command by default searches both the package name and the description. So to fine tune the results we need to filter it out further by using grep.
apt-cache search nginx | grep nginx lua-nginx-memcached - Pure Lua memcached client driver for the nginx embedded Lua language lua-nginx-redis - Pure Lua redis client driver for the nginx embedded Lua language nginx - small, powerful, scalable web/proxy server nginx-common - small, powerful, scalable web/proxy server - common files nginx-doc - small, powerful, scalable web/proxy server - documentation nginx-extras - nginx web/proxy server (extended version) nginx-extras-dbg - nginx web/proxy server (extended version) - debugging symbols nginx-full-dbg - nginx web/proxy server (standard version) - debugging symbols nginx-light - nginx web/proxy server (basic version) nginx-light-dbg - nginx web/proxy server (basic version) - debugging symbols nginx-naxsi - nginx web/proxy server (version with naxsi) nginx-naxsi-dbg - nginx web/proxy server (version with naxsi) - debugging symbols nginx-naxsi-ui - nginx web/proxy server - naxsi configuration front-end nginx-full - nginx web/proxy server (standard version)
The apt-cache search command supports regular expression.
$ apt-cache search ^nginx$ nginx - small, powerful, scalable web/proxy server nginx-extras - nginx web/proxy server (extended version) nginx-light - nginx web/proxy server (basic version) nginx-naxsi - nginx web/proxy server (version with naxsi) nginx-naxsi-ui - nginx web/proxy server - naxsi configuration front-end nginx-full - nginx web/proxy server (standard version)
apt-cache show <package> Shows the full description of <package>. apt-cache showpkg <package> Shows a lot more detail about <package>, and its relationships to other packages.
Which repository does a package belong to
Users often add extra repositories to install software from other sources. If we want to find out, which repository a package is coming from then the apt-cache command can tell that.
$ apt-cache policy wine wine: Installed: (none) Candidate: 1.4.1-0ubuntu5 Version table: 1.4.1-0ubuntu5 0 500 http://in.archive.ubuntu.com/ubuntu/ raring/universe amd64 Packages
The above output shows that the package wine is provided by the raring ubuntu repository.
Lets take another example
$ apt-cache policy google-chrome-stable google-chrome-stable: Installed: 28.0.1500.45-r205727 Candidate: 28.0.1500.70-r209565 Version table: 28.0.1500.70-r209565 0 500 http://dl.google.com/linux/chrome/deb/ stable/main amd64 Packages *** 28.0.1500.45-r205727 0 100 /var/lib/dpkg/status
The above output shows that google chrome is provided by http://dl.google.com/linux/chrome/deb/ repository.
Aptitude is the another command that can be used to manage packages from the commandline just like dpkg and apt-get. Aptitude is considered better and superior to apt-get for a number of reasons. One benefit of aptitude is that when removing a certain package, aptitude will remove all dependencies of that package as well, so that they are not left behind as orphan packages. Apt-get and synaptic cannot do that and lead to unused/orphan packages.
Install a package
Same as apt-get, just use the install command with aptitude to install any package
$ sudo aptitude install firestarter
Again, same as apt-get, use the remove option.
$ sudo aptitude remove firestarter
Search the packages
One of the most useful and frequent task is to search for packages and install any necessary ones. Searching from the commandline is very very handy on a remote server where you dont have a gui and as well on a local system, where you dont want to launch the synaptic package manager.
The following command shall search all packages that have the word nginx in their package name.
$ aptitude search nginx
The following will search packages which have either php or admin in their names.
$ aptitude search php admin
To search for all search terms together wrap in single quotes. The following will search for all packages that have both php and admin in their names
$ aptitude search 'php admin' p mlmmj-php-web-admin - administrative web interface for mlmmj, written p phpldapadmin - web based interface for administering LDAP serve i phpmyadmin - MySQL web administration tool i phppgadmin - web-based administration tool for PostgreSQL
To search both the package name and description use the d modifier.
aptitude search nginx ~dnginx aptitude search 'firmware ~dwireless'
The search function supports regex as well
aptitude search ^nginx$
So construct any kind of regular expression you need and search right away.
Search installed packages
Lets say we want to search for all packages with the name 'apache' in it. The aptitude command can be used with the i switch as shown below
aptitude search ~inginx aptitude search '~inginx'
$ aptitude search '~iapache' i apache2 - Apache HTTP Server metapackage i A apache2-mpm-prefork - Apache HTTP Server - traditional non-threaded model i apache2-suexec - Standard suexec program for Apache 2 mod_suexec .....
Or use grep instead of the i switch.
$ aptitude search nginx | grep ^i i nginx - small, powerful, scalable web/proxy server i A nginx-common - small, powerful, scalable web/proxy server i A nginx-full - nginx web/proxy server (standard version)
Quick and easy.
To reinstall a package using aptitude use the reinstall command
aptitude reinstall <package>
Reconfigure installed packages
Packages can be reconfigured using the dpkg-reconfigure command. For example reconfiguring phpmyadmin.
$ dpkg-reconfigure phpmyadmin
The entire distro upgrade can be done from the terminal as well. For example when you need to upgrade ubuntu on your server. Here is the command
# prepare the system sudo apt-get update sudo apt-get upgrade sudo apt-get dist-upgrade # upgrade distro sudo apt-get install update-manager-core sudo do-release-upgrade
List the repositories
To list the current repositories read the /etc/apt/sources.list and /etc/apt/sources.list.d/* files from the commandline and use grep to filter accordingly. Here are few examples
The following will list the deb repositories (and not the deb-src) from /etc/apt/sources.list file.
$ cat /etc/apt/sources.list | grep "^deb\s" deb http://in.archive.ubuntu.com/ubuntu/ raring main restricted deb http://in.archive.ubuntu.com/ubuntu/ raring-updates main restricted deb http://in.archive.ubuntu.com/ubuntu/ raring universe deb http://in.archive.ubuntu.com/ubuntu/ raring-updates universe deb http://in.archive.ubuntu.com/ubuntu/ raring multiverse .....
The following will list the deb repositories (and not the deb-src) from /etc/apt/sources.list file and /etc/apt/sources.d/* files.
$ grep -h "^deb\s" /etc/apt/sources.list /etc/apt/sources.list.d/* deb http://in.archive.ubuntu.com/ubuntu/ raring main restricted deb http://in.archive.ubuntu.com/ubuntu/ raring-updates main restricted deb http://in.archive.ubuntu.com/ubuntu/ raring universe deb http://in.archive.ubuntu.com/ubuntu/ raring-updates universe deb http://in.archive.ubuntu.com/ubuntu/ raring multiverse deb http://in.archive.ubuntu.com/ubuntu/ raring-updates multiverse .....
To list both deb and deb-src repositories
$ grep -h ^deb /etc/apt/sources.list /etc/apt/sources.list.d/* deb http://in.archive.ubuntu.com/ubuntu/ raring main restricted deb-src http://in.archive.ubuntu.com/ubuntu/ raring main restricted deb http://in.archive.ubuntu.com/ubuntu/ raring-updates main restricted deb-src http://in.archive.ubuntu.com/ubuntu/ raring-updates main restricted deb http://in.archive.ubuntu.com/ubuntu/ raring universe
So njoye the terminal