The CMS silicon tracker, consisting of 1440 silicon pixel and 15 148 silicon strip detector modules, has been aligned using more than three million cosmic ray charged particles, with additional information from optical surveys. The positions of the modules were determined with respect to cosmic ray trajectories to an average precision of 3-4 microns RMS in the barrel and 3-14 microns RMS in the endcap in the most sensitive coordinate. The results have been validated by several studies, including laser beam cross-checks, track fit self-consistency, track residuals in overlapping module regions, and track parameter resolution, and are compared with predictions obtained from simulation. Correlated systematic effects have been investigated. The track parameter resolutions obtained with this alignment are close to the design performance.