This guide provides step-by-step instructions for updating and installing Python 3.11 and 3.12 on an Ubuntu-based instance.
- An instance running Ubuntu
- User with sudo privileges
Run the following command to update the package lists and upgrade installed packages:
sudo apt update && sudo apt upgrade -yInstall necessary system dependencies:
sudo apt install -y software-properties-commonsudo add-apt-repository -y ppa:deadsnakes/ppa
sudo apt updatesudo apt install -y python3.11 python3.11-venv python3.11-dev
sudo apt install -y python3.12 python3.12-venv python3.12-devSet up alternatives for Python versions:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 2
sudo update-alternatives --config python3Select the desired version from the interactive prompt.
Ensure pip is installed and upgraded:
python3 -m ensurepip --default-pip
python3 -m pip install --upgrade pipReboot to apply changes:
sudo rebootCheck installed versions of Python and pip:
pip3 --version
python3 --version- The
update-alternativescommand allows switching between Python versions easily. - Ensure all dependencies are installed before running Python-based applications.
Abhishek Rajput