Magnetic deviation

Magnetic deviation is the error induced in a compass by "local" magnetic fields, which must be allowed for, along with magnetic declination, if accurate bearings are to be calculated. (More loosely, "magnetic deviation" is used by some to mean the same as "magnetic declination". This article is about the former meaning.)
Compass readings.
Compasses are used to determine the direction of true North. However, the compass reading must be corrected for two effects. The first is magnetic declination or variation—the angular difference between "magnetic North" (the local direction of the Earth's magnetic field) and true North. The second is "magnetic deviation"—the angular difference between magnetic North and the compass needle due to nearby sources of interference such as magnetically permeable bodies, or other magnetic fields within the field of influence.
Sources.
In navigation manuals, "magnetic deviation" refers specifically to compass error caused by magnetized iron within a ship or aircraft. This iron has a mixture of permanent magnetization and an "induced" (temporary) magnetization that is induced by the Earth's magnetic field. Because the latter depends on the orientation of the craft relative to the Earth's field, it can be difficult to analyze and correct for it.
The deviation errors caused by magnetism in the ship's structure are minimised by precisely positioning small magnets and iron compensators close to the compass. To compensate for the induced magnetization, two magnetically soft iron spheres are placed on side arms. However, because the magnetic "signature" of every ship changes slowly with location, and with time, it is necessary to adjust the compensating magnets, periodically, to keep the deviation errors to a practical minimum. Magnetic compass adjustment and correction is one of the subjects in the examination curriculum for a shipmaster's certificate of competency.
The sources of magnetic deviation vary from compass to compass or vehicle to vehicle. However, they are independent of location, and thus the compass can be calibrated to accommodate them.
Non-magnetic methods of taking bearings, such as with gyrocompass, astronomical observations, satellites (as GPS) or radio navigation, are not subject to magnetic deviation. Thus, a comparison of bearings taken with such methods with the bearing given by a compass can be used to compute local magnetic deviation.
History.
Sailing ships generally had two kinds of compasses: "steering compasses", two of which would be mounted in a binnacle in front of the helm for use in maintaining a course; and a "bearing compass" that was used for taking the bearings of celestial objects, landmarks and the ship's wake. The latter could be moved around the ship, and it was soon observed that the bearing could vary from one part of the ship to another. The explorer Joao de Castro was the first to report such an inconsistency, in 1538, and attributed it to the ship's gun. Many other objects were found to be sources of deviation in ships, including iron particles in brass compass bowls; iron nails in a wooden compass box or binnacle; and metal parts of clothing. The two steering compasses themselves could interfere with each other if they were set too close together. The "bearing compass" was eventually sited in a fixed position in a binnacle with, as far as possible, an all round view and acquired the name "standard compass". It would nonetheless have a different deviation from the "steering compass", so the compass heading shown on the "steering compass" would be different from the compass heading shown on the "standard compass".
The source of deviation could not always be identified. To reduce this source of error, which was due to induced magnetization in the ship, the surveyor John Churchman proposed a solution known as "swinging the ship" in 1794. This involved measuring the magnetic deviation as the ship was oriented in several compass directions. These measurements could then be used to correct compass readings. This procedure became standard practice in the 19th century as iron became an increasing component of ships.
Once the compass has been corrected using small magnets fitted in the base and with soft iron balls any residual deviation is recorded as a table or graph: the compass correction card, which is kept on board. 
Archibald Smith in 1862 published Admiralty Manual for ascertaining and applying the Deviations of the Compass caused by the Iron in a Ship. The key insight is that the deviation can be written as a Fourier series in the magnetic heading with terms up to the second frequency components. This means that only five numbers are required to be estimated to determine the full deviation card. This method is still used by professional compass correctors who are employed to correct the compass and produce a deviation card.