Python 3.8, a significant historical release, introduced various features that enhanced coding efficiency and system performance. Here’s an overview of its key features:
- Assignment Expressions: Introduced the “walrus operator” (:=) for in-expression variable assignments, streamlining code.
- Positional-only Parameters: Added ‘/’ syntax to specify arguments that must be passed positionally, improving function interfaces.
- F-strings Debugging: Enhanced F-strings with the ‘!=’ specifier to display expressions and their results, facilitating debugging.
- Performance Enhancements: Improved function call speeds, optimized memory usage, and upgraded garbage collection.
- Syntax and Typing Advances: Included new syntax like ‘continue’ in ‘finally’ clauses and expanded typing options such as ‘TypedDict’.
These improvements marked Python 3.8 as a platform for developers, especially those using Ubuntu systems. With these capabilities, let’s dive into installing Python 3.8 on Ubuntu 24.04, 22.04, or 20.04 and harnessing these features effectively.
Python 3.8 Pre-Installation Steps
Update System Before Proceeding
To ensure a smooth installation of Python 3.8 on your Ubuntu system, update the system’s package list. This process refreshes your package database, ensuring that all software installations and upgrades are compatible and based on the latest available versions.
Open your terminal and execute the following command:
sudo apt update
Once the package lists are updated, upgrade any installed packages with newer versions available. This step is crucial to avoid software conflicts arising from outdated components.
To upgrade the packages, use the following command:
sudo apt upgrade
Next, based on your project’s requirements, choose one of the methods below to install Python 3.8.
Method 1: Install Python 3.8 via PPA
Import Python 3.8 PPA
For Ubuntu users, the easiest way to access the latest updates for Python and additional required packages is by importing the “deadsnakes” team Launchpad PPA. This will enable you to install and update Python 3.8 directly from your terminal. To import the PPA, run the following command:
sudo add-apt-repository ppa:deadsnakes/ppa -y
Update Package List After Python 3.8 Import
After importing the PPA, you must update your package list to ensure your system recognizes the newly added repository. To do this, run the following command in your terminal:
sudo apt update
Install Python 3.8 via APT Command
With the PPA successfully imported and your package list updated, you’re ready to install Python 3.8. To do so, run the following command:
sudo apt install python3.8
Verify Python 3.8 Installation
After the installation, verifying that Python 3.8 has been installed correctly and checking its build version is essential. To do this, run the following command in your terminal:
python3.8 --version
Install Additional Python Packages on Ubuntu (Optional)
If desired, you can also install the following additional extra:
Debug module Python 3.8 Ubuntu installation command
sudo apt install python3.8-dbg
Developer (dev) module Python 3.8 Ubuntu installation command
sudo apt install python3.8-dev
VENV (virtual environment) module Python 3.8 Ubuntu installation command
sudo apt install python3.8-venv
Distutils module Python 3.8 Ubuntu installation command
sudo apt install python3.8-distutils
lib2to3 utility module Python 3.8 Ubuntu installation command
sudo apt install python3.8-lib2to3
DBM.GNU module Python 3.8 Ubuntu installation command
sudo apt install python3.8-gdbm
Tkinter module Python 3.8 Ubuntu installation command
sudo apt install python3.8-tk
Alternatively, to install all extras, run the full installation command.
sudo apt install python3.8-full
Method 2: Install Python 3.8 via source Archive
Download Python 3.8
To begin, visit the official Python download page and obtain the download link for the latest version of the Python release you want. Remember that these instructions should work for any version, as you’ll be compiling it yourself. Once you’ve copied the download link, use the wget
command to download the Python 3.8 archive:
wget https://www.python.org/ftp/python/3.8.15/Python-3.8.15.tar.xz
Note: The download link may change, so be sure to obtain a fresh link. The command above is just an example. After downloading the Python archive, extract it and remember to update the version number if you downloaded a newer release:
tar -xf Python-3.8.{version}.tar.xz
Install Required Packages for Python 3.8
Next, you’ll need to install the dependencies required to compile and install Python 3.8:
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev pkg-config make -y
Configure the Python Build Environment
Navigate to the directory where you extracted the Python source code:
cd Python3.8.{version}/
Now, run the ./configure
command with the --enable-optimizations
and --enable-shared
options:
./configure --enable-optimizations --enable-shared
The script will perform several checks to ensure that all necessary dependencies are present on your system. Running ./configure
with --enable-optimizations
will optimize the Python binary by running multiple tests. This can make the build process slower but will result in a faster and more efficient Python installation.
Compile Python 3.8
After configuring the build environment, it’s time to compile Python using the make
command:
make
To significantly increase the compiling speed, you can use the -j <number of CPUs>
option, which specifies the number of CPUs you want to utilize. For example, if your server has 6 CPUs, you can use all six or at least 4 to 5 to increase the speed. The command would be make -j 6
.
make -j 6
Install Python Binaries
After finishing the building process, install the Python binaries by running the following command: sudo make altinstall
. Using the make altinstall
command is recommended to avoid overwriting the default Python 3 binary system.
sudo make altinstall
Finally, configure the dynamic linker run-time bindings by running the ldconfig
command after installation:
sudo ldconfig
Example only:
sudo ldconfig /opt/Python3.8.16
Verify Python 3.8 Installation
Run the following command to confirm that Python 3.8 and the corresponding build version have been successfully installed:
sudo python3.8 --version
Install PIP with Python 3.8
Install PIP via Python 3.8
For most users using the Python PPA repository, installing PIP for Python 3.8 can be accomplished simply by running the following APT command:
sudo apt install python3-pip
Download the get-pip.py script
To manually install PIP for Python 3.8, you will first need to download the get-pip.py
script using the wget
command:
wget https://bootstrap.pypa.io/get-pip.py
This script is provided by the Python Packaging Authority (PyPA) and is used to install or upgrade PIP on your system.
Install PIP for Python 3.8
Once you have downloaded the get-pip.py
file, you can proceed with the installation by running the following command:
python3 get-pip.py
This will install PIP specifically for Python 3.8 on your system.
Upgrade PIP to the latest version
After successfully installing PIP, you should check for upgrades to ensure you have the latest version. You can do this by running the following command:
python3 -m pip install --upgrade pip
You should see output similar to the following:
joshua@ubuntu-linux:~$ python3.8 -m pip install --upgrade pip
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pip in ./.local/lib/python3.8/site-packages (22.3.1)
This output indicates that PIP has been upgraded to the latest version.
Verify the installed version of PIP
You can verify the version of PIP for Python 3.8 that is installed on your system by running the following command:
pip3.8 --version
This will display the current PIP version associated with your Python 3.8 installation.
Switch Default Python Versions
If you need multiple versions of Python installed on your system and want to set a particular one as the default, follow these steps to change Python versions.
Add symbolic links for each Python version
First, you must separately add symbolic links for each Python version via symlink. First, add the group name “python” with the “version numbers you are using”.
Here’s an example (you can customize this or copy it):
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 3
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 4
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 5
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.11 6
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.12 7
Remember that you don’t need to list all the Python versions you have installed – copying the entire command will create symbolic links for the versions on your system.
List available Python versions
To list the available Python versions, use the following command:
sudo update-alternatives --config python
This will display a list of installed Python versions with their respective selection numbers. An asterisk (*) beside the selection number will indicate the currently set default version.
Set a different Python version as the default
To set a different version as the default (e.g., Python 3.8), enter the corresponding selection number (which will vary depending on the number of versions you have installed). In this example, the selection number for Python 3.8 is 6.
After running the command to set Python 3.8 as the default version, the output should confirm the change:
update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python (python) in manual mode
Verify the default Python version
After setting Python 3.8 as the default version, you can list the available alternative options again to ensure that Python 3.8 is now the default version (indicated by an asterisk *):
sudo update-alternatives --config python
This will confirm that the default Python version has been successfully switched to the desired version.
Conclusion
In conclusion, installing Python 3.8 on Ubuntu Linux is straightforward if you follow the provided steps. Users can easily switch between different Python environments by understanding how to manage multiple Python versions and setting the desired one as the default. This flexibility is essential for developers who work on multiple projects with varying Python version requirements. Always keep your Python installation and packages up to date to ensure a secure and stable development environment.