Install Python 3 on Ubuntu

Python is an object-oriented, interpreted, high-level programming language created by Guido van Rossum and was first released in 1991. It reduces the cost of program maintenance with its easy to learn syntax and high user readability. It encourages program modularity and thus code reuse by supporting modules and packages based programming concept. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms and can be freely distributed.

Programmers often prefer Python over other languages as in Python there is no separate compilation step. This increases the productivity for programmers as the edit-test-debug cycle becomes pretty fast. Python just seems to be getting more and more popular with Linux developers and is arguably the best general-purpose language currently available. So as Linux users, you need to get a hold of how to install it and start writing your Python applications.

In this article, we will install the latest version of Python3 on our Ubuntu system and then set up a virtual programming environment where you can write and execute your Python application programs. The article will also help you in writing and running your first Python program, that will get you started with developing your own complex Python applications.

We have run the commands and procedures mentioned in this article on an Ubuntu 18.04 LTS system.

We are using the Ubuntu command line, the Terminal, for installation and set up a virtual programming environment. You can open the Terminal either through the system Dash or the Ctrl+Alt+T shortcut.

Check Current Python Version

Checking the current version of a software not only helps you get the version number of that software installed on your system but also verifies if the software is indeed installed on your system. We will do the same for Python by running the following command in our Terminal:

$ python3 -V

or

$ python3 --version

Test which Python version is installed on the server

The version number will appear as shown in the above output, depending on when you updated your system.

You might also have several versions of Python installed on your system. The following command will help you get a list of all Python versions you have on your system:

$ apt list --installed | grep python

Check Python version with apt

Install Python through apt-get

Installing Python through the apt-get command is pretty simple. First, you need to update your system repository index with that of the Internet so that the latest available version can be installed. Run the following command as sudo in order to do so:

$ sudo apt-get update

Update Ubuntu package lists

Since we already have Python installed on our system, as verified in the previous section, we only need to upgrade it to the latest version as follows:

$ sudo apt-get upgrade python3

Upgrade python

The system might ask you the password for sudo as only an authorized user can add/remove and upgrade software on Ubuntu.

The system will also prompt you with a y/n option in order to confirm the upgrade; please enter Y and then hit Enter to continue.

The latest available version of Python will now be installed on your system.

Now when you check the version number of Python, you will see an updated installation:

Check python version

In case you did not have Python installed in the first place, you can install it as sudo through the following command after running apt-get update:

$ sudo apt-get install python3

Manually Install Python from Source

Python’s website maintains a list of all Python releases on this link:

https://www.python.org/downloads/source/

So if you choose to install Python manually through the source, you have the freedom to install whichever build you want to choose. The website also contains the latest versions that you can not even get through the apt-get command.

We visited the website to see that Python-3.7.1 was the latest available version, so we will download its .tgz file through the following command:

$ wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tgz

Download Python source

When the file download is complete, please run the following command in order to extract the resources:

$ tar -xvf Python-3.7.1.tgz

Unpack Python archive

Once the resources are extracted, you need to run the c program “configure” to check the built. For this, you need to have the C compiler gcc installed on your system. If you do not have it available, please install it through the following command:

$ sudo apt-get install gcc

Change the directory to Python-3.7.1, or to whatever download version you have extracted:

$ cd Python-3.7.1

Now run the following command to run the configuration script:

$ ./configure

Configure python

Now is the time to install Python.

$ make

If you can not run the make command, you might need to install make through the following command:

$ sudo apt-get make

Run make command to build Python 3

Also, run the following command for Python installation:

$ sudo make install

The downloaded version of Python from the website will be installed on your system.

Errors that might be encountered during installation

Error 1

When you run the “sudo make install” command, you might encounter the following error:

Pythin install error resolved

This would mean that a package named zlib1g-dev is missing from your system as you might have never needed it before.

Solution:

Run the following command as sudo in order to install the missing zlib1g-dev package:

$ sudo apt install zlib1g-dev

Then run the following command in order to complete the Python installation:

$ sudo make install

Error 2

When might also get the following error when you run the “sudo make install” command:

No module named _ctypes

This would mean that a package named libffi-dev is missing from your system as you might have never needed it before.

Solution:

Run the following command as sudo in order to install the missing libffi-dev package:

$ sudo apt-get install libffi-dev

Then run the following command in order to complete the Python installation:

$ sudo make install

Upgrade Python to The Latest Version

Before manually installing Python from the source, the version number of our Python installation was 3.6.7

When I checked the version number of Python3.7, it gives the following output:

$ python3.7 -V

