4Geeks chosen to deliver AI education in the Bahamas alongside Harvard, Oxford, and Columbia.See more

How to update Python in Terminal?

In our step-by-step guide on how to update Python in terminal, we explore the most straight forward methods to successfully update Python version in Linux, MacOs and Windows systems
5 min readLast updated

Short answer: To update Python in the terminal, the command depends on your operating system. On Linux, run sudo apt update and sudo apt install python3.11, then set the default with sudo update-alternatives --config python3. On macOS, run brew install python. On Windows, download the installer from python.org and switch versions with py -3.11. Verify with python3 --version.

In order to update the Python version in the terminal, you can use these commands depending on your operating system:

bash
#Linux
sudo apt update 
sudo apt install python3.11
 
#MacOS
brew install python
 
# Windows
winget install Python.Python.3.11

In this article we are focusing on updating python using the terminal but there are many other ways on how to update python version.

Update Python on Windows Terminal

To update Python on Windows you should download the Python version you want to upgrade to from python.org. In case you upgraded Python in place (for example, to a new patch release) and want an existing virtual environment to use it, you can run the following command:

cmd
python -m venv --upgrade VIRTUAL-ENVIRONMENT-PATH-HERE

To select which Python version we want to use, it's as simple as writing py -version-you-want-to-use, example:

cmd
	py -3.8 #selects Python version 3.8
	py -3.11 # Selects Python version 3.11

Updating Python on Linux Terminal

Before getting into upgrading Python, we need to know if it's already installed (no use to upgrade something that doesn't exist). To check if Python is installed type the following:

bash
python3 --version

If there's an installed version of Python, the output should show you the installed version like this:

bash
Python 3.11.1

If your computer doesn't have Python installed, we need first to prepare our system to install Python, so we do the following:

bash
sudo apt update 
sudo apt install software-properties-common

Once we have downloaded the necessary files to install Python latest version, we can install it using apt-get (recommended) or using the source code.

Update with Apt-Get

There's no need to reinvent the wheel, Apt-Get is a package manager built in Linux that will make our life easier while installing.

As a first step, let's configure the deadsnakes PPA on the system by running the following command:

bash
sudo add-apt-repository ppa:deadsnakes/ppa

Time to update the Apt cache and install the Python package:

bash
sudo apt update 
sudo apt install python3.9

Python is now updated, but still is pointing to the previously installed version. We need now to update this:

bash
 sudo update-alternatives --config python3

A list of options will be displayed and prompt to pick the Python version you want to point to, select it by writing the number corresponding to the version you want to use.

To verify this selection, write:

bash
python3 --version

Updating Python on MacOs Terminal

Updating Python on MacOs is more straight forward. In MacOs systems you can as well have different Python versions installed at the same time. This makes it simpler to update it by visiting python.org MacOs download page, select the installer to download and run it.

If you are more of a Homebrew user, run the following commands:

bash
brew install python

Once the installation finishes, you'll have the latest Python version installed which you can verify with the following code:

bash
python3 --version

Hope you enjoy the reading and keep on the Geek side, you can check the 4Geek's Blog for more relevant content like how to exit python in terminal.

Updating Python from the terminal is one of many command-line skills developers rely on daily. If you want to build them in a structured way, compare options to learn to code with guided projects and mentor support.

Frequently asked questions

Can I update Python on Windows with winget?

Yes. On Windows systems that include the winget package manager, run winget search Python.Python to see the available packages, then winget install Python.Python.3.11, or winget upgrade Python.Python.3.11 to bring an existing 3.11 installation to its latest patch. Open a new terminal afterwards so the PATH refreshes, and confirm the result with py -3.11 --version.

Why does python3 --version still show the old version after brew install python?

Your shell is probably finding another python3 first, such as the one Apple provides in /usr/bin. Run which -a python3 to list every copy in the order the shell searches them. If the Homebrew version is not first, add the Homebrew bin directory to the beginning of your PATH in your shell profile, open a new terminal and check again.

Is it safe to change the default python3 with update-alternatives?

It is risky on Ubuntu and Debian. System tools such as add-apt-repository are Python scripts built for the distribution's default version, so re-pointing python3 can make them fail. A safer approach is leaving python3 untouched and calling the new interpreter explicitly, for example python3.9 script.py, or using it inside a virtual environment created with python3.9 -m venv .venv.

How do I move a virtual environment to a newer Python version?

Recreate it. The python -m venv --upgrade option only adapts an environment after Python was upgraded in place, typically to a new patch release, and it cannot move it to a new minor version. Save the dependencies with pip freeze > requirements.txt, delete the folder, create it again with python3.11 -m venv .venv and reinstall with pip install -r requirements.txt.