Although most quotes that you can think of tend to come from Shakespeare or the Bible, it turns out that this line is not from any Shakespeare work at all. You can use any of a number of Shakespeare sites where you can search all of his works and that line is not found.
Where this line actually does come from is the Rubaiyat, written by the poet Omar Khayyam. Omar Khayyam was a poet from what is now Iran but was then called Persia. Besides being a poet, he was a mathematician and scientist. The line you cite comes from Poem #545 in the Rubaiyat.