The song was co-written by John Lennon and Paul McCartney. According to Lennon, it was written together but it was "mainly mine". They wrote it after smoking marijuana, something they hadn't done before in a composing session, which from McCartney's perspective (opposite to Lennon's), "got in the way of songwriting". McCartney on the subject:
We smoked a bit of pot, then we wrote out a multicolored lyric sheet, the first time we'd ever done that. We normally didn't smoke when we were working. It got in the way of songwriting because it would just cloud your mind up — "Oh, shit, what are we doing?" It's better to be straight. But we did this multicolor thing.
Musically, the song is based upon a driving rhythm with few chord changes and a simple melody in the key of D major. (The refrain is a 12 bar blues in D. The main chord is D(#9), also used in Drive my Car and Taxman. The two line verse uses D7, C7, F13, and G7.)