Python is one of the most popular programming languages used today for software development, data analysis, machine learning, and more. The latest major version, Python 3.9, was released on October 5, 2020 and includes many new features and optimizations.
In this comprehensive guide, we will cover how to install Python 3.9 on Linux Mint 20 via two methods:
- Using the Deadsnakes PPA repository (recommended)
- Building from source code
We will also demonstrate how to verify the installation and troubleshoot any issues.
Prerequisites
Before installing Python, ensure your system is up-to-date by running:
sudo apt update
sudo apt upgrade
This will fetch the latest package lists and install any available upgrades.
Method 1: Install using Deadsnakes PPA
The recommended way to install Python 3.9 on Linux Mint 20 is via the Deadsnakes PPA repository maintained by Felix Krull. This contains up-to-date builds for newer Python versions.
Here are the steps to add Deadsnakes PPA and install Python 3.9:
-
Install prerequisites:
sudo apt install software-properties-common
The
software-properties-common
package provides theadd-apt-repository
command used to add PPAs. -
Add Deadsnakes PPA:
sudo add-apt-repository ppa:deadsnakes/ppa
When prompted press
Enter
to continue: -
Once the repository is enabled, update package list:
sudo apt update
-
Install Python 3.9 and its dependencies:
sudo apt install python3.9
Type
y
when asked to continue and install the packages. -
Verify Python 3.9 installed correctly:
python3.9 --version
This will print the version installed:
Python 3.9.0
That‘s it! Python 3.9 is now installed and ready to use on your Linux Mint 20 system.
Method 2: Build from Source Code
Building Python from source is more complex but can be useful if you need specific customizations or optimizations. Here is an overview of the process:
Install Build Tools and Dependencies
Python relies on various system libraries and tools to build correctly. Run the following to install them:
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
Download Python 3.9 Source Code
Use wget
to download the latest Python 3.9 tarball (tgz
compressed file):
wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz
Verify using ls
and you should see the .tgz
file downloaded.
Extract Source Files
Use tar
to extract the downloaded tarball:
tar -xf Python-3.9*.tgz
This will decompress the content into a new Python-3.9.0
directory.
Configure and Build
cd Python-3.9.0
./configure --enable-optimizations
make -j 8
This runs the configure script with performance optimizations enabled, then runs make
to build the Python binary and libraries using 8 CPU cores for faster compilation.
Install Python 3.9
sudo make altinstall
The altinstall
target prevents overwriting the default system Python 3 binary.
Verify Installation
Check that Python 3.9 is available by running:
python3.9 --version
And there we have it installed from source!
Next Steps After Installing Python 3.9
Now that you have a fresh Python 3.9 setup, here are some next steps to take:
- Update pip – Python‘s package manager with
python3.9 -m pip install --upgrade pip
- Setup virtual environments – Create isolated Python environments for your projects with
venv
orpip install virtualenv
- Install IPython – An improved Python REPL shell with
python3.9 -m pip install ipython
- Browse the stdlib – Explore the extensive Python standard library for useful modules like
os
,sys
,datetime
and many more
Also check out our other Linux Mint tutorials and guides for managing Python packages, building projects, and more!
Troubleshooting Guide
Here are solutions for common problems faced during Python 3.9 installation on Linux Mint:
Dependency errors
If you get errors like "package xyz has no installation candidate
" while installing Python packages, run:
sudo apt --fix-broken install
This will attempt to fix any broken dependencies.
Permission errors
Using sudo
is required for installing packages globally. If you get "Permission denied
" errors, rerun the commands with sudo
.
Old Python version still default
To make Python 3.9 the default instead of the older system Python 3.x:
- Find location of the binaries:
which python3
andwhich python3.9
- Back up original binary:
sudo mv /usr/bin/python3 /usr/bin/python3-old
- Symlink new version:
sudo ln -s $(which python3.9) /usr/bin/python3
Now python3
will point to Python 3.9.
Can‘t find Python modules
The altinstall
method places binaries in a separate location like /usr/local/bin/python3.9
rather than /usr/bin
. So you may need to use the full path for running scripts, pip installing packages globally, etc.
Or add the directory with Python 3.9 to your $PATH
:
export PATH="/usr/local/bin:$PATH"
Stale cache issues
If you run into issues with old cached Python packages or corrupt downloads, run:
sudo apt clean
rm -rf ~/.cache/pip
This wipes the OS package cache and pip download cache to resolve such problems.
I hope these troubleshooting tips help you in keeping your Python install happy and running smoothly!
Conclusion
There are a few different ways to get the latest Python 3.9 on Linux Mint 20. The Deadsnakes PPA provides a simple and reliable installation. While compiling from source code enables further customization for power users.
Now that you have Python 3.9 ready, you can take advantage of the many new features and speed improvements it brings. Be sure to also brush up on virtual environments, pip/package management, and other Python best practices.
Let me know in the comments if you have any other questions on getting set up with Python 3.9!