Answer in Details
Lord Shiva is known as the most compassionate and easily pleased deity—Bholenath. He accepts sincere prayers from anyone, regardless of circumstance. If you are unable to avoid onion and garlic due to your hostel situation but maintain devotion and purity of mind, you are still eligible to chant Shiva mantras and perform your spiritual practices.
You can pray with a pure heart and ask Lord Shiva for guidance and forgiveness if you feel you are not able to follow certain dietary restrictions. The most important thing is your intention (bhav) and love for Lord Shiva.
So, you can chant Shiva mantras. Do so with devotion and sincerity—Lord Shiva will surely accept your prayers.