Do I need a comma before the conjuction but in the following sentence?
Yes, you need a comma. If two ideas are worded as independent clauses—meaning each can stand on its own as a sentence—they need to be separated by some mark of punctuation. In this case, you could have two separate sentences:
In your example, the two independent clauses are joined by the coordinating conjunction but, which must be preceded by a comma.
Interestingly, you may choose to omit the comma if the clauses are short or closely related. However, it is never incorrect to use a comma in such cases. For example: