Abstract Providing a sustainable and affordable food supply is one of the most significant challenges facing the global aquaculture industry. Black soldier fly (BSF), Hermetia illucens, meal is a particularly promising alternative to animal-derived protein sources. Following a 90-day feeding experiment with four different diets containing BSF meal with increasing fish meal replacement levels of 25%, 50%, 75%, and 100%, designated as B25, B50, B75, and B100, and a control diet (B0) without BSF meal, the gut microbiota, immune-related gene expression, and Lactococcus petauri resistance of rainbow trout (Oncorhynchus mykiss) were studied. Regardless of diet, rainbow trout intestinal microbiota were different from those in water. In the intestinal mucosa, Fusobacteria, Firmicutes, and Proteobacteria were among the most prevalent taxa. Rainbow trout fed with a BSF-containing diet had a higher bacterial diversity in their gastrointestinal tract than fish fed with a control diet. The abundance of Firmicutes, Actinobacteria, and Proteobacteria increased in tandem with the replacement rate of fish meal with BSF meal. The B75-fed fish were found to be the most resistant group to L. petauri, followed by the groups fed with B100, B50, and B25. Compared to the control group, the expression of cytokines (Tgf, Il-10, Il-1β, , and Il-8) and immune-related genes (IgM, IgT, Mhc-II, and Tlr-5) increased significantly () in all fish groups fed with a BSF-containing diet. Substituting fish meal with BSF meal beyond 50% enhanced the survival rates of fish infected with L. petauri. Furthermore, BSF-containing diets boosted the immune system and cytokine-related genes in fish. Due to improved survival and immunity, rainbow trout may be fed a 50% BSF meal substituted diet without removing chitin or pre-treating BSF meal.