How to Install|Upgrade Python 3.11 on Ubuntu 22.04|20.04

Posted April 2, 2024
How to Install|Upgrade Python 3.11 on Ubuntu 22.04|20.04

Python has many versions. In this case, you need to install a specific Python3 version 3.11 on Ubuntu. In this guide, you will learn how to install or Upgrade Python 3.11 on Ubuntu 22.04|20.04. Installing Python 3.11 on your Ubuntu machine will add extensive Python3 features to enhance your coding experience.

Now, to get Python 3.11 on Ubuntu 22.04|20.04, you will have different methods. Each method is set to successfully get Ubuntu running Python 3.11. In this guide, you will:

  • Get Python 3.11 on Ubuntu 22.04|20.04 using deadsnakes PPA. Note that deadsnakes PPA is deprecated on Ubuntu 18.04. Therefore, consider other methods discussed here.
  • How to use Python3 source and download Python 3.11 to Ubuntu.
  • How to add pip on top of your Ubuntu Python 3.11.
  • Managing different Python3 versions (for example, Python 3.11 and 3.9) within the same Ubuntu machine using PyEnv.
  • How to uninstall Python 3.11 from Ubuntu.
  • Installing Latest Python3 version using sudo apt install python3.

Ready? Now dive into this guide and How to Install|Upgrade Python 3.11 on Ubuntu 22.04|20.04 like a Pro.

Ubuntu Python 3.11 Requisites

To get Python 3.11 ready on your Ubuntu machine, ensure:

  • You have access to your Ubuntu terminal.
  • Have sudo privileges to run sudo commands.
  • Ensure your Ubuntu machine has the updated Ubuntu package index. Use the following commands:
# Check packages needing an Update
sudo apt update
# Authorize Ubuntu to update and upgrade the package index
sudo apt upgrade

SOL 1: Using PyEnv to Install Python 3.11 on Ubuntu

Python 3.11 is a very specific version. This means you must let Ubuntu know you are specifically installing Python 3.11.

We all know Python3 is already installed on your Ubuntu system. Here is a closer look:

python3 --version

Installing Python 3.11 on Ubuntu using Pyenv

I have Python 3.10 installed. The question is. How do I get Python 3.11 on Ubuntu 22.04|20.04? You can choose to use PyEnv for these reasons:

  • It installs multiple versions of Python side by side on your Ubuntu system.
  • You can switch between Python Versions

This means on top of Python 3.10, PyEnv will install Python 3.11 on Ubuntu and let you switch to the version you want to use and any given time.

Getting Python 3.11 Ready on Ubuntu using PyEnv

Before installing Python 3.11, have the necessary dependencies installed:

sudo apt update
sudo apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev \
    libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \
    libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python3-openssl \
    git
  • Install Pyenv to the Ubuntu system:
curl https://pyenv.run | bash

Installing Python 3.11 on Ubuntu using Pyenv

  • You will need to add pyenv to your ~/.bashrc PATH based on the following:

Installing Python 3.11 on Ubuntu using Pyenv

Make sure you copy your command and run it as such:

export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
  • Now restart your shell:
source ~/.bashrc

Installing Python 3.11 on Ubuntu using Pyenv

Pyenv is ready, now use the following command to install Python 3.11 on Ubuntu:

pyenv install 3.11

Here, Python will install Python 3.11.9 as follows:

Installing Python 3.11 to Ubuntu using Source

To use it, Python 3.11 is installed correctly:

Installing Python 3.11 to Ubuntu using Source

Now, set Python 3.11 as the global version for your Ubuntu system:

pyenv global 3.11.9
pyenv local 3.11.9

Finally, confirm the available Python3 version on Ubuntu:

python3 --version
pip3 -V

Installing Python 3.11 to Ubuntu using Source

Uninstalling Python 3.11 Using Pyenv

If you want to Uninstall Python 3.11, use Pyenv as follows:

Check the ready versions:

pyenv versions

Installing Python 3.11 to Ubuntu using Source

This means you are using Python 3.11.9. Uninstall is as such:

pyenv uninstall 3.11.9

Installing Python 3.11 to Ubuntu using Source

Check if this version is completely removed:

