Yes, you can take magnesium and vitamin D together, and there is a good reason they are often sold as a pair: magnesium is one of the things your body uses to turn vitamin D into its active form. They work with each other rather than against each other.
Why they belong together
Vitamin D does not arrive in your bloodstream ready to use. Your liver and kidneys convert it through a couple of steps into the active form, and the enzymes that do that conversion depend on magnesium. If your magnesium is low, vitamin D can be slower to do its job, which is part of why some people taking vitamin D on its own see less change than they expected. Taking the two together covers that base.
How to take them
Vitamin D is fat-soluble, so take it with a meal that has some fat in it, which improves how much you absorb. Magnesium is flexible on timing, and many people take the gentler glycinate form in the evening because it is mildly calming and supports sleep. Taking them at the same time is fine; there is no competition between them.
A note on dose
The right vitamin D dose depends on how low you are starting from, which is one of the few things genuinely worth a blood test. Magnesium has a wide safety margin, though very high doses can loosen the stool, especially in the cheaper oxide form. Choosing the right form of each matters more than chasing a big number on the label, and the D2-versus-D3 point applies to the vitamin D half.
To check this pair, or either one, against any medication you take, the free checker will show you the result and the reasoning.