Installation

In this tutorial you will be building a blog. In order to do that, as you go through the tutorial you'll be instructed on how to install various software on your computer and set up some online accounts as they are needed. This page gathers all of the installation and sign-up instructions in one place (which is useful for some workshop formats).

Brief intro to the command line

Many of the steps below reference the "console", "terminal", "command window", or "command line" -- these all mean the same thing: a window on your computer where you can enter commands. When you get to the main tutorial, you'll learn more about the command line. For now, the main thing you need to know is how to open a command window and what it looks like:

Opening: OS X

Go to Applications → Utilities → Terminal.

Opening: Linux

It's probably under Applications → Accessories → Terminal, or Applications → System → Terminal, but that may depend on your system. If it's not there, you can try to Google it. :)

Install Git

Git is a "version control system" used by a lot of programmers. This software can track changes to files over time so that you can recall specific versions later. A bit like the "track changes" feature in word processor programs (e.g., Microsoft Word or LibreOffice Writer), but much more powerful.

Installing Git

Installing Git: Windows

You can download Git from git-scm.com. You can hit "next" on all steps except for two: in the step where it asks to choose your editor, you should pick Nano, and in the step entitled "Adjusting your PATH environment", choose "Use Git and optional Unix tools from the Windows Command Prompt" (the bottom option). Other than that, the defaults are fine. Checkout Windows-style, commit Unix-style line endings is good.

During installation, if you are presented with the option of "Adjusting the name of the initial branch in new repositories", please choose to "Override the default" and use "main". This will align your installation of Git with the broad direction of the global developer community, and the "main" branch will be used through the remainder of this tutorial. Please see https://sfconservancy.org/news/2020/jun/23/gitbranchname/ and https://github.com/github/renaming for further discussion of this subject.

Do not forget to restart the command prompt or PowerShell after the installation finished successfully.

Installing Git: OS X

Download Git from git-scm.com and follow the instructions.

During installation, if you are presented with the option of "Adjusting the name of the initial branch in new repositories", please choose to "Override the default" and use "main". This will align your installation of Git with the broad direction of the global developer community, and the "main" branch will be used through the remainder of this tutorial. Please see https://sfconservancy.org/news/2020/jun/23/gitbranchname/ and https://github.com/github/renaming for further discussion of this subject.

Note If you are running OS X 10.6, 10.7, or 10.8, you will need to install the version of git from here: Git installer for OS X Snow Leopard

Installing Git: Debian or Ubuntu

command-line

$ sudo apt install git

Adjusting your default branch name

This will align your installation of Git with the broad direction of the global developer community, and the "main" branch will be used through the remainder of this tutorial. Please see https://sfconservancy.org/news/2020/jun/23/gitbranchname/ and https://github.com/github/renaming for further discussion of this subject.

command-line

$ git config --global --add init.defaultBranch main
Installing Git: Fedora

command-line

$ sudo dnf install git

Adjusting your default branch name

This will align your installation of Git with the broad direction of the global developer community, and the "main" branch will be used through the remainder of this tutorial. Please see https://sfconservancy.org/news/2020/jun/23/gitbranchname/ and https://github.com/github/renaming for further discussion of this subject.

command-line

$ git config --global --add init.defaultBranch main
Installing Git: openSUSE

command-line

$ sudo zypper install git

Adjusting your default branch name

This will align your installation of Git with the broad direction of the global developer community, and the "main" branch will be used through the remainder of this tutorial. Please see https://sfconservancy.org/news/2020/jun/23/gitbranchname/ and https://github.com/github/renaming for further discussion of this subject.

command-line

$ git config --global --add init.defaultBranch main

Install Python

First check to see if you already have pyenv installed by running this in a terminal window:

pyenv

If it is already installed, either update it using pyenv update or using the package manager that you used to install it.

If it is not installed you can install it using the following command, pasted into a new terminal window:

curl https://pyenv.run | bash

