This question comes up frequently on the Linux_Newbies list. There are various commands that work, so if you've heard something different, and it works well for you, then use it. There are variations of commands that one person, such as myself, uses more out of habit than anything else.
RPMS are packages compressed using the RedHat Package Manager (ergo, R P M). Both KDE and Gnome have graphic interfaces for installing RPMS, however, some folks seem to have difficulty using them and of course, one feels more like a Linux ace if they install via command line.
I am only going to cover the RPM, tar.gz tar.bz2 and tar.Z formats here. Slack and Debian have their own formats--for example, with Slack, you can use installpkg, but any distro will work with tar.Z, tar.gz and tar.bz2 files. (NOTE: Sometimes tar.gz files have the extension .tgz and tar.bz2 files may have the extension .tbz. This comes from the days when there were more limitations on file name length and calling something .tar.gz already used up 5 characters.)
RPMS are the simplest. First get your RPM, either by downloading it, or perhaps, by using the distro's CD. For example, the RedHat 7.1 CD includes a variety of packages in a directory on both CDs named RedHat/RPMS.
Many packages have to be installed as root. So, if you get permission problems in trying to install something, simply log in as root to do so.
We'll use openssh as an example. I don't know the current version number off the top of my head, but let's say it's 2.7-9. If installing from CD, it's easiest to first change your working directory to the RPMS directory--otherwise, you'll have to type the full path, which, in RH 7.1 would be /mnt/cdrom/RedHat/RPMS/openssh-2.7-9. In the following example, we change to the RPM directory on the CD and install. (I'm assuming you've already mounted the cdrom--if not, the command
(this will give you the version number and let you know if you
might need or want other related packages such as
|rpm -Uvh openssh-2.7-9|
You will then get a message that it's preparing, and see hash marks as the package installs. At the end, hopefully you see a message that it's installed.
The above rpm command is my habit. U means upgrade--this way, if I already have an earlier version of the package, it will be upgraded. If I don't have the package on my system, then it will simply be installed. v is for verbose, to give me any messages that might be helpful during installation and h stands for hash marks, giving me a visual indication of progress. I find this combination works best for me.
tar.gz and tar.bz2 files are installed slightly differently. (But see note below about not necessarily needing the z and j flags) First, I'll assume that you've cd'd over to the directory where the tarball is. This is important!! You have to type the path to the tarball. A frequent newcomer question is that they get the error no such file. This is because they download it to where it downloads, (often a directory called downloads), and then they run the command from somewhere else on their system. So, let's assume you have downloaded the tarball to a directory called downloads. Again we'll use openssh-2.7-9 and assume we have a package called openssh-2.7-9.tar.gz.
cd downloads tar ztf
will list the contents of the tarball. Now, we'll decompress and untar it. Note that in most cases, one can use the "less" command and this will also list the contents. That is, typing, "less openssh-2.7-9.tar.gz" without the quotes, will list the contents of the tarball.
Note that tar commands usually don't require the - in front of the options. (This might vary with your version of tar.) That is, you can do tar -zxvf or tar zxvf and either one will work.
|tar zxvf openssh-2.7-9.tar.gz|
You'll see various things happening on your screen as the tarball is unpacked and unzipped. (A quick note on this--if there is nothing else in the current directory named openssh then you could simply use tar xzvf openssh* and lazily avoid typing out the version number).
If you now do
you'll see that you still have openssh-2.7-9.tar.gz and a new directory called openssh-2.7-9. The flags we added did the following: The z decompressed, or unzipped, if you prefer, the file, the x then extracted its various parts. The v is for verbose, helpful if there are errors and f is for file.
I'm not sure if this is recent, or simply something that I learned fairly recently, but in most cases, you shouldn't need the z (or y or j as described below for bz2 files.) Usually, a simple tar xvf will work, with tar being clever enough to determine the type of compression and automatically use the proper decompression technique.
If you now cd to the new directory and do ls, you'll see a variety of files in there. One of them will be called README. It will give directions of how to install the package. (It may simply direct you to read another file called INSTALL that is often included.) So, to view README
From there, each package is different. A typical sequence of commands might be (but DO NOT try this one without reading the README as other packages have totally different commands)
If the file is a tar.bz2 rather than tar.gz the process is almost the same. However, instead of typing tar -zxvf filename.tar.gzyou would type
|tar -jxvf filename.bz2|
With some distributions of Linux, they're using different versions of bunzip2 and this won't work. You will get a message that j is an unknown option. If this happens, then try again with y.
|tar yxvf filename.bz2.|
One of these should work, but if neither does then
|bunzip2 filename.bz2 && tar -xvf
The bunzip2 command decompresses the file. The && means that if the command completed successfully, then perform the following command which in this case is the tar -xvf. (As mentioned above, extract the various parts of the file, with the verbose and file options).
So, once again using our openssh tarball, it'd be
|bunzip2 openssh-2.7.9.tar.bz2 && tar -xvf openssh-2.7.9.tar|
|uncompress filename.tar.Z && tar -xvf filename.tar|
Note that there are also non-tarred files that are simply compressed. If you have a file called file.bz2 and do tar -jxvf on the file you'll get an error message that it's an inappropriate file type or the like. If the file is called file.bz2 rather than file.tar.bz2 then just decompress it with the appropriate program. If it's a .bz2 file then
If it's a file.gz then substitute gunzip for bunzip2 and if it's just a .Z then the command is uncompress file.Z
From time to time, you might have to open a Microsoft .zip format file. There are zip and unzip utilities. In FreeBSD they're in /usr/ports/archivers/zip and /usr/ports/archivers/unzip. I think that they both install the other as a dependency, but I wouldn't swear to that. Most Linux distributions also have these two utilities available.A relatively new format is xz. The Linux kernel, among other things, uses it. I've only tested on Fedora. I had to run
yum install xz
Once I did that, I was able to extract the kernel tarball with tar xvf kernel-whatever.xz If it's just a compressed xz file, as opposed to a tar.xz, to simply decompress, the command is xz -d file.xz
Lastly, you might need to know how to collect and compress the files. Lets say you have a bunch of great bash scripts that you want to send your friend as a tarball. They're all in a directory called bashscripts. Again, there are various ways to do it (and some that might be simpler than this) but, like everyone else, I have my habits. So, we're in the directory that houses the directory bashscripts. You're going to tar it into a file called scripts.tar and then compress that file into a tar.bz2 file. (I haven't compressed into a tar.Z in a long time--most distros have bzip2 and gzip, so the following should work)
|tar -cf scripts.tar bashscripts && bzip2 scripts.tar|
(If your browser broke that, it should be on one line)
If you'd wanted to make a tar.gz file instead you would substitute gzip for bzip2.
tar -cf scripts.tar---this gives the name of the new file, which consists of the files that you tarred together. bashscripts is obviously all the files in the directory bashscripts. && means that if the command completes successfully, go on to the following command. bzip2 compresses the file (as does gzip). When you're done, you'll find that you have a new file called scripts.tar.bz2 ( or scripts.tar.gz)
The other way to do it, at least with most versions of tar (check the man page to see if it's supported) is to compress it as you're tarring it. This can be done with the j y or z option. In FreeBSD, at least, one has to put the j before the cf or it doesn't work, for example
tar -jcf scripts.tar bashscripts
If you do it the first way, tar -cf scripts.tar && bzip2 scripts.tar the .bz2 suffix is appended to the filename, so you would automatically have a file called scripts.tar.bz2. If you do it the second way, with -jcf then the file will be called whatever you chose to call it---in this case, scripts.tar. Doing a file scripts.tar will show that it is a bzipped file. You can manually add the bz2 suffix or call the file that in the original command, e.g.
tar -jcf scripts.tar.bz2 bashscripts
(This can also work with the -z option.)
That's all there is to it. This should get you started.