Python -V command

Since I want to upgrade the version of Python3 to this installed version, I will run the following command:

$ sudo apt-get upgrade python3

Now you can see that the updated Python version on my system is 3.7.1; the one that I installed manually from the source.

Upgraded Python version

Setup Virtual Programming Environment for Python3

First, let us get familiar with what is a Virtual Programming Environment for Python projects. You can assume it as an isolated space on your system where you can create Python projects having their own set of dependencies that do not affect anything outside of the project. When you are inside this environment, you can make use of Python and pip commands directly instead of using pip3 and Python3 commands. However, outside of this environment, you will have to use the pip3 and Python3 commands to develop and run your applications.

here the step by step procedure for you to create and activate a new virtual programming environment for Python:

Step 1: Install the Prerequisites

Before installing pip, you will need to add a few prerequisites that will help you in setting up your virtual space. Run the following command as sudo in order to install the build-essential, libssl-dev, libffi-dev and python-dev packages to your system:

$ sudo apt-get install build-essential libssl-dev libffi-dev python-dev

Install prerequisites for Python virtualenv

Please click Y and then hit Enter when the system prompts you with a y/n option to continue installation.

All these packages will then be installed to your system.

Step 2: Install pip3 if it is already not installed on your system

You can verify if pip3 is installed on your system or not simply by checking its version number. Please run the following command to check the version:

$ pip3 -V

Check pip version

The above output shows that pip 10.0.1 is already installed on my system.

If your output suggests that pip is not installed on your system, please run the following commands as sudo to install the latest pip3 package:

$ sudo apt-get update

And then,

$ sudo apt install python3-pip

Now that pip3 is installed on your system, you can install any pip package by using the following command syntax:

$ pip3 install [package-name]

Step 3: Create a virtual environment through Python3-venv

In order to create the virtual environment, you need the Python3-venv package installed on your system. Please run the following command as sudo in order to install it:

$ sudo apt install -y python3-venv

Install Python3 venv

Now we will create a folder for your Python virtual environments where you can create your stand-alone virtual environments. You can use the following syntax to create your own working directory:

$ mkdir [environment_dir_name]

Example:

$ mkdir environment_directory

Now change the working directory to the environments directory that you just created:

$ cd environment_directory

Create directory for virtual Python environment

In the environments directory, we will be creating a new virtual environment where you can write your Python programs and create projects.

Syntax:

$ python3 -m venv environment_name

Example:

$ python3 -m venv sample_environment

When you list the contents of your Python environment through the ls command, you will be able to see the following basic contents:

bin include lib lib64 pyvenv.cfg

Example:

$ ls sample_environment

List content of virtualenv

This means that your environment is successfully set up.

Step 4: Activate the Python Virtual Environment

When you want to use the newly created virtual environment, you first need to activate it. Use the following command to syntax to do so:

Syntax:

$ source environment_name/bin/activate

Example:

$ source sample_environment/bin/activate

Activate the Python Virtual Environment

When you activate the environment, you will see how your environment name appears inside brackets, suggesting that you are now inside the environment.

Whenever you want to deactivate the environment, you can use the following command:

$ deactivate

Deactivate virtualenv

This will deactivate the virtual environment and you can work outside of it.

Your First Python Program

You can create and run your first Python program both inside and outside of the Virtual Working environment. In this example, we will tell you how to write a sample Python program inside the virtual environment you just created.

In order to get inside the environment, first change the directory to your environments folder and then activate whichever virtual environment you want to activate.

Once you are inside the virtual environment, you can use your favorite text editor to create your first Python program. In this example, we are using the Nano editor to create a program.

$ nano first_program.py

This command will open a blank text file by the name of first_program.py

Write or paste the following line in your first Python program:

print("This is my first Python program :) :)")

First Python program

Save the file by hitting Ctrl+X, then entering Y and hitting Enter. Your program is now saved in your virtual environment.

Run the following command in order to execute the Python program:

$ python [program_name.py]

Example:

$ python [first_program.py]

Run python app

You can then deactivate the environment. Please remember that when you want to execute this program outside the virtual environment, you might have to use the Python3 commands instead of Python commands.

Conclusion

Most versions of Ubuntu already have Python and Pip3 installed in them but after reading this article you will know how to download and upgrade to the latest versions of each. You have also learned how to create your own Python virtual environment where you can write your independent Python programs and projects. Hope your first program will serve as a basis for you to move to more useful and complex Python applications. Happy programming!

Install Python3 on Ubuntu 18.04 and Set Up a Virtual Programming Environment