The output of the command tells you to add certain lines to your startup files for your terminal sessions. Follow the PyEnv setup instructions copied below - if you are unsure which section to follow, you are probably using a bash shell.

  • For bash:

    Stock Bash startup files vary widely between distributions in which of them source which, under what circumstances, in what order and what additional configuration they perform. As such, the most reliable way to get Pyenv in all environments is to append Pyenv configuration commands to both .bashrc (for interactive shells) and the profile file that Bash would use (for login shells).

    First, add the commands to ~/.bashrc:

    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
    echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
    echo 'eval "$(pyenv init -)"' >> ~/.bashrc
    echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
    

    Then, if you have ~/.profile, ~/.bash_profile or ~/.bash_login, add the commands there as well. If you have none of these, add them to ~/.profile.

    • to add to ~/.profile:

      echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile
      echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile
      echo 'eval "$(pyenv init -)"' >> ~/.profile
      echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.profile
      
    • to add to ~/.bash_profile:

      echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
      echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
      echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
      echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
      
  • For Zsh:

    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
    echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
    echo 'eval "$(pyenv init -)"' >> ~/.zshrc
    echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.zshrc
    

    If you wish to get Pyenv in noninteractive login shells as well, also add the commands to ~/.zprofile or ~/.zlogin.

  • For Fish shell:

    Execute this interactively:

    set -Ux PYENV_ROOT $HOME/.pyenv
    set -U fish_user_paths $PYENV_ROOT/bin $fish_user_paths
    

    And add this to ~/.config/fish/config.fish:

    pyenv init - | source
    

    Bash warning: There are some systems where the BASH_ENV variable is configured to point to .bashrc. On such systems, you should almost certainly put the eval "$(pyenv init -)" line into .bash_profile, and not into .bashrc. Otherwise, you may observe strange behaviour, such as pyenv getting into an infinite loop. See #264 for details.

    Proxy note: If you use a proxy, export http_proxy and https_proxy, too.

Restart your shell

for the PATH changes to take effect.

  exec "$SHELL"

Install Python build dependencies

Install Python build dependencies before attempting to install a new Python version.

You can now begin using Pyenv.

Installing a Python Version with PyEnv

Use the pyenv command in your terminal to install a recent version of Python:

pyenv install 3.9.13

The PyEnv installation wiki provides a list of common issues when installing Python versions on different operating systems.

You can now activate this version of Python just for this shell session:

pyenv shell 3.9.13

Now if you check the Python version it should be 3.9.13:

python --version

See the PyEnv documentation for more detailed usage of the pyenv command.

Install a code editor

There are a lot of different editors and it largely boils down to personal preference. Most Python programmers use complex but extremely powerful IDEs (Integrated Development Environments), such as VSCode and PyCharm.

Our suggestions are below, all of which either have strong built in support for Python and Javascript programming, or have a rich library of extensions and plugins to support a wide range of functionality and features.

Visual Studio Code/VSCodium

Visual Studio Code is a source code editor developed by Microsoft for Windows, Linux and macOS. It includes support for debugging, embedded Git control, syntax highlighting, intelligent code completion, snippets, and code refactoring.

Download it here

Visual Studio Code is built around an open source core, but the released software is under a proprietary license. If you would prefer a free/libre version of Visual Studio Code, VSCodium releases a free build that contains no telemetry or tracking, and no proprietary software.

Download it here

PyCharm

PyCharm is another popular editor. Its community edition is free, open-source and available for Windows, OS X and Linux. PyCharm is developed by JetBrains.

Download it here

Sublime Text

Sublime Text is a very popular editor with a free evaluation period and it's available for all operating systems.

Download it here

Why are we installing a code editor?

You might be wondering why we are installing this special code editor software, rather than using something like Word or Notepad.

The first reason is that code needs to be plain text, and the problem with programs like Word and Textedit is that they don't actually produce plain text, they produce rich text (with fonts and formatting), using custom formats like RTF (Rich Text Format).

The second reason is that code editors are specialized for editing code, so they can provide helpful features like highlighting code with color according to its meaning, or automatically closing quotes for you.

