How to install Python 3 on Ubuntu 22.04 and set up a programming environment?
Overview
Python is a programming language that is gaining popularity among both novice and experienced programmers. It is one of the most widely used programming languages in the world. Its syntax is easy to learn, and its flexibility and versatility make it suitable for a wide range of applications, from web development and data analysis to artificial intelligence and scientific computing. Python's extensive libraries and frameworks also make it a popular choice among developers as it allows for rapid development and quick prototyping. Overall, Python's ease of use, powerful features, and large community of developers have contributed to its popularity and continued growth.
This tutorial will help you create a Python 3 programming environment on an Ubuntu 22.04 server.
Prerequisites
There are certain prerequisites that need to be met before you begin.
Ubuntu 22.04 LTS configured on your system.
Non-root sudo user privileges.
Get Started
Step 1: Configuring Python 3
Python 3 is already installed in Ubuntu 22.04 by default. To ensure that you have the latest version, you need to update your local package index:
sudo apt update
Next, update the software packages present on your system:
sudo apt -y upgrade
Using the -y flag will indicate your consent to install all items, however, additional prompts may need confirmation during system updates and upgrades, which could vary depending on the Linux version you're using.
Check Python 3 version:
python3 -V

In order to handle programming packages for Python, we can utilize pip, a tool that can install and oversee the various software packages that we may require for our development endeavors:
sudo apt install -y python3-pip

You can now install any python package. Here, we have installed numpy using the syntax: 'pip3 install package_name'
pip3 install numpy

Install additional packages and development tools to create a reliable programming environment:
sudo apt install -y build-essential libssl-dev libffi-dev python3-dev

After installing Python, pip, and other necessary tools, you can create a virtual environment to work on your development projects.
Step 2: Creating a Virtual Environment
Virtual environments allow for a segregated area on a server for Python projects, guaranteeing that each project has its own distinct set of dependencies that won't interfere with other projects. By creating a programming environment, you can exercise more authority over Python projects and how different package versions are managed, which is particularly significant when working with external packages. It's possible to establish multiple Python programming environments, with each environment being a folder or directory on the server that is equipped with a few scripts to function as an environment.
Utilize the venv module that comes pre-installed in the standard Python 3 library to set up a programming environment using Python.
To install venv, execute the following command:
sudo apt install -y python3-venv

Now that you have installed the necessary components, you can setup environments. You can either select the directory where you want to store your Python programming environments or use the mkdir command to make a new directory:
mkdir environments
Enter into the directory:
cd environments
After navigating to the created directory, generate an environment:
python3 -m venv p_env
pyvenv creates a new directory that comprises several elements that can be observed using the "ls" command:
ls p_env

The files displayed in the above output collaborate to create a secure environment for your projects by separating them from other files on your server. This helps in version control and guarantees that each project has access to specific packages it requires.
To start using this environment, you must activate it by using the following command:
source p_env/bin/activate
(p_env) ubuntu@testing-1:~/environments$
The prefix indicates that the currently active environment is "p_env", indicating that any programs created here will utilize only the specific settings and packages of this environment.
Once you have completed these procedures, your virtual environment is prepared for utilization.
Step 3: Testing
After configuring our virtual environment, let's proceed with generating a conventional "Hello, World!" program to check the environment's functionality.
Launch a command prompt text editor like nano and initiate the creation of a fresh file.
nano file.py
Write out the program in the file:
print("Hello, World!")

To finish with nano, press CTRL + X followed by Y, and then ENTER to save the file and exit the program.
Execute the program:
python file.py

Conclusion
You have successfully established a Python 3 programming environment and are now ready to initiate a programming project. Also see How to install Python on Windows 10 using UI and Windows Command prompt.
Last updated
Was this helpful?