Upgrade Python Ubuntu [extra Quality] -
# Create a virtual environment with your new Python python3.12 -m venv myproject_env source myproject_env/bin/activate Now python/pip point to the venv's version python --version pip --version 6. Update pip and Set Up Aliases # Upgrade pip for your new Python python3.12 -m pip install --upgrade pip Optional: Add convenient alias to ~/.bashrc alias python3='python3.12' alias pip3='python3.12 -m pip' 7. Common Issues & Fixes | Problem | Solution | |---------|----------| | ModuleNotFoundError: No module named 'distutils' | sudo apt install python3.12-distutils | | pip not found | sudo apt install python3.12-pip or python3.12 -m ensurepip | | venv not available | sudo apt install python3.12-venv | | Broken APT after changing system python3 link | Revert: sudo update-alternatives --config python3 → choose original version | 8. Quick Summary – Recommended Workflow # 1. Add deadsnakes PPA sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update 2. Install Python 3.12 (or newer) sudo apt install python3.12 python3.12-venv python3.12-dev 3. Keep system Python untouched python3 --version # still original system version 4. Use new Python for projects python3.12 -m venv ~/projects/my_env source ~/projects/my_env/bin/activate 9. Uninstalling a Custom Python Version # If installed via deadsnakes sudo apt remove python3.12 If installed via pyenv pyenv uninstall 3.12.2 Never run: sudo apt remove python3 (that's the system Python!) Final advice: Always use virtual environments, never replace /usr/bin/python3 , and prefer pyenv or deadsnakes PPA for installing newer Python versions on Ubuntu.
Instead, install additional Python versions alongside the system one and manage them using alternatives or virtual environments. # Check system Python (don't remove this!) python3 --version Check all installed Python versions ls /usr/bin/python* 3. Install a Newer Python Version (Ubuntu 20.04+) Method A: Using deadsnakes PPA (Recommended for latest releases) # Add the PPA sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update Install Python 3.12 (example) sudo apt install python3.12 python3.12-venv python3.12-dev Install other versions as needed: python3.11, python3.13, etc. Method B: Using apt (for slightly older versions in default repos) # Check available versions apt list -a python3 Install from official repo (usually not the latest) sudo apt install python3.11 4. Manage Multiple Python Versions Using update-alternatives # Register versions sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1 sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 2 Switch between versions sudo update-alternatives --config python3 Using pyenv (Best for per-user, flexible control) # Install pyenv dependencies sudo apt update sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \ libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils \ tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev Install pyenv curl https://pyenv.run | bash Add to ~/.bashrc (or ~/.zshrc) echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(pyenv init -)"' >> ~/.bashrc source ~/.bashrc Install and use a new Python pyenv install 3.12.2 pyenv global 3.12.2 # sets for your user only 5. Use Virtual Environments (Best Practice) Never rely on the global Python for project work. upgrade python ubuntu
1. Understand the Risk – Don't Replace the System Python ⚠️ Critical Warning: Ubuntu's operating system tools (APT, Software Center, etc.) depend on the default system Python (e.g., /usr/bin/python3 ). Never uninstall or overwrite it. # Create a virtual environment with your new Python python3