In this post, we will learn the various ways of installing Python 3 on MacOS or Linux.
Installing on MacOS
MacOS X and above comes with the legacy version of Python i.e. Python 2.7 out of the box. This can be checked by adding the following code in terminal:-
$ python -V
Python 2.7
But that is an old version and we will be using the following ways to install the latest stable version of Python 3.7:-
1. Using the dmg Installer
Open https://www.python.org/downloads/ and this will automatically detect your OS and show you an option to download the latest stable version of Python 3.7, which at the time of writing this blog is 3.7.2.
Double click the downloaded file and follow the steps to install Python 3.7.2.
Once finished you can check for the version installed using the following code in terminal:-
$ python3 -V
Python 3.7.2
You need to remember two things here:-
- Every time you need to use Python 3 u need to use ‘python3’ instead of ‘python’ because ‘python’ by default will run Python 2.7. There are ways to change it but that needs another post and we will come up with that soon.
- -V is in uppercase, we can also use ‘–version’.
Once, you are done you can check for pip using the following code, if it runs without an error, then you are good to go:-
pip3 freeze
or
pip freeze
Alternatively, you can install pip3 using the following command:-
$ sudo easy_install pip3
Watch the video Tutorial
You can also watch the following video too:-
2. Using the HomeBrew
This is the way which hard-core programmer prefer and it is also called the right way to install Python3.
Before installing Python 3 using this method, you will need GCC. The easiest way to install the same is by installing xcode by typing the following in the terminal:-
$ xcode-select --install
Once, you are done installing it, install homebrew using the following command:-
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Once, you are done you need to add PATH in your bash profile, this can be done by going to the home directory using ‘cd’ and then opening the ‘.bash_profile’ using some editor like nano
$ cd
$ sudo nano .bash_profile
This will open your ‘.bash_profile’ now at the very bottom add the following line:-
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
or
export PATH=/usr/local/bin:/usr/local/sbin:$PATH (If you are using MacOS Sierra or higher)
Now, the last command will install the latest stable version of Python 3 for you:-
$ brew install python
Check the installation of Python and pip using the commands described in the first method.
One of the advantages of installing the homebrew version of python is, you can easily update it using a simple command:-
$ brew upgrade python
Watch the video Tutorial
You can also watch the following video too:-
Installing on Linux
Ubuntu 16.10 or later version
Use the following command in your terminal/bash
$ sudo apt-get update
$ sudo apt-get install python3.7
Other versions of Ubuntu e.g. LTS release
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt-get update
$ sudo apt-get install python3.7
You can check for version installed and pip as under:-
$ python3 -V
or
$ python3 --version
$ command -v pip3
If pip3 is not installed follow the instructions here
One thing you need to note that your MacOS or Linux may have an older version of python installed in it if python -V
shows Python 2.7 instead of Python 3.7.2, you need to use python3 and pip3 to run the latest version of Python.