How to Install Python on Ubuntu

As a favorite among novice and intermediate developers, Python is a powerful programming language and very versatile. The simplicity of the language, it’s endless libraries, and the extensive community support makes it a reliable option for almost any project, including website development, data science, machining learning, automation, and many other fields. These reasons are precisely why Python is loved by those interested in coding. A user can easily download Python on their Windows systems or Linux distributions.

In this article, we delve into the details of installing Python on Ubuntu, one of the distributions of Linux, providing step by step instructions. This post serves as an initiation point for developers who are just beginning their coding adventure alongside tailored guides.

Installing Python in Ubuntu

Guaranteeing success in the installation steps we are going to cover, check the Ubuntu system for a preexisting installation of Python. The terminal can be opened from the top, and you may use the following commands:

python --version

Upon installation, the terminal will present the user with the version number Python has been installed. In the case of a no installation error, you will receive the message “command not found” in which case, it will be necessary to install Python.

Installing Python 3

Python 3 is the current stable release of Python programming language. For this post, we will use Python 3 as the default. There are three primary methods for installing Python 3 on Ubuntu:

  1. Using the Advanced Package Tool (APT):

This is the recommended method for most users. APT provides a simple smooth and convenient way to install and manage software packages, including Python. For this open the terminal and type the following command:

sudo apt install python3

The sudo command grants administrative privileges for the installation. Enter your password when prompted. The system will download and install Python 3.

  1. Using the Personal Package Archive

This method allows you to install specific Python versions, including newer ones. It involves adding a Personal Package Archive (PPA) provided by the Deadsnakes project. For this add the repository with the following command in the terminal:

sudo add-apt-repository ppa:deadsnakes/ppa

After adding the repository, Update the package with the following command:

sudo apt update

After the update, you can install Python 3 along with your desired version. For example, if you want to use the 3.11 version you can type the following command:

sudo apt install python3.11
  1. Installing from source code

You can install Python by visiting its official website and downloading its binary for Ubuntu. You can find the latest version by downloading the compressed file for Ubuntu. After downloading place the folder in the directory and access them from the terminal.

cd ~/
sudo ./configure
make
make test

After building the essentials, Use the APT command to install Python.

Verifying the Installation:

Once the installation is complete, verify it by checking the version again. The terminal should display the installed Python version.

Managing Python in Ubuntu

While Python is now installed, it’s recommended to create a virtual environment for your projects. This isolates your project’s dependencies from the system’s Python environment, preventing conflicts and ensuring clean installations. To create a virtual environment, run the following command:

python3 -m venv my_venv

This creates a virtual environment named my_venv. To activate it, type:

source my_venv/bin/activate

Your terminal prompt will now indicate the active virtual environment. To deactivate it, type:

deactivate

Writing Your First Python Program

Now that Python is installed and your environment is set up, you can write your first Python program! Open a text editor and create a file named hello.py. Inside the file, write the following code:

print("Hello, World!")

Save the file and open a terminal window. Navigate to the directory containing the file and run the following command:

python3 hello.py

You should see the message “Hello, World!” displayed on your screen. Congratulations, you’ve successfully written and run your first Python program!

Additional Configuration

Python comes with a built-in package manager called PIP for installing and managing third-party libraries. Install PIP by running the following command:

sudo apt install python3-pip

If you want to set Python 3.11 as the default type the following command:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1

You can also run Python directly from any directory. For this, Add the path to your Python installation directory to the PATH environment variable.

Modifying your shell configuration file

  1. Open your shell configuration file using your preferred text editor. For Bash shells, this is usually ~/.bashrc or ~/.bash_profile.
  2. Add the following line to the end of the file:
export PATH="$PATH:/path/to/python/bin"

Replace /path/to/python/bin with the actual path to your Python installation’s bin directory. For example, if you have Python 3.11 installed, this path might be /usr/bin/python3.11/bin.

  1. Save the file and close the editor.
  2. To apply the changes, source the shell configuration file by running the following command:
source ~/.bashrc

Important Notes

  • You can add multiple directories to your PATH by separating them with colons.
  • Be cautious when modifying your PATH environment variable, as mistakes can lead to unexpected behavior.
  • Consider using virtual environments for your projects to manage dependencies effectively and maintain a clean system environment.

Conclusion

Installing Python on Ubuntu is a straightforward process. By following the steps outlined in this article, you can be up and running with this powerful and versatile programming language in no time. You can explore the various resources available online to enhance your learning and start building projects.

You can install Python on the Linux system. For this, you need a powerful platform to host your Python projects Look no further than Ultahost’s Linux VPS hosting! Our VPS plans offers ensure fast loading times for your applications and easily upgrade your resources as your needs grow. Get started today and enjoy the freedom and flexibility of a VPS!

Related Post

How to Extract .tar.gz Files in Linux

The "tar.gz" file format is a common archive format use...

How to Fix the “wget: command not found...

wget is a widely renowned command-line utility in Linux...

How to Install Visual Studio Code on Linux

Visual Studio Code (VS Code) is a widely used code edit...

How to Check Running Processes in Linux?

In Linux, you can efficiently manage your system by che...

How to Fix Unable to Locate Package in Kali L...

Kali Linux is widely known as an operating system used ...

How to Install Odoo on Ubuntu

Odoo, the powerful open-source enterprise resource plan...

Leave a Comment