« Back to Blog

Setting Up Your Own Python on a Computer

Mar 23, 19  python

I can’t count the times that I’ve gotten access to a shared memory server with an out of date version of CentOS and Python 2.7 as the only python installation. Okay, actually, I can and it’s been eight times. The same goes for getting a new laptop or computer that you use personally and realizing that you don’t want to mess with the local python installation that the operating system is dependent on. I finally have decided to write my authoritative guide to setting up your own python installation on the computer of your choice.

Getting Python

We’re past Python 3.7.1, but I still prefer to stick with the tried and true Python 3.5 version. I have found a lot of science modules are not exactly on the cutting edge either, so this ensures compatibility. The first thing you need to do is download the Python file from the Python Software Foundation and uncompress it:

wget https://www.python.org/ftp/python/3.5.7/Python-3.5.7.tar.xz
tar -xvf Python-3.5.7.tar.xz

This is going to uncompress all the Python files in a directory named Python-3.5.7. We should rename this directory and stick it somewhere that it will not interfere or be interfered with. I like to use the /usr/local/mypython directory, but it’s your call on where to go. Assuming you have the name, you will need to actually move and rename it:

mv Python-3.5.7 /usr/local/mypython
cd /usr/local/mypython

Installing Your Master Python

Now we can begin. We want to install all of the custom python files in this directory–again so that it doesn’t interefere with things–so we decide to add a built subdirectory. We need to specify this when we configure the makefile for installation using the prefix flag.

mkdir built/
./configure --prefix=/usr/local/mypython/built

It is time to compile our files. Once you issue this command, it is a good time to put the kettle on or start a load of laundry.


The last step is much shorter. We have compiled the files and now we need to copy them to our installation directory.

make install

If you navigate to /usr/local/mypython/built/bin, you should now see your own set of pythons!

Installing Your Python Environment

You may or may not have noticed that I used “Master Python” in the prior section. This is because I like to configure my Python that I am using for coding to be different than my master or template Python installation. This will help me in case I install a bad package, I want to experiment with possibly new modules, or there may be multiple users wanting to share this one python.

Python provides a nice tool for this called virtual environments. When you use this utility, it essentially copies all of the files necessary to run python to the directory of your choice–creating a clone of the master python that you can do with whatever you want. I prefer to have my working environment in the base of my home directory structure, but you could put it wherever you want again.

/usr/local/mypython/built/bin/python3 -m venv ~/venv

You will see that Python now installed in your home directory.

Setting Your Path

While you personally know these files are here, your computer is not as wise. You need to let your computer know where this python is. You can do this in a few ways: update your python path, update your bash path, or set up an alias. I prefer to update the bash path because, as you will see in that folder of binaries, there are other utilities that come with python we will be using. To do this, open a text editor for the file ~/.bash_profile and include this string somewhere near the end (make sure it appears before any export $PATH statements):


This command redefines the PATH variable for bash to include first our custom python followed by all the other locations in the PATH variable prior to our definition. (Note that paths are separated by a colon “:” in the path syntax.) Make sure you activate your profile file so that it takes effect immediately:

. ~/.bash_profile

Installing Your Modules

Now you are ready to install whatever modules you need. It is beyond the scope of this tutorial to handle library compilation if needed, but pip provides an easy interface to the Python package directory, where many modules are compiled to include libraries needed. In any case, you can use pip to install whichever modules you desire; just first make sure you activated your profile above by verifying the location of the pip executable

which pip
$ ~/venv/bin/pip
pip install matplotlib numpy scipy


You have now successfully built Python 3.5 from source, installed it to a custom location, created a virtual environment in your home directory, and installed modules within that virtual environment. Congratulations! (And no sysadmin was required!)

[top] | « Back to Blog