Python is one of the most popular and fastest-growing object-oriented programming languages. Python is mostly used for building websites, applications for mobile, web and desktop. It is also the programming language of choice for automating tasks and conducting data analysis.
At the time of writing this article (December 2022) there are two main Python versions being used; Python v2 and v3. Whilst Python 2 will see no major releases, the v3 is under active development and has already seen a lot of stable releases. Currently, the latest stable version is 3.11 which was released on Oct. 24, 2022.
In this article we will demonstrate how to install the latest Python 3.11 version on an Ubuntu server via the apt package manager, and also how to set the default Python version in case you have multiple versions installed on your server.
Install Python 3.11 on Ubuntu using apt
In this tutorial we will be using the “deadsnakes” repository as it contains the more recent Python versions ready for Ubuntu.
Pre-requisites
- root access (or any sudo level user account)
- Ubuntu server (in this tutorial we are using Ubuntu Server 22.10)
Easy Steps to Install Python 3.11 on Ubuntu
Login to your Ubuntu server and type the following commands:
root@ubuntu-server:~$ sudo add-apt-repository ppa:deadsnakes/ppa
root@ubuntu-server:~$ sudo apt update
root@ubuntu-server:~$ sudo apt install python3.11
By adding the repository ppa:deadsnakes/ppa allows us to install other versions as well. See below the commands for installing old python versions.
root@ubuntu-server:~$ sudo apt install python3.11
root@ubuntu-server:~$ sudo apt install python3.10
root@ubuntu-server:~$ sudo apt install python3.7
Finally, type the following command to see all Python binaries installed on your Ubuntu server.
root@ubuntu-server:~$ ls -l /usr/bin/python*
Sample Output:
lrwxrwxrwx 1 root root 10 Dec 2 2022 /usr/bin/python3 -> python3.10 -rwxr-xr-x 1 root root 5901416 Dec 2 2022 /usr/bin/python3.10 -rwxr-xr-x 1 root root 6705016 Dec 3 09:45 /usr/bin/python3.11 -rwxr-xr-x 1 root root 960 Dec 2 2022 /usr/bin/python3-futurize -rwxr-xr-x 1 root root 964 Dec 2 2022 /usr/bin/python3-pasteurize
In our system, the default Python version used is 3.10, as indicated by the symlink:
lrwxrwxrwx 1 root root 10 Dec 2 2022 /usr/bin/python3 -> python3.10
We can also verify the current Python version by executing the following command:
root@ubuntu-server:~$ python -V
Sample Output:
Python 3.10.4
To use Python 11 whilst maintaining an older version as default, type the following command and hit 'Enter':
root@ubuntu-server:~$ python3.11
By doing so you will enter the Python Shell. To exit the Python interpreter, type the following commands within the shell and then press ‘Enter’:
quit()
OR
exit()
Change Default Python Version on Ubuntu Server
In the case you have multiple Python versions installed, you can change the default version as follows. In our example, our Ubuntu server had as default Python 3.10 and we will change it to Python 3.11.
root@ubuntu-server:~$ python3 --version
root@ubuntu-server:~$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
root@ubuntu-server:~$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2
root@ubuntu-server:~$ sudo update-alternatives --config python3
root@ubuntu-server:~$ python3 --version
That’s all! If you followed all the steps above you have successfully installed Python 3.11 on your Ubuntu server!