Some folks like mimat get a bit pedantic. "Viruses are not life because we've defined life in such a way that viruses are not alive". Circular reasoning much?
The ocean contain something on the order of 10-15 viral particles for every bacteria. Viruses are clearly an integral, if less-understood, part of modern ecosystems. The question of where they came from, and how they relate to the other domains of life (bacteria, archaea, eukarya) is an interesting one to explore, if unsatisfying because we don't have a lot in the way of answers.
Why do humans exist? From our genes' perspective, we exist as replicative machines whose primary function is to make more of our genes, enabling them to exist in perpetuity. It's the same with viruses. They exist because the mechanism of viral replication is effective at making more bits of viral genetic information.
Asking 'why' something arose in the first place isn't a very useful way of phrasing. Mutation in evolution doesn't occur with foresight, instead it's the random element of evolution. (Natural and Sexual Selection being totally non-random.) Asking 'how' is a better question.
Thinking about viruses not being alive because they lack the ability to make everything they need for replication. This is more than a bit pedantic: are then humans not alive because we lack the ability to make some thing we need (vitamins)? Viral capsules are machines built by genetic sequences that allow those sequences to replicate in perpetuity - same as us. I certainly think they're a form of life, and like almost all other life, they are dependent on existing in an ecosystem with other forms of life.
As to where life came from, or a particular type like viruses, very interesting question. LUCA, or the last universal common ancestor, has been inferred to have been a protocell using something like methanogenesis or acetogenesis, had a cell membrane, and either DNA or RNA for storing and replicating genetic information. Going earlier in time to the RNA world gets interesting.
Think about a given genetic sequence. While any given instance is transient and fragile, there may exist billions of copies of it at any given time on earth. An exact copy of that sequence may have existed for hundreds of thousands of years. Inexact copies for millions, and if you are generous and think of gene families, genes may have lifetimes measured in billions of years.
Microbes are far more 'promiscuous' with their sharing of genetic information than we usually think about, as multicellular organisms. You could think of microbes as systems that are very flexible, while multicellular systems have traded that adaptability for in-system optimization. That is, to us viruses make us sick, but to a bacteria a plasmid or virus might be costly, but potentially advantageous if it brings with it new, beneficial genetic information.
Viruses could be thought of as a super-spore. A spore is a dormant, but very tough capsule that allows its parent genetic sequences to survive hostile times, and then resume metabolism and replication when better times or environments come. To a virus, every environment is a hostile one except the insides of their specific target cell. In a way, you could think of a virus as 'sleeping' or dormant until it enters a host cell, and only truly active or alive when it is. Think about the actual genetic information as the 'alive' entity, and anything constructed by it as a machine.
Viruses probably predate LUCA.
Time to get super-super-speculative.
Among many ideas as to how life arose, in particular cell membranes, one hypothesis is that early cell membranes arose abiotically. According to this notion, life only gradually gained the ability to modify and eventually synthesize membranes. The original membranes may not have been composed of modern stuff at all, as long as it performed the essential functions of being a selectively permeable barrier. Early oceanic vents provide a place where protomembrane molecules could have been abiotically generated and condense on porous rocks.
Likewise, there is a question as to what came first, or if this is a good question: metabolism or genetic information replication. How do you have one without the other? Well, again, early oceanic vents seem a good candidate for abiotic metabolism to have begun building precursor molecules. Get a precursor molecule with the ability to make something like it, and voila: replicators! Personally, I think the order is this: replicators first and gradual control of metabolism and cell membranes later. You could almost think of metabolic machinery as simply being the means of recreating the conditions that existed in those early vents, or the porous rocks around them: conditions that enabled replicating machinery to replicate.
Who knows if this concept is true! But if something like this is how life got started, then at some point before luca, the original replicator more closely resembled a virus as it exists inside a cell, than it resembles modern cells. If replicators in their early days lacked much control over metabolism and/or membranes, that kind of sounds like viruses. Before early life 'took off' in its modern cellular state, early genes needed to connect with other genes for replication, but not necessarily for distribution. Maybe this took the form of physical connection via protochromosomes. Perhaps branches of genetic life that became many viruses opted for an alternate strategy.
Multiple genes that are located on the same chromosome (circular or linear, doesn't matter) have opted to link their evolutionary futures as closely as possible. They are literally, physically linked. The link may not be physical: our genes, even if located on different chromosomes, still have linked fates, marred only by recombination and sex. Remember that microbes are much more fluid and genetically flexible. From a gene's perspective, a cell is simply a location with other friendly genes, which which the 1st gene shares an evolutionary trajectory. If the 1st gene happens to be on a plasmid, however, its fate might be more independent, as copies get swapped into other cells. Microbes have many ways of swapping genetic information. To the extent that this information can be swapped around, the evolutionary future of those swapped bits is less dependent on the exact nature of the other, more cellular bits. Viral genes have opted for an even more independent evolutionary trajectory than plasmids and similar structures. I mean, really, what eats a virus? They build themselves protective capsules and are only really vulnerable when they are inside a host cell and actively replicating. If I were a genetic sequence with billions of physical instances at any given moment, and potential information lifespan in the billions of years, that honestly doesn't sound like a bad way to replicate.