ubuntu@:~$ pyenv versions
pyenv: version `3.11.9' is not installed (set by /home/ubuntu/.python-version)
  system

Sol 2: How to Install Python 3.11 on Ubuntu using Python3 Source

Probably the most secure way to get Python 3.11 on Ubuntu is to Download Python3 binaries from Python and let Ubuntu know you specifically want Python 3.11.

Here you will simply Download and Extract Python 3.11 binaries from Python 3.11 source code. This means you will use the wget command. At the same time, Ubuntu will need you to install Python 3.11 necessary dependencies before running wget.

Any Python3 installation will use dependencies installed on Ubuntu for source code compilation. Use the following command to get ready:

  • Installing Python 3.11 necessary dependencies:
sudo apt update
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev -y

Getting Python 3.11 Ready on Ubuntu using Source

Almost every Ubuntu system has Python3 installed. In this case, let’s confirm so using the following command:

python3 --version

Installing Python 3.11 to Ubuntu using Source

I have Python 3.10 installed. Now, I need to let Ubuntu use wget Source and use Python 3.11 instead. The following command will ensure Python 3.11 is pulled from the Source to the Ubuntu machine:

wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz

Installing Python 3.11 to Ubuntu using Source

Up to this point, you should have a Python-3.11.0.tgz. It contains the Python 3.11 binaries. Make sure you confirm so using the ls command inside the directory you execute the above wget command:

ubuntu@:~$ ls
Python-3.11.0.tgz get-pip.py

Installing Python 3.11 to Ubuntu using Source

You have Python-3.11.0.tgz ready. To run Python, extract this file using the following command:

tar -xvf Python-3.11.0.tgz

A quick ls command should give you the extract file as such:

Getting Python 3.11 on Ubuntu 22.04|20.04

Now, cd to this new file Python-3.11.0

cd Python-3.11.0

It has the following Python 3.11 source code ready:

Getting Python 3.11 on Ubuntu 22.04|20.04

Every Ubuntu system architecture and environment differs. This means you will need to run the --enable-optimizations command in your Python-3.11.0 ./configure path. Do so using the following command and prepare Ubuntu getting ready to use Python 3.11.0:

sudo ./configure --enable-optimizations 

Finally, install and announce Python-3.11.0 to Ubuntu:

sudo make install

Note: The sudo make install command will change the default Python binary path /usr/bin/python. To avoid such use the following command

sudo make altinstall

At the same time, running the sudo make command will take time to compile the Python install.sh build process. First run the following command and ensure the installation process is faster:

sudo make -j $(nproc)

This command should execute the install.sh file and get Ubuntu with Python 3.11. Use the following command to confirm so:

python3.11 --version

Getting Python 3.11 on Ubuntu 22.04|20.04

How to Install Python 3.11 PIP on Ubuntu

Once you have Python 3.11 ready, you need to install pip, the Python package manager. The following guide examines different methods to do so.

How to Install Python pip2|pip3| on Ubuntu 22.04|20.04

However, this guide uses Python 3.11. Therefore, you might consider Installing PIP specifically for Python 3.11 running on your Ubuntu system. Follow this command:

curl -sS https://bootstrap.pypa.io/get-pip.py | python3.11 

Getting Python 3.11 on Ubuntu 22.04|20.04

Confirm if you have the available python3.11 pip package manager:

pip3.11 -V 

Getting Python 3.11 on Ubuntu 22.04|20.04

Conclusion

I hope you now have Installed Python 3.11 on Ubuntu 22.04|20.04. I hope you have learned:

  • Getting Python 3.11 on Ubuntu 22.04|20.04 using deadsnakes PPA. Note that deadsnakes PPA is deprecated on Ubuntu 18.04. Therefore, consider other methods discussed here.
  • How to use Python3 source and download Python 3.11 to Ubuntu.
  • How to add pip on top of your Ubuntu Python 3.11.
  • Managing different Python3 versions (for example Python 3.11 and 3.9) within the same Ubuntu machine using PyEnv.
  • How to uninstall Python 3.11 from Ubuntu.
  • Installing Latest Python3 version using sudo apt install python3.
How to Install|Upgrade Python 3.11 on Ubuntu 22.04|20.04

Written By:

Joseph Chege