Can you install multiple versions of a single library?
Yes, definitely! Package management under *nix actually makes this very simple. The only thing it will require is that the packages do not conflict (which means a couple tricks might need to be done by the packager). But, your real question requires a more in-depth answer.
Would installing multiple versions of a library like this make software using the different versions work properly?
Maybe. It depends on how each piece of software links to the required library. For example, if you take a look in
/usr/lib, you'll notice that most libraries have at least two different files in this directory (
# is a number). You will likely notice that
libname.so is usually a symlink to the second file. Typically speaking, this is because software can choose to either link to a specific version of a library, or link to whatever version of the library is unversioned (usually the latest version).
Now, if each of the pieces of software has such specific requirements for which version of the library it uses, they may each choose directly to the version they need (rather than hoping that the unversioned library is the one they want). If this is the case, then you're probably home free, and all you need to do is have the different library versions installed correctly. If, however, one of these pieces of software links to the unversioned link but expects it to be a particular version of the library, it will act up unless the unversioned link happens to be the version they expect.
Let's use an example to demonstrate. Under Arch Linux, two versions of
libpng are officially packaged (
1.6). I happen to have both installed.
$ ls -l /usr/lib | grep png
lrwxrwxrwx 1 root root 18 Mar 12 08:05 libpng12.so -> libpng12.so.0.51.0*
lrwxrwxrwx 1 root root 18 Mar 12 08:05 libpng12.so.0 -> libpng12.so.0.51.0*
-rwxr-xr-x 1 root root 168K Mar 12 08:05 libpng12.so.0.51.0*
lrwxrwxrwx 1 root root 19 Jun 27 19:53 libpng16.so -> libpng16.so.16.12.0*
lrwxrwxrwx 1 root root 19 Jun 27 19:53 libpng16.so.16 -> libpng16.so.16.12.0*
-rwxr-xr-x 1 root root 216K Jun 27 19:53 libpng16.so.16.12.0*
lrwxrwxrwx 1 root root 11 Jun 27 19:53 libpng.so -> libpng16.so*
Now, looking at this set of files, you'll notice that
libpng.so (the completely unversioned link) points to
libpng16.so (a versioned link with no soname) which points to
libpng.so.16.12.0. So, if a piece of software chooses to link to
libpng.so, then on my system, it's going to link to
libpng.so.16.12.0; and, hopefully, that's what that piece of software expects. However, if that piece of software actually expects a version of
1.2, there are going to be issues.
The conclusion here is, if you are packaging the software, figure out what hard requirements it has for library versions, and make sure that during the compilation step, it links to the right one. If you are a developer, the lesson is that you should either try to keep your software up-to-date with the latest version of all libraries you link to (so that you can just link straight to
libname.so and probably be fine), or figure out what things you depend on, and require linking only to versions of the library that support it.