As a developer, having the right Python version is crucial for writing and running your applications properly. However, Ubuntu often comes with an older Python version pre-installed. So you may need to install a newer or specific Python release yourself.
In this comprehensive guide, we‘ll walk through the steps to install a specific Python version on Ubuntu 20.04. Whether you want the latest Python 3.x or legacy Python 2.7, this tutorial has got you covered.
There are several reasons why you may want to install a particular Python version on your Ubuntu system:
Application Requirements: The application or library you want to use may require a specific minor/major Python version to function properly. For example, Django 4.x needs Python 3.8 and won‘t work on 3.7 or below.
New Features: Newer Python releases add useful language features, performance improvements, and bug fixes. You‘ll want the latest if using modern Python code and packages. Python 3.10 introduced structural pattern matching, faster dictionaries and more.
Legacy Software: Some old applications depend on Python 2.7 which is obsolete. Installing 2.7 alongside newer releases lets you run this legacy software.
Testing and Experimentation: As a developer, having multiple Python versions lets you easily test applications across releases. You can catch version-specific bugs before users do.
System Stability: Instead of directly replacing the OS-provided Python, install to a separate location. This way if something breaks, your system Python remains intact.
In summary, some valid reasons to install a specific Python version include app requirements, access to new features, supporting legacy software, testing, and system stability.
Now let‘s see the steps…
Prerequisites
This tutorial assumes you have:
- Ubuntu 20.04 LTS desktop installed
- Non-root user account with
sudoprivileges - Internet connectivity to download Python releases
To confirm your Ubuntu version run:
lsb_release -a
And check you have sudo:
sudo -V
If the above commands run fine, you‘re all set! Now let‘s move on to installing the necessary build tools before getting Python itself.
Install Build Tools
Python relies on a number of system libraries and build tools to compile additional modules that extend its base functionality. So before grabbing Python, use apt to install the compiler, headers, linker and other dependencies:
sudo apt update
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
The key packages here:
- build-essential: Essential compilation tools like GCC and make.
- zlib1g-dev: Compression library – enables Python‘s ZIP modules.
- libncurses5-dev: Terminal control and display library – integrates the curses module for text UI apps.
- libgdbm-dev: GNU database manager. Enhances Python‘s dbm module.
- libreadline-dev: Enables advanced line editing and history features for the Python shell.
- libssl-dev: Cryptography and SSL capabilities for Python scripts through pyOpenSSL, pycrypto etc.
The remaining packages provide various other capabilities as their names suggest. Installing them now prevents errors when building Python modules later.
Alright, with the dependencies sorted, we can proceed to install Python itself.
Download Python Release
First pick the Python version that suits your needs. As of writing, here are some popular choices:
- Latest Python 3: Python 3.10.x
- Wide compatibility: Python 3.7.x or 3.8.x
- Legacy 2.7 (not recommended): Python 2.7.x
Visit python.org and grab the source code for your chosen release.
Here is how to download Python 3.8.2:
cd /tmp
wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz
We download directly to /tmp to avoid cluttering the home directory. Feel free to use an alternative location if you prefer.
You can inspect the downloaded archive through:
tar -tf Python-3.8.2.tgz
With Python in hand, it‘s time to install!
Install Python from Source
To demonstrate, we‘ll be installing Python 3.8.2 to a local python-3.8 folder within our home directory.
Extract the archive with:
tar -xzf Python-3.8.2.tgz
This expands the source files into a folder named Python-3.8.2.
Next, enter this folder and configure the Makefile with system-specific settings:
cd Python-3.8.2
./configure --enable-optimizations --with-ensurepip=install --prefix=$HOME/python-3.8
Let‘s break down what we passed to ./configure:
- –enable-optimizations: Enable performance optimizations like the Python byte-code compiler. Can slightly slow initial startup but code runs faster.
- –with-ensurepip=install: Install pip and setuptools by default for easily installing additional Python packages later.
- –prefix=$HOME/python-3.8: The install location. Omit this to replace the default system Python instead.
With configuration done, compile the executable and libraries:
make -j 8
The -j 8 runs 8 parallel jobs to speed up building using multiple CPU cores. Adjust this value as per cores available on your system or remove it altogether to use just one core.
And lastly install:
make altinstall
We use altinstall so our newly built Python is accessible through python3.8 instead of conflicting with older python binaries.
The full process completes in a few minutes. We should now have python3.8 installed locally under ~/python-3.8/.
Set Environment PATH
At this point Python 3.8 is installed but not yet accessible system-wide. We need to add the bin folder inside our install location to the shell‘s PATH.
To do so, use nano or your favorite text editor to open .profile in your home folder:
nano ~/.profile
Assuming your chosen install prefix was $HOME/python-3.8, add this line:
export PATH="$HOME/python-3.8/bin:$PATH"
Save and close the file when done updating.
For changes to take effect, either restart your terminal session or run:
source ~/.profile
With that set, verify you can now access the specific python version:
python3.8 --version
All going well, this should print details on the newly installed Python 3.8 without issues.
Install Pip Modules
Our custom Python has pip and setuptools pre-installed. We can use them to download additional modules:
python3.8 -m pip install numpy scipy matplotlib
This leverages the -m flag to run pip as a module after ensuring the right python version loads first.
Any libraries you install now get added into the local site-packages folder belonging exclusively to our separate Python install. They do not mess with the system PYTHONPATH at all.
This isolation allows you to experiment freely with different versions and builds without destabilizing system components.
Run Python Apps
With everything set up, let‘s write a quick application to test drive our specific python setup.
Save the following as myapp.py:
#!/usr/bin/env python3.8
import sys
print(sys.version)
import numpy as np
print(np.__version__)
The shebang line calls the python3.8 executable explicitly.
Make the script executable:
chmod +x myapp.py
And execute:
./myapp.py
This sample app prints the active Python version before confirming numpy installed correctly.
As long as the shebang calls our custom python binary, apps default to our isolated environment. No need to activate anything!
So that‘s it! You have successfully installed a specific Python release from binaries on Ubuntu 20.04.
Next Steps
Some ideas for taking your specific Python setup further:
- Install tools like virtualenv and create project-specific venvs instead of a system-wide install.
- Look into pyenv for easily managing multiple Python versions on a per-project basis.
- Containerize your apps with Docker so the required Python gets bundled together.
- For data science/analysis packages, check out Anaconda and Miniconda.
Thanks for reading and happy Pythoning! Let me know if you have any questions.