Mac Miller died Friday of an apparent overdose ... TMZ has learned.

Law enforcement sources tell TMZ ... Mac was found Friday around noon in a bedroom at his San Fernando Valley home, and he was pronounced dead at the scene. 

CORONER LEAVING MAC'S HOUSE
TMZ.com

We're told authorities were alerted by a male friend who placed a 911 call from Mac's home. The emergency dispatch audio reveals first responders were called for a patient in cardiac arrest.

And there's this ... in a doc made a few years ago, Mac said, "I'd rather be the corny white rapper than the drugged-out mess who can't even get out of his house. Overdosing is just not cool. You don't go down in history because you overdose. You just die."

Miller has battled substance abuse issues for years ... something that came up again in the wake of his breakup with Ariana Grande. The rapper wrecked his G-Wagon by wrapping it around a utility pole back in May and was arrested for DUI and hit and run. He fled the scene but cops say he later confessed at his home. He blew 2 times the legal limit.

After his arrest Ariana tweeted, "Pls take care of yourself" and was clearly concerned about substance abuse.

After their breakup, Ariana made it clear ... his substance abuse had been a deal breaker in their 2-year relationship.