Introduction
Python, a versatile and powerful programming language, is constantly evolving with new versions offering enhanced features and performance improvements. Python 3.11 is one such version that brings exciting enhancements. To harness its full potential, how to install Python 3.11 from source on Ubuntu 22.04 can be a great choice. This comprehensive guide will walk you through the step-by-step process, ensuring a successful installation for optimal performance.
Table of Contents
Why Install Python 3.11 from Source?
Install Python 3.11 from source on Ubuntu provides several advantages over using the package manager. It allows you to have more control over the configuration, choose custom build options, and ensure compatibility with specific applications. Additionally, it lets you stay up-to-date with the latest Python release, utilizing its performance improvements and new features.
Python versions, Release Dates and EOL:
Versions | Released Dates | End of life |
3.11 | 24 Oct 2022 | 24 Oct 2027 |
3.1 | 04 Oct 2021 | 04 Oct 2026 |
3.9 | 05 Oct 2020 | 05 Oct 2025 |
3.8 | 14 Oct 2019 | 14 Oct 2024 |
3.7 | 26 Jun 2018 | 27 Jun 2023 |
3.6 | 22 Dec 2016 | 23 Dec 2021 |
2.7 | 01 Oct 2008 | 29 Oct 2013 |
Prerequisites
Before getting started install Python 3.11 from source on Ubuntu, here is a list of requirements that you need:
- Ubuntu Server: Make sure you have a clean installation of Ubuntu Server. To set this up, follow our guide : Initial Setup Ubuntu Server 22.04: Secure and Efficient. You can deploy this on a physical machine or a virtual environment like VMware or VirtualBox.
- A stable internet connection to download packages
- Familiarity with basic Linux commands
Install Python 3.11 from Source
If you want to install the latest Python version, you can use two options: installing from source or from a PPA.
Compiling Python on Ubuntu from source allows you to install the most recent Python version and customize the build parameters. However, you will be unable to maintain your Python installation using the apt package manager.
By default, Ubuntu 22.04 comes with Python version 3.10
samm@python3:~$ python3 -V Python 3.10.12 |
Step 1: System Update
Before you begin, ensure that your system is up-to-date by running the following commands:
samm@python3:~$ sudo apt update samm@python3:~$ sudo apt upgrade |
Step 2: Install the Dependencies
Python is 3.11 is the most recent version of the most recent major release. This version incorporates several performance improvements and new features, such as new standards library modules, new syntax, and built-in capabilities, and more.
The following instructions outline the process of compiling Python 3.11 from source on Ubuntu. If you’re installing a newer release, simply modify the version number in the commands provided.
You’ll also need essential build tools and development libraries. Install them with:
samm@python3:~$ sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev |
Step 3: Download and Install python 3.11 from Source
Now navigate to the Python’s download page and download the latest release of Python use wget to fetch it directly from the terminal:
samm@python3:~$ wget https://www.python.org/ftp/python/3.11.4/Python-3.11.4.tgz |
Once the download is complete, extract the archive as follows:
samm@python3:~$ tar -xf Python-3.11.4.tgz |
Open the Python on Ubuntu source directory and execute the configure command.
samm@python3:~$ cd Python-3.11.4/ |
Then execute the following ./configure
command. The command runs a thorough test to check if all the dependencies for installing Python3 are met.
samm@python3:~/Python-3.11.4$ sudo ./configure --enable-optimizations |
The --enable-optimizations
option allows the Python binary to be optimized by running multiple tests. This slows down the building process.
Next, start the build process:
samm@python3:~/Python-3.11.4$ make -j 2 |
The -j$(nproc)
flag parallelizes the build process, utilizing all available CPU cores. By entering nproc
, you may find the number of cores in your system.
Once the build is finished, install the Python binaries by typing:
samm@python3:~/Python-3.11.4$ sudo make altinstall |
We’re using altinstall
instead of install
since the later command would replace the system’s default python3 binary. Upon installation of all the required Python libraries, now confirm the version of Python installed.
samm@python3:~$ python3.11 -V |
You should see the version information displayed.
Python 3.11.4 |
Python 3.11 releases with excellent promises. The best of all is its speed. It’s up to 60% faster than Python 3.10.
Set up a Virtual Environment (Optional)
By creating virtual environments, you can establish a separate space on your server for your Python projects. This enables each project to maintain a unique set of requirements without causing interference with any of the others. Now, let’s proceed to install Venv by entering:
samm@python3:~$ sudo apt install -y python3-venv |
With that module installed, you can set up your environment in any directory of your preference. In this case, we will create a new directory and navigate into it.
samm@python3:~$ mkdir samm-environment && cd samm-environment |
You can create an environment by executing the following command after you are in the directory where you want the environments to live:
samm@python3:~/samm-environment$ python3 -m venv my_env |
You can view the contents of the virtual environment using the ls
command as shown.
samm@python3:~/samm-environment$ ls my_env |
output : bin include lib lib64 pyvenv.cfg |
The python on Ubuntu 22.04 sharing directory will contain Python Wheels, a built-package format for Python that can speed up software development by reducing the number of compilations required.
You must activate this environment before using it, which you can do by entering the command that invokes the activate script as follows:
samm@python3:~/samm-environment$ source my_env/bin/activate |
You’re environment’s name, in this case my_env, will now appear before your command prompt. Your prefix may alter somewhat depending on the Debian Linux version you are using, but the name of your environment in parentheses should be the first item you see on your line:
(my_env) samm@python3:~/samm-environment$ |
This prefix informs us that the environment my_env is now active, which means that any programs we write here will only make use of the parameters and components specific to this environment.
Note: You can choose to use the commands pip or python instead of pip3 inside the virtual environment. You must only use the python3 and pip3 commands if you use Python 3 on a system that is not part of an environment.
Your virtual environment is ready for usage once you’ve completed these procedures.
Create a Simple Python Program
The installation is completed now ready, so let’s make a classic “Hello, World!” program. This will allow us to test our environment and, if we haven’t already, provide us a chance to learn more about Python.
To accomplish this, we’ll create a simple Python file using the vim
text editor. Here, our Python file is called ‘hello.py’.
(my_env) samm@python3:~/samm-environment$ vi hello.py |
print("Hello, World!") |
Save the file and exit.
Finally, we will run the program as follows.
(my_env) samm@python3:~/samm-environment$ python3 hello.py |
The output from your terminal should look like this thanks to the hello.py program you wrote:
Output: Hello, World! |
Enter the command deactivate
to exit the environment, and your current directory will be where you start.
(my_env) samm@python3:~/samm-environment$ deactivate samm@python3:~/samm-environment$ |
Conclusion
By following this guide, you’ve successfully installed Python 3.11 from source on Ubuntu 22.04. This method provides you with greater control over your Python environment, allowing you to take full advantage of its enhanced features and improved performance. Whether you’re a developer or an enthusiast, exploring Python 3.11’s capabilities will undoubtedly enrich your programming journey.
Also Read Our Other Guides :
- How To Install Python 3.11 on Rocky Linux 9
- How To Install MySQL 8.0 on Ubuntu Server 22.04
- How To Install MariaDB 10.9 on Debian 11 Server
- How To Install MariaDB 10.6 on Debian 11 Server
- How To Create AWS CloudFront: A Step-by-Step Guide
Hopefully, now you have learned how to install Python 3.11 from source on Ubuntu 22.04.