How to install Python 3 on Ubuntu 22.04 and set up a programming environment?
Last updated
Last updated
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.
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.
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:
Next, update the software packages present on your system:
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:
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:
You can now install any python package. Here, we have installed numpy using the syntax: 'pip3 install package_name'
Note: The term "package_name" can represent any Python package or library.
Install additional packages and development tools to create a reliable programming environment:
After installing Python, pip, and other necessary tools, you can create a virtual environment to work on your development projects.
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:
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:
Enter into the directory:
After navigating to the created directory, generate an environment:
pyvenv creates a new directory that comprises several elements that can be observed using the "ls" command:
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:
Note: The name of the environment is set as "p_env" in this case and will be added as a prefix to the command prompt. The appearance of the prefix may differ, but the name of your environment should be the first thing you see on your command line.
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.
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.
Note: Here 'file.py' is the name of the file with the extension py. You can set the name of the file as per your requirements:
Write out the program in the file:
To finish with nano, press CTRL + X followed by Y, and then ENTER to save the file and exit the program.
Execute the program:
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.