This post will guide you how to install the latest version of Python 3.7 on Ubuntu Linux 16.04 or 18.04. How to install Python3.7 from the source code under Ubuntu Linux operating system.
- Prerequisites
- What is Python?
- Method1: Installing Python 3.7 via PPA
- Method2: Installing Python 3.7 from Source Code
- Writing Your First Python Program
Prerequisites
- You need to have a non-root user with sudo privileges so that you can install packages on your Ubuntu system.
What is Python?
Python is an interpreted, high-level, general-purpose programming language. It is one of the most popular programming languages in the world. And It is very easy to learn for beginners. You can use python to develop websites, automation framework, or analyze data and many more. It can be do anything that you want so far. Especially, you can use it to write crawler scripts.
At writing time of this post, the latest stable version of Python3 is 3.7.3. And this new version give use many new features, including: Postponed evaluation of type annotations, async and await are now reserved keywords, New context vars and data classes library modules, New breakpoint function, and more. If you want to get more new features, just check out the What’s New in Python 3.7 page.
This post will show you how to install Python 3.7.3 via a third-party PPA and also show you how to install it from source code by manually.
Method1: Installing Python 3.7 via PPA
By default, the installed version of Python is 3.6.5 on your Ubuntu 18.04 Linux. And if you want to install the latest version of Python 3.7.3 on your Ubuntu system, you can install it from a third party PPA repository. Just do the following steps:
Step1: You need to install the software-properties-common package on your system. Normally, it should be installed. Type:
$ sudo apt update $ sudo apt install software-properties-common
Outputs:
devops@devops-VirtualBox:~$ sudo apt install software-properties-common
Reading package lists... Done
Building dependency tree
Reading state information... Done
software-properties-common is already the newest version (0.96.24.32.9).
0 upgraded, 0 newly installed, 0 to remove and 25 not upgraded.
Step2: Add the third-PPA repository to your default repository, type:
$ sudo add-apt-repository ppa:deadsnakes/ppa
Outputs:
devops@devops-VirtualBox:~$ sudo add-apt-repository ppa:deadsnakes/ppa This PPA contains more recent Python versions packaged for Ubuntu. Disclaimer: there's no guarantee of timely updates in case of security problems or other issues. If you want to use them in a security-or-otherwise-critical environment (say, on a production server), you do so at your own risk. Update Note =========== Please use this repository instead of ppa:fkrull/deadsnakes. Reporting Issues ================ Issues can be reported in the master issue tracker at: https://github.com/deadsnakes/issues/issues Supported Ubuntu and Python Versions ==================================== - Ubuntu 16.04 (xenial) Python 2.3 - Python 2.6, Python 3.1 - Python3.4, Python 3.6 - Python3.8 - Ubuntu 18.04 (bionic) Python2.3 - Python 2.6, Python 3.1 - Python 3.5, Python3.7 - Python3.8 - Note: Python2.7 (both), Python 3.5 (xenial), Python 3.6 (bionic) are not provided by deadsnakes as upstream ubuntu provides those packages. The packages may also work on other versions of Ubuntu or Debian, but that is not tested or supported. Third-Party Python Modules ========================== Python modules in the official Ubuntu repositories are packaged to work with the Python interpreters from the official repositories. Accordingly, they generally won't work with the Python interpreters from this PPA. As an exception, pure-Python modules for Python 3 will work, but any compiled extension modules won't. To install 3rd-party Python modules, you should use the common Python packaging tools. For an introduction into the Python packaging ecosystem and its tools, refer to the Python Packaging User Guide: https://packaging.python.org/installing/ Sources ======= The package sources are available at: https://github.com/deadsnakes/ More info: https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa Press [ENTER] to continue or Ctrl-c to cancel adding it. Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease Hit:2 http://mirrors.aliyun.com/ubuntu bionic-security InRelease Hit:3 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease Hit:4 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease Hit:5 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease Get:6 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease [15.9 kB] Hit:7 http://ppa.launchpad.net/flexiondotorg/minecraft/ubuntu bionic InRelease Get:8 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic/main amd64 Packages [26.8 kB] Get:9 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic/main i386 Packages [26.8 kB] Get:10 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic/main Translation-en [6,552 B] Fetched 76.1 kB in 5s (13.9 kB/s) Reading package lists... Done
Note: you need to press Enter to continue adding PPA.
Step3: Once PPA is added, you can install Python3.7 with the following apt command:
$ sudo apt update $ sudo apt install python3.7
Outputs:
devops@devops-VirtualBox:~$ sudo apt install python3.7 Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libpython3.7-minimal libpython3.7-stdlib python3.7-minimal Suggested packages: python3.7-venv python3.7-doc binfmt-support The following NEW packages will be installed: libpython3.7-minimal libpython3.7-stdlib python3.7 python3.7-minimal 0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded. Need to get 4,266 kB of archives. After this operation, 22.5 MB of additional disk space will be used. Do you want to continue? [Y/n] Y Get:1 http://mirrors.aliyun.com/ubuntu bionic-updates/universe amd64 libpython3.7-minimal amd64 3.7.3-2~18.04.1 [545 kB] Get:2 http://mirrors.aliyun.com/ubuntu bionic-updates/universe amd64 python3.7-minimal amd64 3.7.3-2~18.04.1 [1,701 kB] Get:3 http://mirrors.aliyun.com/ubuntu bionic-updates/universe amd64 libpython3.7-stdlib amd64 3.7.3-2~18.04.1 [1,736 kB] Get:4 http://mirrors.aliyun.com/ubuntu bionic-updates/universe amd64 python3.7 amd64 3.7.3-2~18.04.1 [284 kB] Fetched 4,266 kB in 2s (2,520 kB/s) Selecting previously unselected package libpython3.7-minimal:amd64. (Reading database ... 168086 files and directories currently installed.) Preparing to unpack .../libpython3.7-minimal_3.7.3-2~18.04.1_amd64.deb ... Unpacking libpython3.7-minimal:amd64 (3.7.3-2~18.04.1) ... Selecting previously unselected package python3.7-minimal. Preparing to unpack .../python3.7-minimal_3.7.3-2~18.04.1_amd64.deb ... Unpacking python3.7-minimal (3.7.3-2~18.04.1) ... Selecting previously unselected package libpython3.7-stdlib:amd64. Preparing to unpack .../libpython3.7-stdlib_3.7.3-2~18.04.1_amd64.deb ... Unpacking libpython3.7-stdlib:amd64 (3.7.3-2~18.04.1) ... Selecting previously unselected package python3.7. Preparing to unpack .../python3.7_3.7.3-2~18.04.1_amd64.deb ... Unpacking python3.7 (3.7.3-2~18.04.1) ... Processing triggers for mime-support (3.60ubuntu1) ... Processing triggers for desktop-file-utils (0.23-1ubuntu3.18.04.2) ... Setting up libpython3.7-minimal:amd64 (3.7.3-2~18.04.1) ... Setting up python3.7-minimal (3.7.3-2~18.04.1) ... Processing triggers for man-db (2.8.3-2ubuntu0.1) ... Processing triggers for gnome-menus (3.13.3-11ubuntu1.1) ... Setting up libpython3.7-stdlib:amd64 (3.7.3-2~18.04.1) ... Setting up python3.7 (3.7.3-2~18.04.1) ... devops@devops-VirtualBox:~$
Step4: Once installed, you can check the latest version installed of Python with the following command:
$ python3.7 -v
Or
$ python3.7 --version
You should see an output similar to the below output:
devops@devops-VirtualBox:~$ python3.7 -V Python 3.7.3
Method2: Installing Python 3.7 from Source Code
If you want to install the latest version of Python by manually on Ubuntu system from source code. And you need to download the source code of Python from their official website. Then compile and install it. Just do the following steps:
Step1: you need to install all required packages that are used to compile and install Python from source code. Running the following command to install all necessary packages:
$ sudo apt update $ sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev
Step2: downloading the latest version of source code of Python from the Python Official website using the following wget command:
$ wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
Outputs:
devops@devops-VirtualBox:~$ wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
--2019-06-18 10:34:56-- https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
Resolving www.python.org (www.python.org)... 151.101.228.223, 2a04:4e42:36::223
Connecting to www.python.org (www.python.org)|151.101.228.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 22973527 (22M) [application/octet-stream]
Saving to: ‘Python-3.7.3.tgz’
Python-3.7.3.tgz 100%[===================================================================>] 21.91M 56.6KB/s in 6m 33s
2019-06-18 10:41:30 (57.0 KB/s) - ‘Python-3.7.3.tgz’ saved [22973527/22973527]
Step3: extract the downloaded Python package with the following command:
$ sudo tar xzf Python-3.7.3.tgz
Step4: Changing the current directory to Python-3.7.3, and then compile Python source code using the following command:
$ cd Python-3.7.3 $ sudo ./configure --enable-optimizations $ sudo make altinstall
Note: you do not use the standard make install command to compile and install Python source code, as it will overwrite the default system python3 binary. make altinstall can be used to prevent replacing the default python binary file.
–enable-optimizations : Enable expensive, stable optimizations (PGO, etc).
Step5: Python 3.7.3 should be installed on your system, you can test if it is installed successfully by checking the latest version installed with the following command:
$ pythhon3.7 –version
Outputs:
Python 3.7.3
Writing Your First Python Program
You can create a new Python program with vim text editor, and then add the following codes:
$ sudo vim helloword.py #!/usr/bin/python3.7 print("hello world!");
Save and close the file. Then run your program on Ubuntu terminal:
$ sudo python3.7 helloworld.py
After executing, it produces the following output to the screen:
devops@devops-VirtualBox:~$ sudo python3.7 helloworld.py hello world!
Conclusion
You should know that how to install the latest version of Python3 on your Ubuntu or Debian Linux. If you want to see more detailed information about Python, you can directly go to its official web site.