It's really hard to define intelligence because it takes so many forms. But the evidence suggests it evolved pretty early, as would make sense. It's helps survival.
The problem with understanding intelligence is that "intelligence" as we define it is inherently based on our perception and experience of the world. A clam's retraction response to touch, for example, is only considered "unintelligent", "primitive", or "basal" because for our purposes, "intelligence" is a standard where we are the baseline by which everything else is measured. By our definitions, "intelligence" manifests as behaviors like complex social structures, solving of specific kinds of (human-designed) puzzles, or self-recognition in mirrors (wow, just like humans do! Funny coincidence, huh). But why? Why should that be what is considered intelligent? Why is a relatively "simple" response to light or touch
not intelligent? We're the only animals who care about this sort of thing, so naturally, we set the standard based on what we experience ourselves. I have to wonder what the clams would think of our definitions if we could communicate with them.
Frustratingly, intelligence is often treated as a quality scale, rather than a measure/tool by which to truly understand another being's experience. "Intelligence" = importance in most facets of pop science, and ultimately research as well. "Intelligent" animals like gorillas, dolphins, or octopuses are treated as more worthy of study than "unintelligent" species.
Regardless of the way it's used, "intelligence" is a relatively unhelpful measure in my opinion. Because, again, what really is "intelligence"? Ultimately, it's a measure of how similar an animal's cognition is to humans. It's not truly meaningless, of course. An animal's similarity to humans is an interesting thing (to us, the humans). But "intelligence" is treated as a set of standards that can apply to everything, as if every animal can have its intelligence measured, and that's just not the case. Because intelligence is standardized on human behaviors, and we are so different from most animals, our baselines simply cannot be applied to most of them. Essentially, a dolphin is only considered more "intelligent" than a worm because a dolphin's behaviors are more similar to ours than a worm's are.