Get the highlights in your inbox every week.
How to install Python on Linux | Opensource.com
How to install Python on Linux
Step-by-step instructions for installing the latest Python instead of (or alongside) an older version on Linux.
Python is now the most popular, most used programming language. Python's simple syntax and low learning curve make it the ultimate choice for beginners as well as professional developers. Python is also a very versatile programming language. It's used nearly everywhere—from web development to artificial intelligence—really anywhere other than mobile development.
If you're using Python, there's a good chance you're a developer (or want to become one), and Linux is a great platform for creating software. But when you're working with Python every day, you sometimes want to stay up to date with the very latest version. You may not want to replace the default install of Python on your system just to test drive the latest one, so this article explains how to install the latest version of Python 3 on Linux without replacing the version provided by your distribution.
Use the python --version terminal command to check whether Python is already installed and, if so, which version you have. If Python is not installed on your Linux system, or you want to install an updated version, follow the steps below.
Step-by-step installation instructions
Step 1: First, install development packages required to build Python.
$ sudo apt update
$ sudo apt install build-essential zlib1g-dev \
libncurses5-dev libgdbm-dev libnss3-dev \
libssl-dev libreadline-dev libffi-dev curl
$ sudo dnf groupinstall development
Step 2: Download the stable latest release of Python 3
Visit the official Python website and download the latest version of Python 3. After the download is complete, you hav a .tar.xz archive file (a "tarball") containing the source code of Python.
Step 3: Extract the tarball
Once the download is complete, extract the tarball by either using the extractor application of your choice or the Linux tar command, for example:
$ tar -xf Python-3.?.?.tar.xz
Step 4: Configure the script
Once the Python tarball has been extracted, navigate to the configure script and execute it in your Linux terminal with:
$ cd Python-3.*
The configuration may take some time. Wait until it is successfully finishes before proceeding.
Step 5: Start the build process
If you already have a version of Python installed on your system and you want to install the new version alongside it, use this command:
$ sudo make altinstall
The build process may take some time.
If you want to replace your current version of Python with this new version, you should uninstall your current Python package using your package manager (such as apt or dnf) and then install:
$ sudo make install
However, it's generally preferable to install software as a package (such as a .deb or .rpm file) so your system can track and update it for you. Because this article assumes the latest Python isn't yet packaged yet, though, you probably don't have that option. In that case, you can either install Python with altinstall as suggested, or rebuild an existing Python package using the latest source code. That's an advanced topic and specific to your distribution, so it's out of scope for this article.
Step 6: Verify the installation
If you haven't encountered any errors, the latest Python is now installed on your Linux system. To verify it, write one of these commands in your terminal:
If the output says Python 3.x, Python 3 has been successfully installed.
Create a virtual environment (optional)Python provides a package known as venv (virtual environment), which helps you isolate a program directory or package from other ones.
To create a virtual environment, enter the following in the Python terminal (in this example, assume the version of Python you've installed is in the 3.8 series):
python3.8 -m venv example
This command creates a new directory (which I've named example), with some subdirectories.
To activate the virtual environment, enter:
$ source example/bin/activate
Notice that your terminal prompt ($) is now preceeded by an environment name.
To deactivate the virtual environment, use the deactivate command:
(example) $ deactivate
Python is a fun language that's developed and improved frequently. Getting familiar with new features is easy, once you understand how to install the latest release without interfering with the stable version provided from your distribution.
If you have any feedback or questions, please leave them in the comments.