Tutorial

How To Install Python 3 and Set Up a Programming Environment on an Ubuntu Server

How To Install Python 3 and Set Up a Programming Environment on an Ubuntu Server
Not using Ubuntu 22.04?Choose a different version or distribution.
Ubuntu 22.04

Introduction

The Python programming language is an increasingly popular choice for both beginners and experienced developers. Flexible and versatile, Python has strengths in scripting, automation, data analysis, machine learning, and back-end development. First published in 1991 with a name inspired by the British comedy group Monty Python, the development team wanted to make Python a language that was fun to use.

This tutorial will get your Ubuntu 22.04 server set up with a Python 3 programming environment. Programming on a server has many advantages and supports collaboration across development projects. The general principles of this tutorial will apply to any distribution of Debian Linux.

Prerequisites

To follow this tutorial, you will need a non-root user with sudo privileges on an Ubuntu 22.04 server. To learn how to achieve this setup, follow our initial server setup guide.

If you’re not already familiar with a terminal environment, you may find the article An Introduction to the Linux Terminal useful for becoming better oriented with the terminal.

With your server and user set up, you are ready to begin.

Step 1 — Setting Up Python 3

Ubuntu 22.04 and other versions of Debian Linux ship with Python 3 pre-installed. To make sure that your versions are up-to-date, update your local package index:

  1. sudo apt update

Then upgrade the packages installed on your system to ensure you have the latest versions:

  1. sudo apt -y upgrade

The -y flag will confirm that you agree to all items being 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, check the version of Python 3 that is installed in the system by running the following:

  1. python3 -V

The output in your terminal window will let you know the version number. While this number may vary, the output will be similar to the following:

Output
Python 3.10.4

To manage software packages for Python, let’s install pip, a tool that will install and manage programming packages we may want to use in our development projects. You can learn more about modules or packages that you can install with pip by reading How To Import Modules in Python 3.

  1. sudo apt install -y python3-pip

Python packages can be installed with the following:

  1. pip3 install package_name

Here, package_name can refer to any Python package or library, such as Django for web development or NumPy for scientific computing. So if you would like to install NumPy, you can do so with the command pip3 install numpy.

There are a few more packages and development tools to install to ensure that you have a robust setup for your programming environment:

  1. sudo apt install -y build-essential libssl-dev libffi-dev python3-dev

Once Python is set up, and pip and other tools are installed, you can set up a virtual environment for your development projects.

Step 2 — Setting Up a Virtual Environment

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. This is especially important when working with third-party packages.

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. Let’s install venv by running the following:

  1. sudo apt install -y python3-venv

With this installed, 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 the following:

  1. mkdir environments

Then navigate to the directory where you’ll store your programming environments:

  1. cd environments

Once you are in the directory where you would like the environments to live, you can create an environment by running the following command:

  1. python3 -m venv my_env

Essentially, pyvenv sets up a new directory that contains a few items which we can view with the ls command:

  1. ls my_env
Output
bin include lib lib64 pyvenv.cfg

Together, these files work to ensure that your projects are isolated from the broader context of your server, so that system files and project files don’t mix. This is good practice for version control and to ensure that each of your projects has access to the particular packages that it needs.

To use this environment, you need to activate it, which you can achieve by typing the following command that calls the activate script:

  1. source my_env/bin/activate

Your command prompt will now be prefixed with the name of your environment, in this case it is called my_env. Depending on what version of Debian Linux you are running, your prefix may appear somewhat differently, but the name of your environment in parentheses should be the first thing you see on your line:

This prefix lets us know that the environment my_env is currently active, meaning that when we create programs here they will use only this particular environment’s settings and packages.

Note: Within the virtual environment, you can use the command python instead of python3, and pip instead of pip3 if you prefer. If you use Python 3 on your machine outside of an environment, you need to use the python3 and pip3 commands exclusively.

After following these steps, your virtual environment is ready to use.

Step 3 — Creating a “Hello, World” Program

Now that we have our virtual environment set up, let’s create a traditional “Hello, World!” program. This will let us test our environment and provides us with the opportunity to become more familiar with Python if we aren’t already.

To do this, open up a command line text editor such as nano and create a new file:

  1. nano hello.py

Once the text file opens up in the terminal window write out the program:

hello.py
print("Hello, World!")

Save the file and exit nano by pressing CTRL + X, Y, and then ENTER.

Once you exit the editor and return to your shell, you can run the program:

  1. python hello.py

The hello.py program that you created should cause your terminal to produce the following output:

Output
Hello, World!

To leave the environment, type the command deactivate and you will return to your original directory.

Conclusion

Now, you have a Python 3 programming environment set up on your Ubuntu Linux server and can begin a coding project!

If you are using a local machine rather than a server, refer to the tutorial that is relevant to your operating system in our How To Install and Set Up a Local Programming Environment for Python 3 series.

With your server ready for software development, you can continue to learn more about coding in Python by reading our free How To Code in Python 3 eBook, or consulting our Python tutorials.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Default avatar

Technical Writer

Educator and writer committed to empowering our community by providing access to the knowledge and tools for making creative ideas into a reality


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.