Cocaine users consistently develop working memory (WM) impairments but the mediating molecular mechanisms are unknown so far. Recent evidence suggests that the serotonin (5-HT) system is altered by chronic cocaine use, while also being involved in WM processing. Thus, we investigated the effects of genetic variations impacting 5-HT activity and of peripheral 5-HT transporter (5-HTT) mRNA expression on WM performance in cocaine users and stimulant naive controls. Two hundred twenty participants (126 cocaine users, 94 controls) were assessed with visuospatial, spatial, and verbal WM tasks, genotyped for the length polymorphism in the promoter region of the 5-HTT (5-HTTLPR), the variable number of tandem repeats in the second intron of the 5-HTT (VNTR In2), two single-nucleotide polymorphisms (rs4570625 and rs1386497) in the tryptophan hydroxylase-2 (TPH2) gene and quantified for peripheral 5-HTT mRNA expression in whole-blood samples. Several significant gene × environment interactions between 5-HT genotypes and cocaine use on WM emerged: in cocaine users, the long/long (5-HTTLPR), 9+10/9+10 (VNTR In2) and C/C (TPH2 rs1386497) genotypes were risk alleles for WM impairments, whereas in healthy controls these polymorphisms were associated with improved WM performance. Analogously, high 5-HTT mRNA levels were associated with worse executive WM performance in cocaine users but with increased performance in controls. These gene × environment interactions suggest that the 5-HT system has an important role in the development of cognitive deficits in chronic cocaine users. Hence, pharmacological compounds targeting 5-HT neurotransmission might be promising for the treatment of cognitive deficits in cocaine dependence.