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.
Though I was a Synaptic user earlier, when I started with the command line tools, I found the command line tools easier and faster.
On Ubuntu there are 3 main commands to manage packages. These are dpkg, apt-* and aptitude.
In this tutorial we shall take a quick look at some of the examples of the apt-get command that can be used to install and manage packages.
Apt-get command examples
The apt-get command can be used to install, remove, update packages. It can also upgrade the entire distro to a new version.
1. 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?"... $
2. 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
To install multiple packages in one go, just mention all of them separated by space. For example:
$ sudo apt-get install php-fpm php-curl
3. Upgrade selected package
To upgrade a selected package just do install again
$ sudo apt-get install nginx
This will upgrade the package if updates are available from the repository.
4. Getting the source code of a package
The source code of any package can be downloaded using the following command
$ apt-get source gbrainy
5. Remove an installed package
Use the remove option with apt-get to remove a package
$ apt-get remove apache2
Apt-cache command examples
Apt-cache is a command that comes with apt-get and can be used to check information about packages.
1. 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.
2. Search packages
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.
3. 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.
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'
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>
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