Different species of earthworms belong to different ecosystems. The three primary ecosystems of earthworms are leaf litter/compost, topsoil/subsoil and deep soil. The worms that live in these ecosystems are respectively called epigeic, endogeic and anecic.
The physical properties of these ecosystems, especially of the soil, are very important to the life of earthworms. Earthworms are sensitive to changes in the temperature, moisture and pH of the soil. Salt content, texture and aeration also play a role in providing a suitable habitat. Most earthworms prefer to live in neutral or slightly acidic soil, but exceptions do exist.
Because the physical properties of soil are so important, earthworm populations can be devastated when chemicals, insecticides and certain fertilizers are introduced. Some earthworm species have become endangered or protected due to changes in farming practices.
In the larger ecosystem, earthworms are an important part of the food chain. They are consumed by several types of birds, mammals, lizards, insects and other invertebrates. Many parasites also survive by feeding on and living inside earthworms.