A backyard is not necessary. In fact, a yard can lead to behavior problems.
It is often thought that a yard is necessary to give a dog exercise and an area where he can run and play. If you have this option, it is of course a nice bonus. It can only lead to problems if the yard is used as a dog sitter or a replacement for walking the dog.
A dog needs both physical and mental challenges. A large backyard offers him physical challenge, but the mental challenges are hard to find. If you take your dog for a walk, he will discover all kinds of new scents. He smells new dogs that walked or peed here. He sees different people and dogs and he hears new sounds. He is stimulated mentally and will therefore be tired and satisfied. There are almost no new scents and stimuli in his own backyard.
In addition, a dog can quickly develop unwanted behavior in the yard, including barking, digging, demolishing and possibly aggression problems. If you are not there, there is no training. A dog then teaches itself the unwanted behavior.
Due to a shortage of mental challenges your dog could start barking at passers-by or other stimuli around the backyard. Since this is self-rewarding behavior there is a high probability that it will increase and can eventually lead to very strong territorial behavior towards people, dogs, cars, etc.
The same shortage of challenges and supervision can also cause digging holes in the garden, demolition or eating (poisonous) plants or garden furniture. If the dog has not (properly) learned to be alone, he can bark or howl due to separation anxiety. This can be a major annoyance for your neighbors.
Even with a puppy in the house it can seem ideal in the summer to continuously leave the door open, so that a puppy can pee and play in the yard. However, supervision is very important as long as a puppy (and adult dog) has not yet learned what desirable behavior is. Unwanted behavior is quickly learned without supervision. When a dog learns what to do, we can give him more and more freedom because the chance of unwanted behavior is reduced.
So to come back to the question, should a dog have a yard? A backyard can be a great addition. Do not consider it a substitute for a walk with the dog. If your dog is in the yard, make sure to have fun playing, training or just being together while you do something for yourself. Dogs are social creatures and like to be in our company. Also consider brain work to give your dog these mental challenges.