The cyclic GMP–AMP synthase (cGAS)–STING pathway is a central component of the cell-autonomous innate immune system in animals. The cGAS protein is a sensor of cytosolic viral DNA and, upon sensing DNA, it produces a cyclic GMP–AMP (cGAMP) signalling molecule that binds to the STING protein and activates the immune response. The production of cGAMP has also been detected in bacteria, and has been shown, in Vibrio cholerae, to activate a phospholipase that degrades the inner bacterial membrane. However, the biological role of cGAMP signalling in bacteria remains unknown. Here we show that cGAMP signalling is part of an antiphage defence system that is common in bacteria. This system is composed of a four-gene operon that encodes the bacterial cGAS and the associated phospholipase, as well as two enzymes with the eukaryotic-like domains E1, E2 and JAB. We show that this operon confers resistance against a wide variety of phages. Phage infection triggers the production of cGAMP, which—in turn—activates the phospholipase, leading to a loss of membrane integrity and to cell death before completion of phage reproduction. Diverged versions of this system appear in more than 10% of prokaryotic genomes, and we show that variants with effectors other than phospholipase also protect against phage infection. Our results suggest that the eukaryotic cGAS–STING antiviral pathway has ancient evolutionary roots that stem from microbial defences against phages.