Introduction: Acute lung injury (ALI) is a devastating pulmonary illness with diffuse inflammatory responses. Hydromorphone (Hyd) is an opioid agonist used for relieving moderate-to-severe pain. The present work investigated the effect of Hyd on cardiopulmonary bypass (CPB)-induced ALI by regulating pyroptosis of alveolar macrophages (AMs). Methods: Rats were subjected to CPB, followed by Hyd treatment. The lung injury in rat lung tissues was appraised by the ratio of lung wet/dry weight (weight), histological staining, and the total protein concentrations in bronchoalveolar lavage fluid, and lung function was assessed by oxygenation index and respiratory index, and lung macrophage pyroptosis was observed by fluorescence staining. Alveolar macrophages were separated and pyroptosis was determined by western blot assay and enzyme-linked immunosorbent assay. The expression patterns of nuclear factor erythroid 2-related factor 2/heme oxygenase 1 (Nrf2/HO-1), nod-like receptor protein 3 (NLRP3), N-terminal gasdermin-D, and cleaved caspase-1 were examined by real-time quantitative polymerase chain reaction, western blot, and immunohistochemistry assays. The impact of NLRP3 or Nrf2 on pyroptosis of AMs and CPB-induced ALI was observed after treatment of nigericin (NLRP3 agonist) or ML385 (Nrf2 inhibitor). Results: Hyd attenuated CPB-induced lung injury as manifested by reductions in lung inflammation and edema, the scores of lung injury, the ratio of lung wet/dry weight, and the total protein concentrations in bronchoalveolar lavage fluid. Besides, Hyd repressed NLRP3 inflammasome-mediated pyroptosis of AMs after CPB treatment. Hyd upregulated Nrf2/HO-1 expression levels to repress NLRP3 inflammasome-mediated pyroptosis. Treatment of nigericin or ML385 counteracted the role of Hyd in ameliorating pyroptosis of AMs and CPB-induced ALI. Conclusions: Hyd alleviated NLRP3 inflammasome-mediated pyroptosis and CPB-induced ALI via upregulating the Nrf2/HO-1 pathway, which may be achieved by AMs.