List of content you will read in this article:
- 1. What is Python? [Definition]
- 2. Prerequisites
- 3. Configuring Python Environment Variables
- 4. What Version Would Users Require?
- 5. How to Install Python on Ubuntu?
- 6. Updating Python to the Latest Version
- 7. Managing Multiple Python Versions
- 8. What is the Most Efficient Way to Execute Python Code?
- 9. How to Uninstall Python on Ubuntu?
- 10. Security Considerations
- 11. Installing Additional Python Packages
- 12. Conclusion
Python is among the most popular languages with a wide range of applications, from software and web development to data analysis and visualization. Many professionals and students mostly prefer learning Python as it opens up various job opportunities.
If you are among the individuals wishing to learn and practice Python, you will definitely need Python installed on your computer system. But what if you want to practice Python on Ubuntu? Don’t worry! This article will guide you on how to install Python on Ubuntu.
Though most Linux distributions come with Python in the default system packages, you may not find it for some reason.
We will help you with our guide on installing Python on Ubuntu 18.04. However, before jumping straight into the tutorial, let us introduce you to the renowned programming language.
What is Python? [Definition]
Python is an open-source programming language developed in 1991 by Von Hansen in Holland. It is a general-purpose, high-level, and object-oriented scripting language.
The major design philosophy of the language emphasizes code readability. Also, the language has an elegant and simple syntax that uses minimal English keywords. This makes it an easy-to-learn language. Besides ease of use, Python comes with a comprehensive library, and hence sometimes referred to as batteries included.
Unlike other common programming languages, where code blocks are defined in the parenthesis, Python uses whitespace indentation to specify blocks of code. This makes Python code look uncluttered and readable. Indentation means many identical spacing characters at the beginning of each block's lines, and this number increases in the inner code blocks. This way, blocks of code are automatically organized.
Python isn't pre-installed on Windows, although it doesn't rule out the possibility that Windows users will find the lightweight programming language helpful. It's not as simple as upgrading to the most recent version, so be sure you've got the tools for the task.
Prerequisites
Before you begin the process of installing Python on your Ubuntu system, it's essential to ensure that you meet the following prerequisites:
- Ubuntu System: You must have a computer running Ubuntu as your operating system. This installation guide is intended for Ubuntu users, so please make sure you have an Ubuntu-based distribution installed. It's recommended to use Ubuntu 18.04 or Ubuntu 20.04 for compatibility.
- Sudo Privileges: You need to have a user account with sudo privileges. Sudo access allows you to execute commands with administrative permissions, which is necessary for installing software and updating system packages.
- Terminal Access: You should have access to a terminal window or command-line interface (CLI). Throughout this installation process, you'll be using the terminal to execute commands. You can open a terminal window using the keyboard shortcut "Ctrl + Alt + T."
Configuring Python Environment Variables
Customizing Python's environment variables allows you to tailor its behavior to your specific needs. Here, we'll explore two crucial environment variables: PATH and PYTHONPATH.
- PATH Variable: The PATH environment variable specifies directories that the system should search for executable files. Modifying it can help you control which Python interpreter is used and where Python-related scripts are located.
To add a custom Python installation or scripts directory to your PATH, edit your shell's profile configuration file (e.g., ~/.bashrc or ~/.zshrc) and append the following line, replacing /path/to/your/python with the desired Python path:
export PATH="/path/to/your/python:$PATH"
Save the file and run source ~/.bashrc (or source ~/.zshrc) to apply the changes.
- PYTHONPATH Variable: The PYTHONPATH environment variable specifies additional directories where Python should look for modules and packages. It's useful for managing your own Python modules or overriding the default module search path.
To add a directory to your PYTHONPATH, open your shell's profile configuration file and add a line like this:
export PYTHONPATH="/path/to/your/python/modules:$PYTHONPATH"
Replace /path/to/your/python/modules with the directory containing your Python modules or packages. Save the file and run source ~/.bashrc (or source ~/.zshrc) to activate the changes.
What Version Would Users Require?
Regrettably, a major upgrade to Python many years ago resulted in a large break among Python releases. It may be a little perplexing for beginners but don't worry. We'll demonstrate how to set up all of the major ones.
You'll notice the separation immediately when you go to the Python for Windows downloads tab. The registry always asks whether you want the latest Python 2 or Python 3 upgrade at the core, square, and center. Installing and running both versions simultaneously is recommended to get the most out of Python. However, if you already know which version you need for your goals, then stick with that one.
However, using the latest stable version of Python is always better, as it comes with bug fixes, new features, and improvements to existing features.
How to Install Python on Ubuntu?
As stated above, most Linux distributions come with Python in their system packages. So, let us first see whether your Ubuntu system has Python installed or not. If yes, we shall also check its version.
How to Check if Python Is Installed on Ubuntu
If you're unsure whether Python is already installed on your Ubuntu system, follow these steps to check:
- Open the Terminal: Press Ctrl + Alt + T to open the terminal. The terminal is where you can interact with your system and run commands.
- Type the Python Command: In the terminal, type the following command and press Enter:
python3
This command launches the Python interpreter. If Python is installed, you will see something like this:
Python 3.12.0b3 (main, June 29 2023, 17:44:14) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
The output includes the Python version and some additional information.
- Interact with the Python Interpreter: If you see the Python prompt (>>>), it means Python is installed. You can interact with Python directly from the terminal by entering Python commands. Type exit() or press Ctrl + D to exit the Python interpreter.
- Check Python Version: You can also check the Python version from the terminal without entering the Python interpreter. Simply run the following command:
python3 --version
This command will display the version of Python currently installed on your computer.
If, instead of seeing the Python interpreter, you encounter an error message that says "bash: python3: command not found," it indicates that Python is not installed on your system.
Install Python on Ubuntu Using APT
APT stands for Advanced Package Tool. It is a default package manager of Ubuntu. This package manager comes with a Python package that you can download and install on your system. To do so, follow the below steps:
- Press Ctrl + Alt + T to open the terminal window.
- Type the following command to update the repository list of the local system.
sudo apt update
- Use the following command to download the latest Python version.
sudo apt install python3
When you run this command, APT will automatically find the Python package and install it on your system.
Install Python 3 on Ubuntu Using Deadsnakes PPA
Sometimes, the above method of installing Python may not work. If this is the case, you can install Python 3 using the Deadsnakes PPA.
The Deadsnakes PPA allows you to install multiple versions of Python Ubuntu. It consists of more recent Python versions compiled for Ubuntu. Meanwhile, PPA is an acronym for Personal Package Archives. It is a repository specially designed for Ubuntu.
You can add the Deadsnakes PPA to your system’s repository list and install Python. To do so, follow the below steps. However, it is important to note that you cannot directly add the Deadsnakes PPA to your system’s repository. You first need to download the "software-properties-common" package. This package helps you add PPAs on Ubuntu.
- Download the "software-properties-common" package on your system by using the following command:
sudo apt install software-properties-common
- Now, add the official link of the Deadsnake PPA to your system’s repository using the following command.
sudo add-apt-repository ppa:deadsnakes/ppa
- Update your system’s packages.
sudo apt update
- Now, download the latest version of Python on Ubuntu.
sudo apt install python3
Install Python 3 on Ubuntu From the Source Code
Visit the official website and navigate to the “Downloads” section to get Python. You can find out the most recent Python version for Ubuntu. Go ahead and download it.
Untar the file after downloading the tarball. You will see a couple of directories after entering the file. You'll want to look at the readme file because it contains instructions for installing Python on an Ubuntu VPS.
Please note: All the commands written below must be entered with root access.
Adjust the directory of the untarred Python file and execute the command below in the terminal.
cd ~/
Install python command:
sudo ./configure
make
make test
sudo make install
It will make the installation of Python as Python3.
If you get an error message or warning when running sudo ./configure, such as no compiler found, install the below library to get rid of it:
apt-get install build-essential
Also, if you get an error message or warning when running, like make not found, j run the below command to install make:
sudo apt install make
After you've installed the above libraries, such as create and build-essential, you should be able to use the install python command.
Another way to install Python is to use the apt-get commands as seen below:
In the terminal, type the following commands:
sudo apt-get update
It will ensure that Ubuntu's repositories are up to date. Then, run the following command to install Python 3:
sudo apt-get install python3
Updating Python to the Latest Version
Keeping your Python installation up to date is crucial to access the latest features and security updates. To update Python to the latest version, follow these steps:
- Update Package Lists: Open a terminal and run the following command to update your system's package lists:
sudo apt update
- Upgrade Python: To upgrade Python to the latest version, use the following command:
sudo apt install python3
This command ensures that you have the most recent Python version installed on your system.
Alternatively, you can use the --only-upgrade flag to upgrade Python without reinstalling:
sudo apt --only-upgrade install python3
This method is beneficial if you have previously installed Python using APT.
Managing Multiple Python Versions
If you need to work with multiple Python versions on your system, Ubuntu allows you to do so. Here's how to manage different Python versions:
- Using Python 2.x: If you have Python 2.x and Python 3.x installed, you can run commands for Python 2.x using the python command. For example:
python --version
- Using Python 3.x: To run commands using Python 3.x, use the python3 command. For example:
python3 --version
- Specifying Python Version: If you have multiple Python 3.x versions installed (e.g., Python 3.7.x and Python 3.8.x), you can specify which version to use by including the version number with the python3 command. For instance:
python3.7 --version
python3.8 --version
These steps allow you to work with different Python versions depending on your project requirements.
What is the Most Efficient Way to Execute Python Code?
Simply run the following command to run Python code.
python
Installation of Extra Python Packages
Run the command pip3 list to watch what modules are installed in the application. It will show a list of all the modules currently installed in the environment. If you choose to install additional modules in the environment, such as the request library, you can do so with pip3 install requests. Now, run the pip3 list to see if this request lib is installed in this setting.
Directory as a Package for Distribution
Within the Python project or directory, a file named init.py is used as a delivery kit. As a result, you can make this file with a single-touch button. This file does not need to contain any data; it simply needs to be present in the directory to function as a package.
Utilizing Various Python Versions
You’d have two separate versions of Python on your framework if you used the alt install tool. Each installation comes with its own set of instructions.
Execute commands on every older Python 2.x version on your machine with the Python button. Consider the following scenario:
python --version
Using python3 to execute a script in the newer version. Consider the following scenario:
python3 --version
You can install different major (3.x or 2.x) versions of Python on your device. Use the second digit to specify which version you choose to use if you have both Python 3.7.x and Python 3.8.x installed:
python3.7 --version
python3.8 --version
How to Uninstall Python on Ubuntu?
In case you no longer wish to have Python installed on your Ubuntu 18.04 system, here’s what you need to do:
First, find out what version of Python you have installed by typing:
python -v
Let’s say that you have Python 3.9 installed. You can uninstall it using the following command:
sudo apt autoremove python3.9
Security Considerations
When working with Python on your Ubuntu system, it's essential to keep security in mind. Python is a powerful language with a vast ecosystem of packages and libraries, but it's crucial to take precautions to ensure the safety and integrity of your Python environment. Here are some essential security considerations:
- Keep Python and Packages Updated: Regularly update Python and its packages to patch vulnerabilities.
- Use Virtual Environments: Isolate projects with virtual environments to prevent conflicts and ensure security.
- Choose Trusted Packages: Select reputable packages from PyPI and review documentation.
- Check for Vulnerabilities: Use tools like safety to scan for known vulnerabilities in dependencies.
- Avoid Untrusted Code: Don't run untrusted Python code to prevent security breaches.
- Secure Environment Variables: Safeguard sensitive data stored in environment variables or configuration files.
- Follow Secure Coding Practices: Implement secure coding practices to minimize vulnerabilities in your code.
Installing Additional Python Packages
Python's package manager, pip, simplifies the process of installing external Python packages and libraries. Whether you need additional modules for data analysis, web development, or other tasks, pip has you covered. Here's how to use it:
- Basic Installation: To install a package, open your terminal and use the following command, replacing package_name with the name of the package you want to install:
pip install package_name
For example, to install the popular NumPy package for numerical computations, run:
pip install numpy
- Installing Specific Versions: You can specify a particular version of a package by adding == followed by the version number. For instance, to install Matplotlib version 3.4.1:
pip install matplotlib==3.4.1
- Upgrading Packages: Keep your packages up to date by running:
pip install --upgrade package_name
For example, to upgrade the requests library:
pip install --upgrade requests
- Commonly Used Packages: Here are some commonly used Python packages you might want to install:
- requests: For making HTTP requests.
- pandas: A powerful data analysis and manipulation library.
- beautifulsoup4: Used in web scraping for parsing HTML and XML.
- Django or Flask: Web frameworks for building web applications.
- scikit-learn: Machine learning library for data modeling and analysis.
- matplotlib: A popular library for creating data visualizations.
You can install any of these packages using the pip command followed by the package name.
Remember to maintain a clean and organized Python environment by creating virtual environments for different projects. This ensures that installed packages are isolated, reducing conflicts and ensuring your Python setup stays efficient and reliable.
Conclusion
This was all about installing Python on Ubuntu using different methods. We recommend running all versions simultaneously on Ubuntu if you want to get the most out of Python. It helps us use the most modern Python version while operating older Python scripts and testing functionality for newer projects. However, if you're sure you only need one Python version, you may install just Python 2 or Python 3.
Also, if you wish to use Python on Windows instead, check out our detailed tutorial on how to install Python on Windows.
One OF my major goals is getting new experiences about ICT and what’s more making progress through this field.