How to Install Python 3 on Linux. Guide to Installing and Configuring Python Correctly

Daniel Morales
Mar 28, 2020


Most likely your current Linux distribution already has Python installed, but you probably don't have the latest version, or at least version 3. To see if your operating system is installed and to find out the current version, just type the following code in your command line:

python --version
You probably have version 2, let's check if you have version 3 installed:

python3 --version
If version 3 appears, you have it installed and updated. 

If you get version 2, or a version less than 3.6.5 (at this time), it's a good idea to install the latest version. To do this, it depends on the Linux distribution you have installed on your computer.

Ubuntu

Depending on the Ubuntu distribution you are running on your machine, the instructions vary. You can determine which version of Ubuntu you have with the following command:

➜  ~ lsb_release -a 
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 16.04.5 LTS
Release:	16.04
Codename:	xenial
Depending on the version number under Release on the console output, continue with these instructions

  • Ubuntu 17.10, Ubuntu 18.04 (and higher) come with Python 3.6 by default. You should be able to call it with the python3 command.
  • Ubuntu 16.10 and 17.04 don't come with Python 3.6 by default, but it's in the universal repository. You should be able to install it with the following commands:
    • $ sudo apt-get update
      $ sudo apt-get install python3.6
    • you can then call it with the python3.6 command.
  • If you are using Ubuntu 14.04 or 16.04, Python 3.6 is not in the universal repository, you will need to get it from a Personal Packages File (PPA). For example, to install Python from the PPA "deadsnakes", do the following:
    • $ sudo add-apt-repository ppa:deadsnakes/ppa
      $ sudo apt-get update
      $ sudo apt-get install python3.6
    • As in the previous case, call it with the command python3.6.
Linux Mint
Mint and Ubuntu use the same package management system, which often makes our lives easier. You can follow the instructions above for Ubuntu 14.04. The PPA "deadsnakes" works with Mint.

CentOS
The IUS community does a good job of providing new software releases for "Enterprise Linux" distributions (i.e. Red Hat Enterprise and CentOS). You can use their work to help you install Python 3

To install, you should first update your system with the yum package manager:

$ sudo yum update
$ sudo yum install yum-utils
then you can install the CentOS IUS package that will bring you up to date with your site:

$ sudo yum install https://centos7.iuscommunity.org/ius-release.rpm
Finally you can install Python and Pip:

$ sudo yum install python36u
$ sudo yum install python36u-pip
Debian
There are sources that say that the Ubuntu 16.10 method works the same for Debian, but it often causes failures in Debian 9.

One problem with Debian, however, is that it generally does not install the sudo command by default. To install it, you will need to do the following before carrying out the Python compile-time instructions below:

$ su
$ apt-get install sudo
$ vi /etc/sudoers

After that, open the file /etc/sudoers using the command sudo vim (or your favorite text editor.) Add the following line of text at the end of the file, replacing your_username with your real username:

your_username ALL=(ALL) ALL
Fedora
Fedora has a roadmap for switching to Python 3 as the default Python. So the current version and future versions will be delivered with Python 2 as default, but Python 3 will be installed. If the python3 installed in your version is not 3.6, you can use the following command to install it

$ sudo dnf install python36
openSUSE
Doing this installation by means of zypper is somewhat problematic and may not work properly. It is a better idea to build the Python code from source. To do this, it is necessary to install the development tools, which can be done in YaST (through the menus) or using zypper:

sudu zypper install -t pattern devel_C_C++
This step will take some time and involves the installation of 154 packages, but once it is complete, you will be able to build the source code as shown in the Compiling Python from Source section below.

Arch Linux
Arch Linux is quite aggressive in keeping up with Python versions. You probably already have the latest version. If not, you can use this command:

$ packman -S python

Compiling Python from source
To start, you need to get the Python source code. Python.org makes it pretty easy. If you go to the download page, you'll see the latest Python 3 source code at the top. (Be sure not to take the Legacy Python, Python 2).

When you select the version, at the bottom of the page there is a Files section. For example for the latest version 3.8 you can see it here: https://www.python.org/downloads/release/python-381/.  Select the compressed source tarball and download it to your machine. If you prefer a command line method, you can easily use wget to download it to your current directory:

$ wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
Step 2: Prepare your system
There are a few distribution-specific steps needed to build Python from scratch. The goal of each step is the same in all distributions, but you may have to translate it into your distribution if you don't use apt-get:

The first step you should take when performing an operation like this is to update the system packages on your machine before you start. In Debian, this is what it looks like:

$ sudo apt-get update
$ sudo apt-get upgrade
Next, we want to make sure that the system has the necessary tools to build Python. There are a lot of them and you may already have some, but that's okay. We've listed them all on one command line, but you can split the list into shorter commands by just repeating the sudo apt-get install -y portion:

# For apt-based systems (like Debian, Ubuntu, and Mint)
$ sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev  libncursesw5-dev xz-utils tk-dev

# For yum-based systems (like CentOS)
$ sudo yum -y groupinstall development
$ sudo yum -y install zlib-devel


Step 3: Build Python
Once you have the prerequisites and the tar file, you can unpack the source code into a directory. Note that the following command will create a new directory called Python-3.6.5 under which you are:

$ tar xvf Python-3.6.5.tgz
$ cd Python-3.6.5
Now you need to run the ./configure tool to prepare the construction:

$ ./configure --enable-optimizations --with-ensurepip=install
Then build the Python programs using make. The -j option simply tells make to split the construction into parallel steps to speed up compilation. Even with parallel constructions, this step can take several minutes:

$ make -j 8
Next, you'll want to install your new version of Python. You will use the altinstall target here so as not to overwrite the Python version of the system. Since you are installing Python in /usr/bin, you will need to run it as root:

$ sudo make altinstall
Warning: Please only use the altinstall target in make. Using the installation target will overwrite the python binary. While this sounds like a great idea, there are large parts of the system that depend on the pre-installed version of Python.

Step 4: Verify the Python Installation
Finally, you can test your new version of Python:

$ python3.6 -V
Conclusion: 

Installing Python 3 on linux is not that difficult, we just have to follow the step by step of each one of the distributions, and with this we will get the power of python on our machine, and we can start making code focused on Data Science and machine Learning!

“How to Install Python 3 on Linux. Guide to Installing and Configuring Python Correctly”
– Daniel Morales twitter social icon Tweet

Share this article:

0 Comments

Post a comment
Log In to Comment

Related Stories

Jul 23, 2021

Pandas vs SQL. When Data Scientists Should Use One Over the Other

A deep dive into the benefits of each toolTable of ContentsIntroductionPandasSQLSummaryReferencesIntroductionBoth of these tools are important to n...

Matt Przybyla
By Matt Przybyla
Jul 14, 2021

How To Write The Perfect Data Science CV

These tips are also applicable to Software Engineers. Make a few changes in your CV and land that job!Writing a good CV can be one of the toughest ...

Roman Orac
By Roman Orac
Jul 09, 2021

Separating Hype From Value In Artificial Intelligence

You've probably heard a lot about data science, artificial intelligence and big data. Frankly, there has been a lot of hype around these areas. Wha...

Daniel Morales
By Daniel Morales

Win USD $2,000 in cash prizes with our data science competition!

🎉 Model submissions for the "Keyword Recency Prediction" competition will close in

arrow-up icon