diff --git a/content/articles/99999980-upgrade-python-on-linux.md b/content/articles/99999980-upgrade-python-on-linux.md index f839fc7..9f7a050 100644 --- a/content/articles/99999980-upgrade-python-on-linux.md +++ b/content/articles/99999980-upgrade-python-on-linux.md @@ -1,15 +1,15 @@ -Title: Upgrade Python to latest version (3.12) on Ubuntu Linux or WSL2 +Title: Upgrade Python to latest version (3.13) on Ubuntu Linux or WSL2 Date: 2024-06-06 Category: Snippets Tags: python, ubuntu, wsl Author: Rehan Haider -Summary: A complete guide on how to upgrade Python to the latest version (Python 3.12) on Ubuntu Linux and solve associated issues. Also works on WSL2. +Summary: A complete guide on how to upgrade Python to the latest version (Python 3.13) on Ubuntu Linux and solve associated issues. Also works on WSL2. Keywords: Linux, Python, Ubuntu, Python 3.10, Slug: upgrade-python-to-latest-version-on-ubuntu-linux -**Last Updated:** 2023-10-06 +**Last Updated:** 2025-01-06 -Ubuntu both Desktop & WSL2 Linux systems come with Python installed by default, but, they are usually not the latest. This is a short guide on how to upgrade your Python to the latest version (Python 3.12) on Ubuntu Linux and solve associated issues. +Ubuntu both Desktop & WSL2 Linux systems come with Python installed by default, but, they are usually not the latest. This is a short guide on how to upgrade your Python to the latest version (Python 3.13) on Ubuntu Linux and solve associated issues. I am using Ubuntu 22.04 on WSL2, but this should work on any Ubuntu version. @@ -24,8 +24,7 @@ python3 --version ## Updating Python to the latest version Ubuntu's default repositories do not contain the latest version of Python, but an open source repository named `deadsnakes` does. - -### Step 1: Check if Python3.12 is available for install +### Step 1: Check if Python3.13 is available for install First, update your system by running @@ -38,41 +37,41 @@ sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update ``` -Check if Python 3.12 is available by running +Check if Python 3.13 is available by running ```bash -apt list | grep python3.12 +apt list | grep python3.13 ``` -This will produce the below result, if you see python3.12 it means you can install it +This will produce the below result, if you see python3.13 it means you can install it ![apt list check if python is present]({static}/images/99999980-apt_list.png) -If you see something similar to the above, it means you can install Python 3.12. +If you see something similar to the above, it means you can install Python 3.13. -### Step 2: Install Python 3.12 -Now you can install Python 3.12 by running +### Step 2: Install Python 3.13 +Now you can install Python 3.13 by running ```bash -sudo apt install python3.12 +sudo apt install python3.13 ``` -Now though Python 3.12 is installed, if you check the version of your python by running `python3 --version` you will still see an older version. This is because if you are using Ubuntu Desktop, the default Python is needed by the system and changing it will break your system. +Now though Python 3.13 is installed, if you check the version of your python by running `python3 --version` you will still see an older version. This is because if you are using Ubuntu Desktop, the default Python is needed by the system and changing it will break your system. -### Step 3: Run Python 3.12 +### Step 3: Run Python 3.13 -You can run Python 3.12 by running +You can run Python 3.13 by running ```bash -python3.12 --version +python3.13 --version ``` -**The right way to run Python 3.12 On Linux Desktops is by using a virtual environment.** +**The right way to run Python 3.13 On Linux Desktops is by using a virtual environment.** E.g. you can create a new virtual environment by running ```bash -python3.12 -m venv env +python3.13 -m venv env ``` and activate it by running @@ -84,7 +83,7 @@ source env/bin/activate Now you can run `python --version` and you should see the latest version of Python as the output. ### Extra -If you really, really, really don't want to type `python3.12` every time you want to run a file, you can create an alias. +If you really, really, really don't want to type `python3.13` every time you want to run a file, you can create an alias. If you are using bash, run ```bash @@ -101,7 +100,7 @@ echo "alias python=/usr/bin/python3" >> ~/.zshrc After restarting your terminal, you can run your Python apps with `py` or `python`. -## WSL2 Only: Set Python 3.12 as default +## WSL2 Only: Set Python 3.13 as default > !!! warning "Steps beyond here are tested on WSL2, it may break your Ubuntu/Linux desktop." @@ -119,7 +118,7 @@ Then save and close the file. Next, update the default Python by adding both versions to an alternatives by running the below ```bash sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1 -sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 2 +sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.13 2 ``` Now run @@ -134,10 +133,10 @@ Now run `python3 --version` again and you should see the latest Python as the ou ## Fix pip and disutils errors -Installing the new version of Python will break `pip` as the `distutils` for Python3.12 is not installed yet. +Installing the new version of Python will break `pip` as the `distutils` for Python3.13 is not installed yet. ### Fix Python3-apt -Running `pip` in terminal will not work, as the current pip is not compatible with Python3.12 and python3-apt will be broken, that will generate an error like +Running `pip` in terminal will not work, as the current pip is not compatible with Python3.13 and python3-apt will be broken, that will generate an error like ```text Traceback (most recent call last): File "/usr/lib/command-not-found", line 28, in @@ -168,24 +167,24 @@ sudo apt install python3-apt ### Install pip & distutils -Running `pip` will still throw an error `pip: command not found`. We need to install the latest version of pip compatible with Python 3.12. +Running `pip` will still throw an error `pip: command not found`. We need to install the latest version of pip compatible with Python 3.13. Also, if try to manually install the latest version of pip, it will throw an error like ```text ImportError: cannot import name 'sysconfig' from 'distutils' -(/usr/lib/python3.12/distutils/__init__.py) +(/usr/lib/python3.13/distutils/__init__.py) ``` Or you might also see an error stating `No module named 'distutils.util'`. This is because the `distutils` module is not installed yet, to install run the below command ```bash -sudo apt install python3.12-distutils +sudo apt install python3.13-distutils ``` Now you can install `pip` by running ```bash curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py -sudo python3.12 get-pip.py +sudo python3.13 get-pip.py ``` > If you get an error like `bash: curl: command not found` then you need to install curl first by running `sudo apt install curl` @@ -199,7 +198,7 @@ Error: Command '['/path/to/env/bin/python3', '-Im', 'ensurepip', '--upgrade', '- You can fix this by reinstalling venv by running ```bash -sudo apt install python3.12-venv +sudo apt install python3.13-venv ``` All should be done now. It is complicated, but this is how you update Python to latest version. diff --git a/content/images/99999980-apt_list.png b/content/images/99999980-apt_list.png index 1f5c21d..6f145ee 100644 Binary files a/content/images/99999980-apt_list.png and b/content/images/99999980-apt_list.png differ