How to Install Python in Linux

While the Raspberry Pi’s operating system contains the latest, stable version of Python, other Linux distros don’t come with Python 3 pre-installed. If you’re not going down the Pi route, then here’s how to check and install Python for Linux.

Linux is such a versatile operating system that it’s often difficult to nail down just one-way of doing something. Different distributions go about installing software in different ways, so for this particular tutorial we will stick to Linux Mint.

First you need to ascertain which version of Python is currently installed in your Linux system. To begin with, drop into a Terminal session from your distro’s menu, or hit the Ctrl+Alt+T keys. Next, enter:

python --version

into the Terminal screen. You should have the output relating to version 2.x of Python in the display. Most Linux distro come with both Python 2 and 3 by default, as there’s plenty of code out there still available for Python 2. Now enter:

python3 --version

In our case we have both Python 2 and 3 installed. As long as Python 3.x.x is installed, then the code in our tutorials will work. It’s always worth checking to see if the distro has been updated with the latest versions, enter:

sudo apt-get update && sudo apt-get upgrade

to update the system. Once the update and upgrade completes, enter:

python3 --version

again to see if Python 3.x is updated, or even installed. As long as you have Python 3.x, you’re running the most recent major version, the numbers after the 3. indicate patches plus further updates. Often they’re unnecessary, but they can contain vital new elements.

However, if you want the latest, cutting edge version, you’ll need to build Python from source. Start by entering these commands into the Terminal:

sudo apt-get install build-essential checkinstall
sudo apt-get install libreadline-gplv2-dev
libncursesw5-dev libssl-dev libsqlite3-dev tk-dev
libgdbm-dev libc6-dev libbz2-dev

Open up your Linux web browser and go to the Python download page: https://www.python.org/
downloads. Click on the Downloads, followed by the button under the Python Source window. This opens a download dialogue box, choose a location, then start the download process.

In the Terminal, go to the Downloads folder by entering:

cd Downloads/

Then unzip the contents of the downloaded Python source code with:

tar -xvf Python-3.Y.Y.tar.xz (replace the Y’s with the version numbers you’ve downloaded).

Now enter the newly unzipped folder with:

cd Python-3.Y.Y/

Within the Python folder, enter:

./configure
sudo make altinstall

This could take a while, depending on the speed of your computer. Once finished, enter:

python3.7 --version

to check the latest installed version. You now have Python 3.7 installed, alongside older Python 3.x.x and Python 2.

For the GUI IDLE, you’ll need to enter the following command into the Terminal:

sudo apt-get install idle3

The IDLE can then be started with the command:

idle3

Note that IDLE runs a different version to the one you installed from source.

You’ll also need PIP (Pip Installs Packages), which is a tool to help you install more modules and extras. Enter:

sudo apt-get install python3-pip

Once PIP is installed, check for the latest update with:

pip3 install --upgrade pip

When complete, close the Terminal and Python 3.x will be available via the Programming section in your distro’s menu.

Find more guides like this in…

David Hayward

David has spent most of his life tinkering with technology, from the ZX Spectrum, getting his hands on a Fujitsu VPP5000/100 supercomputer, and coding on an overheating Raspberry Pi. He's written for the likes of Micro Mart, Den of Geek, and countless retro sites and publications, covering reviews, creating code and bench testing the latest tech. He also has a huge collection of cables.

Related Articles

Back to top button