In the following of Python 3 tutorials, in this article, we will learn you How to install Python 3 on Ubuntu 20.04. As you know the Python programming language is an increasingly popular choice for both beginners and experienced developers. To mention other features of this, we can say that flexible and versatile, Python has strengths in scripting, automation, data analysis, machine learning, and back-end development.
How to install Python 3 on Ubuntu 20.04
By reading this tutorial, you will get your Ubuntu 20.04 server set up with a Python 3 programming environment. We would show you the programming on a server has many advantages and supports collaboration across development projects and the general principles of this tutorial will apply to any distribution of Debian Linux.
The tutorial may be more useful if you know:
- a non-root user with sudo privileges
- To set up, follow our Initial server setup on Ubuntu 20.04
1- Setting Up Python 3
You just need to make sure that our versions are up-to-date, as the Ubuntu 20.04. And other versions of Debian Linux ship with Python 3 pre-installed.
To update and upgrade the system with the apt command to work with Ubuntu’s Advanced Packaging Tool:
sudo apt update sudo apt -y upgrade
Next, the -y flag will confirm that you are agreeing for all items to be installed, but depending on your version of Linux, you may need to confirm additional prompts as your system updates and upgrades. Once the process is complete, you can check the version of Python 3 that is installed in the system by typing:
You’ll receive output in the terminal window that will let you know the version number. While this number may vary.
Also, you can install pip, to manage software packages for Python.it is a tool that will install and manage programming packages we may want to use in our development projects.
sudo apt install -y python3-pip
You can install Python packages, using the following command:
pip3 install package_name
Then, you can use the package_name to refer to any Python package or library, such as Django for web development or NumPy for scientific computing. In case you want to install NumPy, you can do so with the command pip3 install numpy.
However, there are a few more packages and development tools to install to ensure that we have a robust set up for our programming environment:
sudo apt install -y build-essential libssl-dev libffi-dev python3-dev
2-Setting Up a Virtual Environment
In this step, we want to explain that Virtual environments enable you to have an isolated space on your server for Python projects, ensuring that each of your projects can have its own set of dependencies that won’t disrupt any of your other projects.
Setting up a programming environment provides greater control over Python projects and over how different versions of packages are handled, which is especially important when working with third-party packages.
Therefore, you can set up as many Python programming environments as you would like. Each environment is basically a directory or folder on your server that has a few scripts in it to make it act as an environment.
While there are a few ways to achieve a programming environment in Python, we’ll be using the venv module here, which is part of the standard Python 3 library.
To install venv:
sudo apt install -y python3-venv
After that, you are ready to create environments. Let’s either choose which directory we would like to put our Python programming environments in, or create a new directory with mkdir, as in:
mkdir environments cd environments
You are learning How to install Python 3 on Ubuntu 20.04
You can create an environment when you are in the directory where you would like the environments to live.
python3 -m venv my_env
Since, pyvenv sets up a new directory that contains a few items, which you can view this by using the following command:
bin include lib lib64 pyvenv.cfg share
You need to activate it to use this environment. By typing the command below it would be achieved.
Now, your command prompt will be prefixed with the name of your environment now. Here it is called my_env. our prefix may appear somewhat differently, depending on what version of Debian Linux you are running. And the name of your environment in parentheses should be the first thing you see on your line:
(my_env) [email protected]:~/ environments
When you see this prefix, it means that the environment my_env is currently active and creates programs where they will use only this particular environment’s settings and packages.
Important: Within the virtual environment, you can use the command python instead of python3, and pip instead of pip3 if you would prefer. If you use Python 3 on your machine outside of an environment, you will need to use the python3 and pip3 commands exclusively.
3- Creating a “Hello, World” Program
This will let you test your environment. And provides us with the opportunity to become more familiar with Python if you aren’t already.
To do this, open up a command-line text editor such as nano and create a new file:
(my_env) [email protected]:~/ environments nano hello.py
Once the text file opens up in the terminal window, type out your program:
Then, by pressing CTRL and X keys to exit nano. To save the file, press y when you are asked.
Now, run the following command when you exit out of nano and return to your shell.
(my_env) [email protected]:~/ environments python hello.py
The hello.py program that you just created should cause your terminal to produce the following output:
You must type the command deactivate and you will return to your original directory to leave the environment.
Good job! At this point, you have a Python 3 programming environment set up on your Ubuntu Linux server. So you can now begin a coding project!
Dear user, we hope you would enjoy this tutorial How to install Python 3 on Ubuntu 20.04, you can ask questions about this training in the comments section, or to solve other problems in the field of Eldernode training, refer to the Ask page section and raise your problems in it.
Do not miss some related articles: