How to install Python
1. Introduction
In this blog post I will show you the easiest ways how to install Python 3 on your system. You do not need to have any prior knowledge with programming, but it will help you if you are already familiar with computers.
To have the greatest learning effect, I recommend you to type the examples and not just copy and paste. Thus, you can process the learned better and retrieve easier in later situations.
2. Installing Python on Linux
We start with installing Python 3 on Linux. If you are not already using Linux, I strongly recommend that you use Linux or MacOS for programming. If you are using a Windows system and you do not have Linux, you can install a virtual machine, such as a VirtualBox and set up and install Linux there. I use a virtual machine for the examples myself so I suggest you do the same.
In Linux there are different versions, one speaks also of derivatives. I’ll show you how to install Python among the two most popular derivatives. These are Debian based derivatives and Red Hat based derivatives.
Let’s start with Debian based derivatives.
2.1 Debian Based Derivatives
The first thing we wanna check is if we have already installed Python 3 on our machine. We do this by typing the following command in a terminal:
sudo apt list --installed | grep python3
If Python 3 is not installed, we should get an empty output. In order to install Python, the next step is to download and update the package lists from the repositories. On Debian based systems like Ubuntu you can use the apt-get update
command. apt-get
is the package manager in Debian systems. Enter the following commands:
sudo apt-get update
Now that the package lists have been updated we can upgrade to the latest versions with the apt-get upgrade
command:
sudo apt-get -y upgrade
Once the packages have been upgraded, we can install Python 3.7. This is done with the apt-get install
command:
sudo apt-get install -y python3.7
Once the installation is complete, we confirm that it was successful by calling Python 3.7 and printing out the version number:
python3.7 -V
# expected output
Python 3.7.3
Now we have successfully completed the installation of Python. Since we want to work with other Python packages in the future, we can now install the package management program PIP. For this we execute the following command:
sudo apt-get install -y python3-pip
In the following, we make sure that Python 3.7 is the latest python3
version. To achieve that we set a symbolic link to python3.
sudo ln -sf /usr/bin/python3.7 /usr/bin/python3
Once PIP has been installed we have to update PIP. Unfortunately we have to do this manually although we have just installed PIP, cause the installed version is pretty old, so yes, this step is necessary:
python3 -m pip install --upgrade pip
Once the installation is complete we also confirm this by calling the version number of PIP:
python3 -m pip -V
# expected output
pip 19.2.1 from /home/vagrant/.local/lib/python3.7/site-packages/pip (python 3.7)
Now we are still testing if we can actually install libraries with PIP. For this we use a very popular Python math library named numpy
python3 -m pip install numpy
And that’s it. We have just successfully installed a recent version of Python under a Debian based Linux.
2.2 RHEL (Red Hat) based derivatives
Now I will show you how to install a current Python version under Red Hat based derivatives. In this example I used CentOS as the operating system.
First we check if Python 3 is already installed on our system.
# check if we have a working python3 version installed
python3
# expected output
-bash: python3: command not found
To make sure that no Python packages are installed we check the package list of CentOS. In CentOS you use the yum
command, where yum
stands for Yellowdog Updater, Modified. For the experts among you: the actual package manager under CentOS is rpm and yum is just another rapper for rpm.
# check if we have a any python3 libraries installed
yum list installed | grep python3
# expected output
<empty result>
The next step is to add the repository containing the desired Python libraries located in https://centos7.iuscommunity.org/ius-release.rpm
.
sudo yum install -y https://centos7.iuscommunity.org/ius-release.rpm
Once we have successfully added the new repository, we need to update it. This is done with the yum update
command.
This can take quite some time (usually a few minutes) depending on your internet bandwith and computational power
sudo yum update -y
As soon as the update has been completed successfully, we can install Python 3. In addition to the main Python library, we will also install some auxiliary libraries, such as PIP. We already got to know PIP from the previous example.
sudo yum install -y python36u python36u-libs python36u-devel python36u-pip
Once the installation is complete, we can check that everything has gone through properly. For this we print the version number of Python by executing the following command:
python3.6 -V
# expected output
Python 3.6.x
Now we make sure that the Python version just installed is the current version used on our system by changing the symbolic link. We test this by calling the new link we have just created:
sudo ln -sf /usr/bin/python3.6 /usr/bin/python3
python3 -V
# expected output
Python 3.6.x
I advise against overriding /usr/bin/python, because after this action the package management yum will get problems. This is because yum requires Python 2.x instead of 3.x. Just leave /usr/bin/python as it is and we are good.
Now we have successfully completed the installation of Python. Since we still want to work with other Python packages in the future, we make sure that we have the latest version of PIP installed. To install the latest version of PIP we execute the following command:
sudo python3 -m pip install --upgrade pip
Once the installation is complete we confirm this by printing out the version number:
python3 -m pip -V
# expected output
pip 19.2.1 from /home/vagrant/.local/lib/python3.7/site-packages/pip (python 3.6)
Now we will test if we can actually install libraries with PIP. For this we use again the mathematics library of Python with the name numpy:
sudo -H python3 -m pip install numpy
And that’s it. We have just successfully installed a recent version of Python under a Red Hat based Linux system.
3. Installing Python on MacOS
Finally, I will show you how to install a current Python version under MacOS. I have used MacOS 10.14 Mojave for this example. We will use Homebrew for the installation. For this we need to first install the command line tools of Apple’s Xcode. We open a terminal and install the command line tools with the following command:
xcode-select --install
Confirm all questions and wait a few minutes. Xcode is very large program, so the installation may take a few minutes. Once the installation of Xcode is complete we can install Homebrew. For this we type the following:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Once the installation is complete we confirm that it was successful:
brew --version
# expected output
Homebrew 2.1.2
Homebrew/homebrew-core (git revision a326c; last commit 2019-05-15)
Homebrew/homebrew-cask (git revision 6b623; last commit 2019-05-15)
Now we can install Python:
brew install python3
Once the installation has been completed we confirm this by typing the following:
python3 --version
# expected output
Python 3.7.x
Again, we can install PIP analogous to the examples with Linux. This is done in the same way as before, so I refer to it at this point.
4. Questions
You have now learned how to install Python on Linux and MacOS and you should be able to answer the following questions:
- What is
apt-get
? - What is
yum
? - What is
Homebrew
? - What is
PIP
?
5. Summary
In this tutorial, I showed how Python can be installed on Linux and MacOS. Here we looked at the installation of Python in different Linux derivatives and used the different package managers.
The most important tools from this blog post are:
You can find code examples in my GitLab repository. If you want to learn more about Python and programming, I recommend you this book.
If you enjoyed the blog post, be sure to check out my YouTube Channel as well, write me a message or comment below and subscribe to my newsletter to stay up to date..