One hallmark of substance use disorder is continued drug use despite negative consequences. When drug-taking behavior is punished with aversive stimuli, i.e. footshock, rats can also be categorized into punishment-resistant or compulsive vs. punishment-sensitive or non-compulsive phenotypes. The serotonin (5-hydroxytryptamine, 5-HT) system modulates responses to both reward and punishment. The goal of the current study was to examine punishment phenotypes in heroin self-administration and to determine the role of dorsal raphe nucleus (DRN) 5-HT neurons in both basal and punished heroin self-administration. First, rats were exposed to punished heroin self-administration and neuronal excitability of DRN 5-HT neuronswas compared between punishment-resistant and punishment-sensitive phenotypes using ex vivo electrophysiology. Second, DRN 5-HT neuronal activity was manipulated in vivo during basal and punished heroin self-administration using chemogenetic tools in a Tph2-iCre rat line. While rats separated into punishment-resistant and punishment-sensitive phenotypes for punished heroin self-administration, DRN 5-HT neuronal excitability did not differ between the phenotypes. While chemogenetic inhibition of DRN 5-HT neurons was without effect, chemogenetic activation of DRN 5-HT neurons increased both basal and punished heroin self-administration selectively in punishment-resistant animals. Additionally, the responsiveness to chemogenetic activation of DRN 5-HT neurons in basal self-administration and motivation for heroin in progressive ratio each predicted resistance to punishment. Therefore, our data support the role for the DRN 5-HT system in compulsive heroin self-administration.