Skip to content

Commit

Permalink
UPDATE: Updates the article to python 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
rehanhaider committed Oct 6, 2023
1 parent b2b8408 commit f1d9025
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 37 deletions.
116 changes: 79 additions & 37 deletions content/articles/99999980-upgrade-python-on-linux.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,109 @@
Title: Upgrade Python to latest version (3.10) on Ubuntu Linux
Date: 2021-08-29
Title: Upgrade Python to latest version (3.12) on Ubuntu Linux or WSL2
Date: 2023-10-06
Category: Snippets
Tags: python, ubuntu
Tags: python, ubuntu, wsl
Author: Rehan Haider
Summary: A complete guide on how to upgrade Python to the latest version (Python 3.10) on Ubuntu Linux and solve associated issues
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.
Keywords: Linux, Python, Ubuntu, Python 3.10,
Slug: upgrade-python-to-latest-version-on-ubuntu-linux

**Last Updated:** 2022-08-11
**Last Updated:** 2023-10-06

Linux systems come with Python install by default, but, they are usually not the latest. Python also cannot be updated by a typical `apt upgrade` command as well.
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.

To check the version of Python installed on your system run
I am using Ubuntu 22.04 on WSL2, but this should work on any Ubuntu version.

First, check the version of Python installed on your system run
```bash
python3 --version
```
> `python` keyword is used for Python 2.x versions which has been deprecated
In this guide we will

1. Update Python to the latest version
2. Fix pip & other Python related issues
3. While doing the above two, ensure your Ubuntu which is heavily dependent on Python does not break

## 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.

> !!! warning "Python3.10 is not officially available on Ubuntu 20.04, ensure you backup your system before upgrading."

### Step 1: Check if Python3.10 is available for install
### Step 1: Check if Python3.12 is available for install

First, update your system by running

```bash
sudo apt update && sudo apt upgrade -y
```

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

Check if Python 3.10 is available by running
Check if Python 3.12 is available by running

```bash
apt list | grep python3.10
apt list | grep python3.12
```

This will produce the below result, if you see python3.10 it means you can install it
This will produce the below result, if you see python3.12 it means you can install it

![apt list check if python is present]({static}/images/99999980-apt_list.png)

### Step 2: Install Python 3.10
Now you can install Python 3.10 by running
If you see something similar to the above, it means you can install Python 3.12.

### Step 2: Install Python 3.12
Now you can install Python 3.12 by running

```bash
sudo apt install python3.10
sudo apt install python3.12
```

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.

### Step 3: Run Python 3.12

You can run Python 3.12 by running

```bash
python3.12 --version
```

**The right way to run Python 3.12 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
```

and activate it by running

```bash
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 are using bash, run
```bash
echo "alias py=/usr/bin/python3" >> ~/.bashrc
echo "alias python=/usr/bin/python3" >> ~/.bashrc
```


Or, if you have [oh-my-zsh](https://ohmyz.sh/) installed, you can avoid typing out `python3` by running
```bash
echo "alias py=/usr/bin/python3" >> ~/.zshrc
echo "alias python=/usr/bin/python3" >> ~/.zshrc
```
After restarting your terminal, you can run your Python apps with `py` or `python`.

Now though Python 3.10 is installed, if you check the version of your python by running `python3 --version` you will still see an older version. This is because you have two versions of Python installed and you need to choose Python 3.10 as the default.

### Step 3: Set Python 3.10 as default
## WSL2 Only: Set Python 3.12 as default
> !!! warning "Steps beyond here are tested on WSL2, it may break your Ubuntu/Linux desktop."
> !!! warning "Steps beyond here are tested on Ubuntu 20.04 in VM & WSL2, but are experimental , proceed at your own risk."

Changing the default alternatives for Python will break your Gnome terminal. To avoid this, you need to edit the `gnome-terminal` configuration file.

Expand All @@ -71,7 +119,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.10 2
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 2
```

Now run
Expand All @@ -86,10 +134,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.10 is not installed yet.
Installing the new version of Python will break `pip` as the `distutils` for Python3.12 is not installed yet.

### Fix Python3-apt
Running `pip` in terminal will not work, as the current pip is not compatible with Python3.10 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.12 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 <module>
Expand Down Expand Up @@ -120,24 +168,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.10.
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.

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.10/distutils/__init__.py)
(/usr/lib/python3.12/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.10-distutils
sudo apt install python3.12-distutils
```

Now you can install `pip` by running

```bash
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
sudo python3.10 get-pip.py
sudo python3.12 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`
Expand All @@ -151,15 +199,9 @@ Error: Command '['/path/to/env/bin/python3', '-Im', 'ensurepip', '--upgrade', '-

You can fix this by reinstalling venv by running
```bash
sudo apt install python3.10-venv
sudo apt install python3.12-venv
```

All should be done now. It is complicated, but this is how you update Python to latest version.

### Extra
If you have [oh-my-zsh](https://ohmyz.sh/) installed, you can avoid typing out `python3` by running
```bash
echo "alias py=/usr/bin/python3" >> ~/.zshrc
echo "alias python=/usr/bin/python3" >> ~/.zshrc
```
Now you can run your files with `py` or `python`.

Binary file modified content/images/99999980-alternatives.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified content/images/99999980-apt_list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f1d9025

Please sign in to comment.