It's often attributed to Twain, but several quote websites say it was either William Greener or Rep. Charles Brownson in 1964.
The Mark Twain Center says he didn't say it.
https://english.stackexchange.com/questions/67527/origin-of-the-expression-never-argue-with-someone-who-buys-ink-by-the-barrel