How to install specific Ubuntu packages, with exact version?

You can use apt-get to install a specific version of the package a long as it is in an archive that apt knows about. From the apt-get manpage:

A specific version of a package can be selected for installation by following the package name with an equals and the version of the package to select. This will cause that version to be located and selected for install. Alternatively a specific distribution can be selected by following the package name with a slash and the version of the distribution or the Archive name (stable, frozen, unstable).

For example, you could do:

sudo apt-get install apache2=2.2.20-1ubuntu1

Note that you may need to do some dependency resolution on your own in this case, but if there are any problems apt-get will tell you what is causing them. On my 11.10 system I would need to do the following to get this to work:

sudo apt-get install apache2=2.2.20-1ubuntu1 \
                     apache2.2-common=2.2.20-1ubuntu1 \
                     apache2.2-bin=2.2.20-1ubuntu1 \
                     apache2-mpm-worker=2.2.20-1ubuntu1

You can display available package versions as follows:

sudo apt list -a apache2

To check which versions are available, you can check via:

sudo apt-cache madison ^apache2

If won't work, consider running sudo apt-get update before to update the package list.

Then copy the version or use the following syntax:

sudo apt-get install apache2=2.2.\*

To check which version you've installed, run:

dpkg -l 'apache2*' | grep ^i

If the version info is truncated, try:

COLUMNS=100 dpkg -l <packageName>

I'll expand on earlier answers with other handy versioning commands in the apt family. To see which versions are available, run apt-cache policy:

# apt-cache policy apache2
apache2:
  Installed: (none)
  Candidate: 2.4.7-1ubuntu4.5
  Version table:
     2.4.10-1ubuntu1.1~ubuntu14.04.1 0
        100 http://us.archive.ubuntu.com/ubuntu/ trusty-backports/main amd64 Packages
     2.4.7-1ubuntu4.5 0
        500 http://security.ubuntu.com/ubuntu/ trusty-security/main amd64 Packages
     2.4.7-1ubuntu4 0
        500 http://us.archive.ubuntu.com/ubuntu/ trusty/main amd64 Packages

Then, as mentioned elsewhere, install a specific version with apt-get:

# apt-get install apache2=2.4.7-1ubuntu4.5
...

You can now see which version you have installed by running apt-cache policy again:

# apt-cache policy apache2
apache2:
  Installed: 2.4.7-1ubuntu4.5
  Candidate: 2.4.7-1ubuntu4.5
  Version table:
     2.4.10-1ubuntu1.1~ubuntu14.04.1 0
        100 http://us.archive.ubuntu.com/ubuntu/ trusty-backports/main amd64 Packages
 *** 2.4.7-1ubuntu4.5 0
        500 http://security.ubuntu.com/ubuntu/ trusty-security/main amd64 Packages
        100 /var/lib/dpkg/status
     2.4.7-1ubuntu4 0
        500 http://us.archive.ubuntu.com/ubuntu/ trusty/main amd64 Packages

If you don't want newer versions to be installed on updates, pin the package with apt-mark:

# apt-mark hold apache2
apache2 set on hold.

Let's say a new version of apache2 is added to the package index and your machine is synced with apt-get update. You'll see this when you next run apt-get upgrade:

# apt-get upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
The following packages have been kept back:
  apache2
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.