Answer:
It can be done either by natrually or by vaccination.
Naturally when we encounter with any pathogen first time then our body gain immunity against it (immunity period varies from pathogen type)
Artificially we get immunity by vaccination and i.e either by taking killed or less virulent form of pathogen which is also called first generation vaccine or by taking its DNA which can initiate immunity in us which is also called second generation immunity.