We'll see all this in action later. Soon, you'll come to think of your trusty old code editor as one of your favorite tools. :)

Set up virtualenv and install Django

Part of this section is based on tutorials by Geek Girls Carrots (https://github.com/ggcarrots/django-carrots).

Part of this section is based on the django-marcador tutorial licensed under the Creative Commons Attribution-ShareAlike 4.0 International License. The django-marcador tutorial is copyrighted by Markus Zapke-Gründemann et al.

Virtual Environments

Virtual environments allow a developer to have an encapsulated Python environment, using a specific version of Python, and with dependencies installed in a way that only affect the virtual environment. This is important as different projects or even different versions of the same project may have different dependencies, and virtual environments allow you to switch between them seamlessly and explicitly.

Using pyenv virtualenv with pyenv

To create a virtualenv for the Python version used with pyenv, run pyenv virtualenv, specifying the Python version you want and the name of the virtualenv directory. For example, because we can make a virtual environment for Kolibri using Python 3.9.9:

$ pyenv virtualenv 3.9.9 djangotutorial-py3.9

If you get 'command not found' or a similar error, and pyenv virtualenv is not installed, please follow the installation instructions.

will create a virtualenv based on Python 3.9.9 under $(pyenv root)/versions in a folder called kolibri-py3.9.

List existing virtualenvs

pyenv virtualenvs shows you the list of existing virtualenvs and conda environments.

$ pyenv virtualenvs
  3.9.9/envs/djangotutorial-py3.9 (created from /home/youuuu/.pyenv/versions/3.9.9)
  djangotutorial-py3.9 (created from /home/youuuu/.pyenv/versions/3.9.9)

There are two entries for each virtualenv, and the shorter one is just a symlink.

Activate virtualenv

If you want a virtual environment to always activate when you enter a certain directory, you can use the pyenv local command.

mkdir django-tutorial
cd django-tutorial
pyenv local djangotutorial-py3.9

Now whenever you enter the directory, the virtual environment will be activated.

You can also activate and deactivate a pyenv virtualenv manually:

pyenv activate djangotutorial-py3.9
pyenv deactivate

Delete existing virtualenv

Removing the directories in $(pyenv root)/versions and $(pyenv root)/versions/{version}/envs will delete the virtualenv, or you can run:

pyenv uninstall djangotutorial-py3.9

You can also delete existing virtualenvs by using virtualenv-delete command, e.g. you can run:

pyenv virtualenv-delete djangotutorial-py3.9

This will delete virtualenv called djangotutorial-py3.9.

For more information on use of virtual environments see the pyenv-virtualenv documentation.

Installing Django

Now that you have your virtualenv started, you can install Django.

Before we do that, we should make sure we have the latest version of pip, the software that we use to install Django:

command-line

(myvenv) ~$ python -m pip install --upgrade pip

Installing packages with requirements

A requirements file keeps a list of dependencies to be installed using pip install:

First create a requirements.txt file inside of the django-tutorial/ folder, using the code editor that you installed earlier. You do this by opening a new file in the code editor and then saving it as requirements.txt in the django-tutorial/ folder. Your directory will look like this:

django-tutorial
└───requirements.txt

In your django-tutorial/requirements.txt file you should add the following text:

django-tutorial/requirements.txt

Django~=3.2.13

Now, run pip install -r requirements.txt to install Django.

command-line

(myvenv) ~$ pip install -r requirements.txt
Collecting Django~=3.2.13 (from -r requirements.txt (line 1))
  Downloading Django-3.2.13-py3-none-any.whl (7.9MB)
Installing collected packages: Django
Successfully installed Django-3.2.13

That's it! You're now (finally) ready to create a Django application!

Create a GitHub account

If you do not have a Github account, go to GitHub.com and sign up for a new, free user account. Be sure to remember your password (add it to your password manager, if you use one).

Additional reading

Congratulations, you are all set up and ready to go! If you would like to know more, these chapters can give additional background information:

Start building a Django project!

Go straight to Your first Django project!.

results matching ""

    No results matching ""