You can legally defend yourself with a gun if the situation includes these 3 elements
While specific laws vary, it is generally accepted in every state in the U.S. that you have a right to defend yourself with lethal force. Whether you exercise this right legally is based on the "reasonable man" doctrine. In other words, would a reasonable person in the same situation be likely to use deadly force in self defense?
What is considered reasonable? Self defense expert Massad Ayoob says this: "Deadly force is justified only when undertaken to prevent imminent and otherwise unavoidable danger of death or grave bodily harm to the innocent."
To make this easier to understand, most people look for three elements to determine whether the use of deadly force, such as a firearm, is justified.
Did your attacker have the ability to cause death or grievous bodily harm? This usually means, did the attacker have a lethal weapon, such as a gun or knife.
Use of lethal force against an unarmed attacker may also be justified, such as when you are faced with multiple attackers or a single attacker who is causing you serious harm. However, as in the famous Trayvon Martin case, this can lead to a claim of "disparity of force" and make your defense more difficult.
Was your attacker close enough to carry out the attack? If the attacker was unarmed, he would have to be within arms length. If he had a weapon, he would have to be close enough to use the weapon against you. How close is close enough? It depends on the weapon, the circumstances, and what the jury or judge thinks about it.
Opportunity also means the attack must be here and now. Thinking that someone may harm you at a future date or at another place is not a legally acceptable justification for using lethal force.
Did the attacker intend to cause you harm? Was your life in jeopardy? Someone can have a gun and be standing right in front of you, such as a concealed weapons permit holder, but have no intention of causing harm. On the other hand, someone can run at you with a baseball bat screaming that they're going to kill you. In the first, there is probably no jeopardy. In the second, there probably is.
What it boils down do is this: Do you genuinely believe you are in mortal danger and is using a gun the only means of ending the threat? Assuming you don't initiate the conflict, you don't have a duty to retreat, and you honestly believe you are in grave danger, your use of a firearm may be justified.
Of course, it's always possible that police, a prosecutor, a judge, or a jury will disagree with you, in which case you can find yourself in serious legal trouble.