Either small leaks or changes in atmospheric pressure. However [if the bubble is less than 1/4 inch across it won't affect accuracy][1].

>**Bubbles can form from high elevation or exposure to cold temperatures, which cause the liquid to contract, leaving an air bubble behind. Usually, the bubble will disappear when the compass is returned to sea level and/or room temperature.** If it doesn't, place it in a warm spot-like a sunny windowsill-so the liquid can heat, expand, and return to it's normal volume.

[Compass Bubbles][2]

>A compass is usually filled with water or compass oil, which is a mixture of water and mineral spirits. Bubbles usually emerge in compasses as a result of drastic atmospheric pressure changes or a slight leak in the housing material.

[Source][3]

>Generally, magnetic compasses are designed to not have a bubble; if you see a bubble, the chances of a leak is high. This may seem unlikely to you, because you may not have noticed any fluid escaping from the compass. 

>...

>Your compass has most likely developed a very small leak, which has allowed some fluid to escape. When you observe the bubble in the morning, your compass is cool and the missing fluid has caused a bubble to form. As the temperature rises, the fluid expands, thereby taking up volume and making the bubble disappear. 

[Source][4]

There are a number of patents on compasses with efforts to solve this problem.

>A method of preventing air bubbles in a compass equipped with fluid expansion capability, **wherein a compass unit is assembled with a fluid expansion diaphragm; the compass unit is filled with fluid expanding the diaphragm, and sealed while maintaining the diaphragm in its expanded state for preventing air bubbles in reduced pressure and/or reduced temperature conditions,** without significantly changing the basic structure of the compass.

>...

>Description

>FIELD OF INVENTION

>This invention relates to an air bubble free compass.

>BACKGROUND OF INVENTION

>Some compasses are designed with a housing which includes a neoprene expansion diaphragm that allows for expansion of the damping oil when the compass is subjected to heat which can occur when the compass is in the sun on the bridge of a ship. The diaphragm expands and contracts maintaining proper fluid pressure in the compass unit.

[Air bubble free compass and method of manufacturing same][5]

>A compass housing capable of withstanding high external pressure such as encountered in deep submergence applications. The housing includes a pair of hemispherical portions interconnected to form a sealed enclosure and at least one portion being formed of a resilient material capable of expansion and contraction in the presence of varying differential pressure conditions. The housing is completely filled with a damping liquid under pressure to provide an internal pressure higher than atmospheric pressure such that in the presence of higher external pressures, the inner and outer pressures tend to equalize, thereby minimizing the differential pressure on the housing itself.

[High pressure compass housing][6]

>According to the invention, the rst-mentioned object is realized by the liquid being enclosed within a chamber formed of a cover portion of transparent material and -a. thin resilient bottom portion secured thereto, this bottom portion being adapted to adjust itself to the changes of volume of the enclosed liquid caused by variations in temperature.

>The latter object is fullled, according to `the invention, by the compass needle being arranged on a pin Whichis carried by an intermediate partition or bridge, a frame or the like arranged between the transparent cover and the resilient bottom, the said partition or the like being provided with openings and being so arranged that the distance of the needle from -.the cover remains substantially consta-nt in spite of the move-v ments on the resilient bottom.

[Fluid compass][7]


  [1]: https://outdoors.stackexchange.com/a/15045/8794
  [2]: https://www.backpacker.com/gear/compass-bubbles
  [3]: https://goneoutdoors.com/bubble-out-compass-8568883.html
  [4]: http://www.mrs-marine.com.my/index.php/technical-info/magnetic-compass-frequently-asked-questions
  [5]: https://patents.google.com/patent/US5469631
  [6]: https://patents.google.com/patent/US4004348
  [7]: https://patents.google.com/patent/US2136970